前言

android native开发会碰到native代码无法调试问题,而app主工程中的native代码是可以调试的。如果项目中存在多个module,那么在application模块中依赖library模块,并且library模块中有native代码的时候,当debug library模块中的这些native代码时可能会发现断点打不进去。导致这个问题的根本原因是因为即使在运行application模块的debug构建时,其依赖的library模块并不是以debug构建,而是以release构建。

方法一

在library模块和application模块中加入忽略strip的正则匹配,如下

android {
    //...
   if (isDebug()) {
        packagingOptions {
            doNotStrip "*/armeabi/*.so"
            doNotStrip "*/armeabi-v7a/*.so"
            doNotStrip "*/arm64-v8a/*.so"
            doNotStrip "*/x86/*.so"
            doNotStrip "*/x86_64/*.so"
            doNotStrip "*/mips/*.so"
            doNotStrip "*/mips64/*.so"
            //...
        }
    }
}

library模块和application模块中的gradle都需要加入。但是打正式release包的时候是需要剔除so的符号表的,防止so被破解。因此,最好配置一个开关,且这个开关不会被提交到git中去,因此local.properties是最合适的。isDebug方法写在顶层的build.gradle中,这样各个module里边都可以引用。

boolean isDebug() {
    boolean ret = false
    try {
        Properties properties = new Properties()
        File file = project.rootProject.file('local.properties')
        if (!file.exists()) {
            return false
        }
        properties.load(file.newDataInputStream())
        String debugStr = properties.getProperty("debug")
        if (debugStr != null && debugStr.length() > 0) {
            ret = debugStr.toBoolean()
        }
    } catch (Throwable throwable) {
        throwable.printStackTrace()
        ret = false
    }
    project.logger.error("[${project.name}]Debug:${ret}")
    return ret
}

然后在local.properties中加入debug=true,修改packagingOptions配置,增加判读逻辑isDebug()。

如果使用上述方式还不行,将主app也添加cmake,包含native代码。gradle参考配置如下:

plugins {
    id 'com.android.application'
}
android {
    compileSdk 32
    defaultConfig {
        applicationId "com.example.app2"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_ARM_MODE=arm', '-DANDROID_STL=c++_static'
                cppFlags "-std=c++11 -frtti -fexceptions"
            }
        }
        ndk {
            abiFilters "arm64-v8a"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.18.1"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    if(isDebug()){
        packagingOptions {
            doNotStrip "*/armeabi/*.so"
            doNotStrip "*/armeabi-v7a/*.so"
            doNotStrip "*/arm64-v8a/*.so"
            doNotStrip "*/x86/*.so"
            doNotStrip "*/x86_64/*.so"
            doNotStrip "*/mips/*.so"
            doNotStrip "*/mips64/*.so"
            //...
        }
    }
}

方法二

在Run -> Edit Configuration的配置页面,Debugger -> Symbol Directories里面添加第一步生成debug aar的代码目录。

gradle中的task未显示问题:

解决方法: 依次点击:File -> Settings -> Experimental -> 取消勾选 “Do not build Gradle task list during Gradle sync”,如下图所示 最后,sync 一下即可。

debug aar的生成:

点击执行assembleDebug。

然后配置Symbol Directories中的符号表目录。

方法三

在Project Structure中,对应module的Debuggable和Jni Debuggable置为true。