欢迎光临
我们一直在努力

Android library native调试代码遇到问题怎么解决

这篇文章主要介绍了Android library native调试代码遇到问题怎么解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android library native调试代码遇到问题怎么解决文章都会有所收获,下面我们一起来看看吧。

方法一

在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调试代码遇到问题怎么解决”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注云搜网行业资讯频道。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。