Skip to content

Instantly share code, notes, and snippets.

@osipxd
Last active July 4, 2024 18:38
Show Gist options
  • Save osipxd/d4f65accb53973ef1bb306680153f160 to your computer and use it in GitHub Desktop.
Save osipxd/d4f65accb53973ef1bb306680153f160 to your computer and use it in GitHub Desktop.
The task to add debuggable=true to obfuscated Android builds
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)
}
}
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