温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2023-04-17 15:10:02 来源:亿速云 阅读:98 作者:iii 栏目:开发技术

这篇文章主要介绍了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的生成:

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

点击执行assembleDebug。

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

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

方法三

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

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

关于“Android library native调试代码遇到问题怎么解决”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Android library native调试代码遇到问题怎么解决”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI