Android studio 3.0 调试NDK代码

文章主要描述在Android Studio中依赖NDK时如何进行JNI的单步调试。
如果是单个NDK工程的话,可以直接在设置中设置就行,而在依赖NDK个时候,Android Studio 设置会主动调用release版本,这样就无法单步调试,所以需要进行一些特殊的设置才行。设置方法方法如下:

  • 1.首先在app module的build.gradle中添加如下代码:
android.buildTypes {
    debug{
        debuggable true
        jniDebuggable true
    }
}
  • 2.进行工程的依赖设置,来告诉IDE使用debug的jni版本。在app module的build.gradle中添加代码如下:
dependencies {
    releaseImplementation project(path: ':', configuration: 'release')
    debugImplementation project(path: ':', configuration: 'debug')
}
  • 3.在library module中进行单独设置,来允许debug版本的编译,方法是在library module的build.gradle中添加代码如下:
android {
    publishNonDefault true
}


configurations {
    // Expose the debug version of the library to other modules
    debug
    release
}

这样再进行编译,就可以进行NDK的单步调试了。为了方便大家,我把对应的的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: ':', configuration: 'release')
    debugImplementation project(path: ':', 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'
}

文章转载自:Android Studio NDK多工程单步调试 -- 一个程序猿的胡思乱想

你可能感兴趣的:(Android studio 3.0 调试NDK代码)