Android library native 代碼不能調(diào)試解決方法匯總

android native開發(fā)會碰到native代碼無法調(diào)試問題,而app主工程中的native代碼是可以調(diào)試的。如果項目中存在多個module,那么在application模塊中依賴library模塊,并且library模塊中有native代碼的時候,當(dāng)debug library模塊中的這些native代碼時可能會發(fā)現(xiàn)斷點打不進去。導(dǎo)致這個問題的根本原因是因為即使在運行application模塊的debug構(gòu)建時,其依賴的library模塊并不是以debug構(gòu)建,而是以release構(gòu)建。

方法一

在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"
            //...
        }
    }
}

ibrary模塊和application模塊中的gradle都需要加入。但是打正式release包的時候是需要剔除so的符號表的,防止so被破解。因此,最好配置一個開關(guān),且這個開關(guān)不會被提交到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的生成:

image.png

點擊執(zhí)行assembleDebug。
然后配置Symbol Directories中的符號表目錄。
image.png

方法三

image.png

在Project Structure中,對應(yīng)module的Debuggable和Jni Debuggable置為true。

參考資料

http://www.itdecent.cn/p/76957970545e/

https://fucknmb.com/2017/05/11/Android-Studio-Library%E6%A8%A1%E5%9D%97%E4%B8%ADNative%E4%BB%A3%E7%A0%81%E8%BF%9B%E8%A1%8Cdebug%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/

http://www.itdecent.cn/p/7f80be68f99b

http://www.itdecent.cn/p/76957970545e/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容