覆盖调试版本的 android:vmSafeMode 属性

Override android:vmSafeMode attribute for debug builds

在尝试优化调试应用程序的构建和部署速度时,我发现在安装期间执行 /system/bin/dex2oat 花费了大量时间。这是 ART ahead of time compiler.

我在定位 API 22 时发现您现在可以停止 ART AOT 编译:

<application
    ...
    android:vmSafeMode="true">
 </application>

我发现部署速度有了显着提高,但我担心进行此更改可能产生的副作用。它一定会对运行时性能造成小的影响,但是启用 android:vmSafeMode 选项是否会产生任何其他后果?

是否可以在 gradle 构建文件中为调试构建覆盖此属性?还是创建特定于调试的清单文件是唯一的解决方案?

仅为调试版本启用 android:vmSafeMode 的最佳方法是使用调试清单来补充主要 AndroidManifest.xml.

的内容

要添加它,请创建一个新文件 …/app/src/debug/AndroidManifest.xml 并添加以下内容 xml:

<manifest
 xmlns:android="http://schemas.android.com/apk/res/android"> 
 <application android:vmSafeMode="true" />
</manifest>

添加此调试清单并安装您的应用后,您应该检查设备 logcat 输出以确保在执行 dex2oat 进程时正确应用 vmSafeMode 标志。查找参数 --compiler-filter=interpret-only。 此输出还报告 dex2oat 进程执行所花费的时间,因此您可以比较进行更改前后的情况。

I/dex2oat﹕ /system/bin/dex2oat --zip-fd=6 --zip-location=/data/app/com.testing.sample.myapp-1/base.apk --oat-fd=7 --oat-location=/data/dalvik-cache/arm/data@app@com.testing.sample.myapp-1@base.apk@classes.dex --instruction-set=arm --instruction-set-features=div --runtime-arg -Xms64m --runtime-arg -Xmx512m --compiler-filter=interpret-only --swap-fd=8
I/dex2oat﹕ dex2oat took 1.258ms (threads: 4) arena alloc=0B java alloc=2MB native alloc=502KB free=7MB

也可以使用 aapt 工具检查 APK 是否启用了 vmSafeMode:

aapt list -a myapkfile.apk
...
A: android:vmSafeMode(0x010102b8)=(type 0x12)0xffffffff
...

我还没有看到任何关于删除提前编译导致​​的错误的报告。但是,由于性能下降,您的应用程序可能会暴露出在进行此更改之前不可见的问题。

非常密集的处理可能会慢很多倍。如果您的应用属于此类,最好不要删除提前编译。

我正在为子孙后代恢复这个,因为我知道一个更干净的方法。

您可以在 gradle 中使用清单占位符以避免复制整个清单文件。

在您的 build.gradle 中添加以下内容:

default {
        manifestPlaceholders = [vmSafeModeEnabled: "true"]
}
buildTypes{
    release {
        manifestPlaceholders = [vmSafeModeEnabled: "false"]
    }
}

然后在清单中改用它

android:vmSafeMode="${vmSafeModeEnabled}"

当 gradle 构建运行时,它将根据构建类型应用适当的值。