Skip to content

Commit

Permalink
fix(ManifestPatcher): add vmSafeMode manifest attribute (#73)
Browse files Browse the repository at this point in the history
On certain ROMs like GrapheneOS with agressive dexopt settings, the current method of deleting ART profiles upon startup does not work as dexopt runs right after app installation. This code cache would then be deletable only via ADB or root.
  • Loading branch information
Enovale authored Jun 16, 2024
1 parent 0271511 commit 6b3da4f
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object ManifestPatcher {
private const val ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android"
private const val USES_CLEARTEXT_TRAFFIC = "usesCleartextTraffic"
private const val DEBUGGABLE = "debuggable"
private const val VM_SAFE_MODE = "vmSafeMode"
private const val REQUEST_LEGACY_EXTERNAL_STORAGE = "requestLegacyExternalStorage"
private const val NETWORK_SECURITY_CONFIG = "networkSecurityConfig"
private const val LABEL = "label"
Expand All @@ -30,6 +31,13 @@ object ManifestPatcher {
val writer = AxmlWriter()

reader.accept(object : AxmlVisitor(writer) {
// Without this, decompiling the finished manifest has the android namespace
// under an autogenerated name like axml_00 or something.
override fun ns(prefix: String?, uri: String?, ln: Int) {
val realUri = uri ?: ANDROID_NAMESPACE
super.ns(prefix, realUri, ln)
}

override fun child(ns: String?, name: String?) =
object : ReplaceAttrsVisitor(
super.child(ns, name),
Expand Down Expand Up @@ -88,14 +96,16 @@ object ManifestPatcher {
) {
private var addDebuggable = debuggable
private var addLegacyStorage = true
private var useVmSafeMode = true
private var addUseClearTextTraffic = true

override fun attr(ns: String?, name: String, resourceId: Int, type: Int, value: Any?) {
if (name == NETWORK_SECURITY_CONFIG) return
super.attr(ns, name, resourceId, type, value)
if (name == REQUEST_LEGACY_EXTERNAL_STORAGE) addLegacyStorage = false
if (name == VM_SAFE_MODE) useVmSafeMode = false
if (name == DEBUGGABLE) addDebuggable = false
if (name == USES_CLEARTEXT_TRAFFIC) addUseClearTextTraffic = false
super.attr(ns, name, resourceId, type, value)
}

override fun child(ns: String?, name: String): NodeVisitor {
Expand Down Expand Up @@ -124,9 +134,10 @@ object ManifestPatcher {
}

override fun end() {
if (addLegacyStorage) super.attr(ANDROID_NAMESPACE, REQUEST_LEGACY_EXTERNAL_STORAGE, -1, TYPE_INT_BOOLEAN, 1)
if (addDebuggable) super.attr(ANDROID_NAMESPACE, DEBUGGABLE, -1, TYPE_INT_BOOLEAN, 1)
if (addUseClearTextTraffic) super.attr(ANDROID_NAMESPACE, USES_CLEARTEXT_TRAFFIC, -1, TYPE_INT_BOOLEAN, 1)
if (addLegacyStorage && Build.VERSION.SDK_INT >= 29) super.attr(ANDROID_NAMESPACE, REQUEST_LEGACY_EXTERNAL_STORAGE, android.R.attr.requestLegacyExternalStorage, TYPE_INT_BOOLEAN, 1)
if (useVmSafeMode) super.attr(ANDROID_NAMESPACE, VM_SAFE_MODE, android.R.attr.vmSafeMode, TYPE_INT_BOOLEAN, 1)
if (addDebuggable) super.attr(ANDROID_NAMESPACE, DEBUGGABLE, android.R.attr.debuggable, TYPE_INT_BOOLEAN, 1)
if (addUseClearTextTraffic) super.attr(ANDROID_NAMESPACE, USES_CLEARTEXT_TRAFFIC, android.R.attr.usesCleartextTraffic, TYPE_INT_BOOLEAN, 1)
super.end()
}
}
Expand Down

0 comments on commit 6b3da4f

Please sign in to comment.