Android library native调试代码遇到的问题解决
前言
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。
到此这篇关于Android library native调试代码遇到的问题解决的文章就介绍到这了,更多相关Android library native调试代码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
ERROR/AndroidRuntime(17121)的问题解决
ERROR/AndroidRuntime(17121)的问题解决,需要的朋友可以参考一下2013-05-05flutter BottomAppBar实现不规则底部导航栏
这篇文章主要为大家详细介绍了flutter BottomAppBar实现不规则底部导航栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2019-07-07Android开发中ProgressDialog简单用法示例
这篇文章主要介绍了Android开发中ProgressDialog简单用法,结合实例形式分析了Android使用ProgressDialog的进度条显示与关闭、更新等事件响应相关操作技巧,需要的朋友可以参考下2017-10-10Android onCreateOptionsMenu的使用方法总结
这篇文章主要介绍了Android onCreateOptionsMenu的使用方法总结的相关资料,在Android下,每一个activity都捆绑了一个Menu,要想定义和使用菜单,都必须在Activity下进行操作,需要的朋友可以参考下2017-08-08Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入
这篇文章主要介绍了Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下2017-01-01
最新评论