android ndk 博客,Android NDK 开发流程

[TOC]

一、下载并配置NDK

8de0f40543f7e7f393fb34d260f8ffc4.png

二、创建 Android 项目,进行NDK配置

1、配置好NDK路径之后记得在工程目录的local.properties文件中进行关联,注意要配置自己NDK的路径

268a6e461220a04235024d83cf00935d.png

2、如果建立的是普通的 Android 项目,可按照文章内容进行配置,如果直接建立C/C++项目,下面的配置可以直接忽略的,因为系统自动配置好了。

048677c8edd7dc43eb8ce750da226f43.png

三、在 Android 项目中声明 native 方法publicclassJnitest{

// 声明所需的 native 方法

static{

System.loadLibrary("Jnitest");

}

publicnativeStringgetString();

publicnativevoidsetString(Stringstr);

}

四、编译 native 方法生成 .h 头文件

1、点击 Build –> Make Project,生成 class 文件

2、找到class文件,在app->build->intermediates->classes->debug目录下:

6ed3c703fe708a54e1d9b7f143a578b3.png

3、通过javah命令生成.h头文件,点击 Android Studio 底下菜单中的 Terminal ,一次输入:

cd app\build\intermediates\classes\debug

// 这里注意改成自己的包名

javah-jni com.mtf.ndktest.Jnitest

入下图所示,您的 .h 头文件已经生成成功,默认位置在 debug 目录下

dbc6c78158d4a0aeaccb528f6eef4407.png

4b916a38bd4ddc55296975027ceb62a7.png

/* DO NOT EDIT THIS FILE - it is machine generated */

#include

/* Header for class com_mtf_ndktest_Jnitest */

#ifndef_Included_com_mtf_ndktest_Jnitest

#define_Included_com_mtf_ndktest_Jnitest

#ifdef __cplusplus

extern"C"{

#endif

/*

* Class: com_mtf_ndktest_Jnitest

* Method: getString

* Signature: ()Ljava/lang/String;

*/

JNIEXPORT jstring JNICALLJava_com_mtf_ndktest_Jnitest_getString

(JNIEnv*,jobject);

/*

* Class: com_mtf_ndktest_Jnitest

* Method: setString

* Signature: (Ljava/lang/String;)V

*/

JNIEXPORTvoidJNICALLJava_com_mtf_ndktest_Jnitest_setString

(JNIEnv*,jobject,jstring);

#ifdef __cplusplus

}

#endif

#endif

五、创建 C/C++ 文件实现 .h 文件

创建jni目录,将刚才生成的.h文件复制过来,创建.c/.cpp文件,实现.h文件中的方法,方法名称可以直接从.h文件中复制过来。

89bb8be92eb73f2f385a59840acfeaeb.png

六、编译生成 .so 文件

1、用cmake方式编译so库,创建CMakeLists.txt文件。

#Formore information about usingCMakewithAndroidStudio,read the

#documentation:https://d.android.com/studio/projects/add-native-code.html

#Setsthe minimum version ofCMakerequired to build thenativelibrary.

cmake_minimum_required(VERSION3.4.1)

#Createsand names a library,sets it as either STATIC

#or SHARED,and provides the relative paths to its source code.

#Youcan define multiple libraries,andCMakebuilds themforyou.

#Gradleautomatically packages shared libraries with your APK.

add_library(#Setsthe name of the library.

Jnitest

#Setsthe library as a shared library.

SHARED

#Providesa relative path to your source file(s).

src/main/jni/Jnitest.cpp)

#Searchesfora specified prebuilt library and stores the path as a

#variable.BecauseCMakeincludes system libraries in the search path by

#default,you only need to specify the name of thepublicNDK library

#you want to add.CMakeverifies that the library exists before

#completing its build.

find_library(#Setsthe name of the path variable.

log-lib

#Specifiesthe name of the NDK library that

#you wantCMaketo locate.

log)

#SpecifieslibrariesCMakeshould link to your target library.You

#can link multiple libraries,such as libraries you define inthis

#build script,prebuilt third-party libraries,or system libraries.

target_link_libraries(#Specifiesthe target library.

Jnitest

#Linksthe target library to the log library

#included in the NDK.

${log-lib})

2、配置app module目录下的build.gradle文件

android{

......

defaultConfig{

......

externalNativeBuild{

cmake{

cppFlags""

abiFilters"armeabi","armeabi-v7a","x86"//输出指定cpu体系结构下的so库。

}

}

}

externalNativeBuild{

cmake{

path"CMakeLists.txt"

}

}

......

}

3、编译并看一下结果

publicclassMainActivityextendsAppCompatActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextViewtv=(TextView)findViewById(R.id.tv_test);

tv.setText(Jnitest.getString());

}

}

b783cff768e33d4781df3ec687e7d623.png

(完)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值