文章主要描述在Android Studio中依賴NDK時如何進(jìn)行JNI的單步調(diào)試。
如果是單個NDK工程的話,可以直接在設(shè)置中設(shè)置就行,而在依賴NDK個時候晌缘,Android Studio 設(shè)置會主動調(diào)用release版本,這樣就無法單步調(diào)試痢站,所以需要進(jìn)行一些特殊的設(shè)置才行磷箕。設(shè)置方法方法如下:
-
1.首先在app module的build.gradle中添加如下代碼:
android.buildTypes {
debug{
debuggable true
jniDebuggable true
}
}
-
2.進(jìn)行工程的依賴設(shè)置,來告訴IDE使用debug的jni版本阵难。在app module的build.gradle中添加代碼如下:
dependencies {
releaseImplementation project(path: ':<ndkModuleName>', configuration: 'release')
debugImplementation project(path: ':<ndkModuleName>', configuration: 'debug')
}
-
3.在library module中進(jìn)行單獨(dú)設(shè)置岳枷,來允許debug版本的編譯,方法是在library module的build.gradle中添加代碼如下:
android {
publishNonDefault true
}
configurations {
// Expose the debug version of the library to other modules
debug
release
}
這樣再進(jìn)行編譯呜叫,就可以進(jìn)行NDK的單步調(diào)試了空繁。為了方便大家,我把對應(yīng)的的build.gradle貼下來:
1.app模塊的build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.example.testndkapplication"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug{
debuggable true
jniDebuggable true
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
releaseImplementation project(path: ':<ndkModuleName>', configuration: 'release')
debugImplementation project(path: ':<ndkModuleName>', configuration: 'debug')
}
2.ndk模塊的build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags '-fexceptions'
arguments '-DANDROID_STL=c++_static', '-DANDROID_TOOLCHAIN=clang','-DANDROID_ARM_MODE=arm'
targets "geocraft_base"
}
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'armeabi-v7a','armeabi'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
jniDebuggable true
}
}
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
}
文章轉(zhuǎn)載自:Android Studio NDK多工程單步調(diào)試 -- 一個程序猿的胡思亂想