AOSP System app Android.bp集成Leakcanary检测内存泄漏


本文以Settings模块为例,继承Leakcanary 2.12

确定依赖的aar和jar包。

两种方式确定,一种方式是在android studio中通过在build.gradle中引入leaknacary,然后查看external libraries中新增的以来,到maven center下载对应版本的jar包和aar包。 另一种方式是直接在github release页面下载leakcanary源码自行编译出aar和jar依赖。

Settings集成leakcanary

定位到packages/apps/Settings目录。将依赖的jar包和aar包拷贝到libs目录.

白俊遥博客

在libs目录下新建Android.bp文件:

java_import {
    name: "leakcanary-object-watcher",
    jars: ["leakcanary-object-watcher-2.12.jar"],
    libs: ["shark-log"],
    sdk_version: "current",
}
java_import {
    name: "okio-2.2.2",
    jars: ["okio-2.2.2.jar"],
    sdk_version: "current",
}
java_import {
    name: "shark",
    jars: ["shark-2.12.jar"],
    libs: ["shark-graph","okio-2.2.2"],
    sdk_version: "current",
}
java_import {
    name: "shark-android",
    jars: ["shark-android-2.12.jar"],
    libs: ["shark"],
    sdk_version: "current",
}
java_import {
    name: "shark-graph",
    jars: ["shark-graph-2.12.jar"],
    libs: ["shark-hprof","okio-2.2.2"],
    sdk_version: "current",
}
java_import {
    name: "shark-hprof",
    jars: ["shark-hprof-2.12.jar"],
    libs: ["shark-log"],
    sdk_version: "current",
}
java_import {
    name: "shark-log",
    jars: ["shark-log-2.12.jar"],
    sdk_version: "current",
}

android_library_import {
    name: "leakcanary-android-utils",
    aars: ["leakcanary-android-utils-2.12.aar"],
    static_libs: ["shark-log"],
    sdk_version: "current",
}
android_library_import {
    name: "leakcanary-object-watcher-android-core",
    aars: ["leakcanary-object-watcher-android-core-2.12.aar"],
    static_libs: ["leakcanary-object-watcher","leakcanary-android-utils","curtains-1.2.4"],
    sdk_version: "current",
}
android_library_import {
name: "curtains-1.2.4",
aars: ["curtains-1.2.4.aar"],
sdk_version: "current",
}

android_library_import {
    name: "leakcanary-object-watcher-android",
    aars: ["leakcanary-object-watcher-android-2.12.aar"],
    static_libs: ["leakcanary-object-watcher-android-core"],
    sdk_version: "current",
}

android_library_import {
    name: "leakcanary-object-watcher-android-androidx",
    aars: ["leakcanary-object-watcher-android-androidx-2.12.aar"],
    static_libs: ["leakcanary-object-watcher-android-core"],
    sdk_version: "current",
}

android_library_import {
    name: "leakcanary-object-watcher-android-support-fragments",
    aars: ["leakcanary-object-watcher-android-support-fragments-2.12.aar"],
    static_libs: ["leakcanary-object-watcher-android-core"],
    sdk_version: "current",
}

android_library_import {
    name: "leakcanary-android-core",
    aars: ["leakcanary-android-core-2.12.aar"],
    static_libs: ["shark-android","leakcanary-object-watcher-android-core",
"leakcanary-object-watcher-android-androidx",
"leakcanary-object-watcher-android-support-fragments"
],
sdk_version: "current",
}

android_library_import {
    name: "leakcanary-android",
    aars: ["leakcanary-android-2.12.aar"],
    static_libs: ["leakcanary-android-core","leakcanary-object-watcher-android",
"leakcanary-object-watcher-android-androidx",
"leakcanary-object-watcher-android-support-fragments"
],
    sdk_version: "current",
}

在Settings/Android.bp中引入依赖:

android_app {
    name: "Settings",
    defaults: ["platform_app_defaults"],
    platform_apis: true,
    certificate: "platform",
    system_ext_specific: true,
    privileged: true,
    required: [
        "privapp_whitelist_com.android.settings",
        "settings-platform-compat-config",
    ],
    static_libs: ["Settings-core","leakcanary-android",
"leakcanary-android-core",
"leakcanary-object-watcher-android-support-fragments",
"leakcanary-object-watcher-android-androidx",
"leakcanary-object-watcher-android",
"leakcanary-object-watcher-android-core",
"leakcanary-android-utils",
"shark-log",
"shark-hprof",
"shark-graph",
"shark-android",
"shark",
"leakcanary-object-watcher",
"curtains-1.2.4"],
    uses_libs: ["org.apache.http.legacy"],
    resource_dirs: [],
    optimize: {
        proguard_flags_files: ["proguard.flags"],
    },
}

完事后mma单编模块,就会在out/target/product/productName/system/system_ext/priv_app/Settings目录下生成Settings.apk。

问题处理


将生成的apk安装到手机后,打开Settings闪退,logcat信息会报错提示:

LeakCanary should only be used in debug builds, but this APK is not debuggable

这里我直接修改leakcanary源码,leakcanary-android-core/src/main/java/leakcanary/internal/InternalLeakCanary.kt:

//  private val isDebuggableBuild by lazy {//    (application.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0//  }
    private val isDebuggableBuild = true

将isDebuggableBuild直接返回true,重新build生成leakcanary-android-core.aar重命名替换libs目录下的leakcanary-android-core-2.12.aar即可,这是我编译好的包:

https://www.lixiaopeng.top/oepnsource/leakcanary-2.12-aar.zip


最终效果:
白俊遥博客


Adam博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论
  • Powered by bjyblog modified by Adam © 2014-2024 www.lixiaopeng.com 版权所有 ICP证:鲁ICP备15039297号
  • 联系邮箱:14846869@qq.com