Jni是android提供的与其他语言交互的接口,常用的就是c或c++,之前写jni代码是非常痛苦的,一方面像写记事本一样没有各种函数提示,一方面无法调试,单靠打log效率非常低,所以之前都有点不想写jni的代码,但是最近得知了jni可以像Java一样动态调试,非常感兴趣,经过一番折腾,把具体步骤介绍一下。
1、环境:
Android studio 2.2以上
Jdk 1.8以上
NDK r12+
安装CMake、LLDB
2、新建工程,将Include C++ Support勾上
3、C++ Standard选默认的就行,下面第一个“Exception Support”代表处理c++异常,最好勾上,点击Finish
4、目录主要变化,因为采用CMake,所以默认C代码放在cpp目录下,然后比较重要的一个文件是CMakeLists.txt
5、CMakeLists介绍,如下是CMakeLists文件的内容:
#Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
include_directories( imported-lib/include/ )
add_library( # Sets the name of the library.
native-lib2
# Sets the library as ashared library.
SHARED
# Provides a relative pathto your source file(s).
# Associated headers in thesame location as their source
# file are automaticallyincluded.
src/main/cpp/native-lib.cpp)
add_library(tmms-au SHARED src/main/cpp/tmms-au-jni.c)
add_library(tmloader STATIC IMPORTED)
set_target_properties( # Specifies the target library.
tmloader
# Specifies theparameter you want to define.
PROPERTIESIMPORTED_LOCATION
# Provides thepath to the library you want to import.
src/main/cpp/libtmloader.a )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of theNDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries(tmms-au tmloader ${log-lib})
target_link_libraries( # Specifies the target library.
native-lib2
# Links the targetlibrary to the log library
# included in theNDK.
${log-lib} )
说明:
1、 include_directories( imported-lib/include/ )作用就是将所有头文件放在一个目录里,然后指定目录位置,以后在c代码中include就不需要加路径了。
2、 add_library(native-lib SHARED src/main/cpp/native-lib.cpp)该函数负责生成so文件相关信息,第一个参数是so文件名称,第二个是类型,比如SHARED就是动态链接库;第三个就是c文件所在路径,注意头文件可以直接在c代码中包含路径就行,可以不用在这边写。另外这个方法也可以用来导入其他的so文件,例如add_library(native-lib SHARED IMPORTED),但是调用这个后一定要通过set_target_properties设置可执行文件的位置。
3、 find_library(log-lib log)是寻找NDK本地so文件,第一个参数是保存要找的so文件的路径变量名,下面会用到,第二个参数就是要找的NDK中的so文件名。
4、 target_link_libraries(native-lib imported-lib ... ${log-lib})作用是将其他的库导入到你的目标so中,第一个参数就是目标so文件,后面的都是要导入的其他库。
5、 接下来就能按照上面的步骤创建多个属于自己的so文件了
6、build.gradle介绍:
applyplugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 17
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-fexceptions"
}
}
ndk {
// Specifies the ABIconfigurations of your native
// libraries Gradle shouldbuild and package with your APK.
abiFilters 'armeabi'
}
}
buildTypes {
release {
minifyEnabled false
proguardFilesgetDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
/* externalNativeBuild {
// Encapsulates your CMake buildconfigurations.
cmake {
// Provides a relative pathto your CMake build script.
path"CMakeLists.txt"
}
}*/
externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include:['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
exclude group: 'com.android.support',module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
testCompile 'junit:junit:4.12'
}
1) defaultConfig.externalNativeBuild,里面就是之前创建项目指定的一些选项,另外defaultConfig.ndk负责指定需要创建哪些类型的so文件,最好指定一下,不然会全部创建,非常慢的。
2) externalNativeBuild{
// Encapsulates your CMake buildconfigurations.
cmake {
// Provides a relative pathto your CMake build script.
path"CMakeLists.txt"
}
这个负责指定CMakeLists.txt文件路径。另外如果想要采用NDK来编译,就要改成externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
7、开始调试
按照上面所有步骤配置好后就能开始调试了,比较麻烦的一点是每次修改CMakeLists.txt最好要clean project一下,不然会出现很多没必要的问题,这种方式和之前gradle-experimental创建的jni调试效果差不多,就是比较方便,不用修改插件了。好了,可以尽情的调试你的c代码了!