Android library native調(diào)試代碼遇到的問題解決
前言
android native開發(fā)會(huì)碰到native代碼無(wú)法調(diào)試問題,而app主工程中的native代碼是可以調(diào)試的。如果項(xiàng)目中存在多個(gè)module,那么在application模塊中依賴library模塊,并且library模塊中有native代碼的時(shí)候,當(dāng)debug library模塊中的這些native代碼時(shí)可能會(huì)發(fā)現(xiàn)斷點(diǎn)打不進(jìn)去。導(dǎo)致這個(gè)問題的根本原因是因?yàn)榧词乖谶\(yùn)行application模塊的debug構(gòu)建時(shí),其依賴的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"
//...
}
}
}
library模塊和application模塊中的gradle都需要加入。但是打正式release包的時(shí)候是需要剔除so的符號(hào)表的,防止so被破解。因此,最好配置一個(gè)開關(guān),且這個(gè)開關(guān)不會(huì)被提交到git中去,因此local.properties是最合適的。isDebug方法寫在頂層的build.gradle中,這樣各個(gè)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的配置頁(yè)面,Debugger -> Symbol Directories里面添加第一步生成debug aar的代碼目錄。
gradle中的task未顯示問題:
解決方法: 依次點(diǎn)擊:File -> Settings -> Experimental -> 取消勾選 “Do not build Gradle task list during Gradle sync”,如下圖所示 最后,sync 一下即可。
debug aar的生成:

點(diǎn)擊執(zhí)行assembleDebug。
然后配置Symbol Directories中的符號(hào)表目錄。

方法三

在Project Structure中,對(duì)應(yīng)module的Debuggable和Jni Debuggable置為true。
到此這篇關(guān)于Android library native調(diào)試代碼遇到的問題解決的文章就介紹到這了,更多相關(guān)Android library native調(diào)試代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android游戲開發(fā):實(shí)現(xiàn)手勢(shì)操作切換圖片的實(shí)例
本文主要介紹 Android游戲開發(fā)實(shí)現(xiàn)手勢(shì)操作切換圖片的實(shí)例,這里整理了詳細(xì)的資料和示例代碼,有開發(fā)Android游戲應(yīng)用的小伙伴可以參考下2016-08-08
Android界面設(shè)計(jì)(APP設(shè)計(jì)趨勢(shì) 左側(cè)隱藏菜單右邊顯示content)
這文章講述了2013年未來(lái)的移動(dòng)APP設(shè)計(jì)趨勢(shì),感覺挺有道理的:Android界面設(shè)計(jì)實(shí)現(xiàn)左側(cè)隱藏菜單右邊顯示content,感興趣的你可以了解下啊,希望本文對(duì)你的APP設(shè)計(jì)提高有所幫助哦2013-01-01
Android?實(shí)現(xiàn)卡片堆疊錢包管理動(dòng)畫效果
這篇文章主要介紹了Android?實(shí)現(xiàn)卡片堆疊錢包管理動(dòng)畫效果,實(shí)現(xiàn)思路是在動(dòng)畫回調(diào)中requestLayout?實(shí)現(xiàn)動(dòng)畫效果,用Bounds?對(duì)象記錄每一個(gè)CardView?對(duì)象的初始位置,當(dāng)前位置,運(yùn)動(dòng)目標(biāo)位置,需要的朋友可以參考下2022-07-07
Android仿一點(diǎn)資訊收藏Toast動(dòng)畫效果
這篇文章主要介紹了Android仿一點(diǎn)資訊收藏Toast動(dòng)畫效果,效果非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
Android簡(jiǎn)易圖片瀏覽器的實(shí)現(xiàn)
最近做了一個(gè)圖片瀏覽小程序,本文主要介紹了Android簡(jiǎn)易圖片瀏覽器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Android中ActionBar以及menu的代碼設(shè)置樣式
這篇文章主要介紹了Android中ActionBar以及menu的代碼設(shè)置樣式的相關(guān)資料,需要的朋友可以參考下2015-07-07
Android入門之實(shí)現(xiàn)自定義Adapter
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)自定義Adapter,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-11-11
Android Back鍵點(diǎn)擊兩次退出應(yīng)用詳解及實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android Back鍵點(diǎn)擊兩次退出應(yīng)用詳解及實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-10-10
Flutter利用Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取效果
這篇文章主要為大家詳細(xì)介紹了如何利用Flutter中的Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03

