Last active
July 4, 2024 18:38
-
-
Save osipxd/d4f65accb53973ef1bb306680153f160 to your computer and use it in GitHub Desktop.
The task to add debuggable=true to obfuscated Android builds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
android { | |
buildTypes { | |
debug { | |
// ... | |
buildConfigField(type = "boolean", name = "DEBUG", value = "true") | |
} | |
} | |
} | |
androidComponents { | |
onVariants(selector().withBuildType("debug")) { | |
val makeDebuggableTask = tasks.register<MakeDebuggableTask>("make${name.capitalize()}Debuggable") | |
it.artifacts.use(makeDebuggableTask) | |
.wiredWithFiles( | |
taskInput = { it.mergedManifest }, | |
taskOutput = { it.debuggableManifest }, | |
) | |
.toTransform(SingleArtifact.MERGED_MANIFEST) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.build | |
import org.gradle.api.DefaultTask | |
import org.gradle.api.file.RegularFileProperty | |
import org.gradle.api.tasks.InputFile | |
import org.gradle.api.tasks.OutputFile | |
import org.gradle.api.tasks.TaskAction | |
/** Adds `android:debuggable="true"` to the manifest. */ | |
abstract class MakeDebuggableTask : DefaultTask() { | |
@get:InputFile | |
abstract val mergedManifest: RegularFileProperty | |
@get:OutputFile | |
abstract val debuggableManifest: RegularFileProperty | |
@TaskAction | |
fun addDebuggableTag() { | |
var manifest = mergedManifest.get().asFile.readText() | |
manifest = if ("android:debuggable" in manifest) { | |
manifest.replace( | |
oldValue = "android:debuggable=\"false\"", | |
newValue = "android:debuggable=\"true\"", | |
) | |
} else { | |
manifest.replace( | |
oldValue = "<application", | |
newValue = "<application\n android:debuggable=\"true\"" | |
) | |
} | |
debuggableManifest.get().asFile.writeText(manifest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment