[TOC]
一、下载并配置NDK
二、创建 Android 项目,进行NDK配置
1、配置好NDK路径之后记得在工程目录的local.properties文件中进行关联,注意要配置自己NDK的路径
2、如果建立的是普通的 Android 项目,可按照文章内容进行配置,如果直接建立C/C++项目,下面的配置可以直接忽略的,因为系统自动配置好了。
三、在 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目录下:
3、通过javah命令生成.h头文件,点击 Android Studio 底下菜单中的 Terminal ,一次输入:
cd app\build\intermediates\classes\debug
// 这里注意改成自己的包名
javah-jni com.mtf.ndktest.Jnitest
入下图所示,您的 .h 头文件已经生成成功,默认位置在 debug 目录下
/* 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文件中复制过来。
六、编译生成 .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());
}
}
(完)