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代碼了!