The Gradle plugin simplifies the process of preserving necessary declarations from obfuscation when using the R8 tool.
It provides the KeepApi
and KeepReflection
annotations, along with corresponding R8 rules that are
automatically applied to your project based on the annotations used.
repositories {
mavenCentral()
}
plugins {
id("dev.g000sha256.keep") version "1.0.0"
}
keep {
outputDirectory = <File>
type = <KeepLibraryExtension.Type> // All / ApiOnly / ReflectionOnly
}
Annotation and rules for API don't apply to the application module.
keep {
outputDirectory = <File>
}
Apply annotations and generate rules with the command:
./gradlew keepGenerate
The KeepApi
and KeepReflection
annotations are used similarly but serve different purposes.
- Use the
KeepReflection
annotation to preserve declarations accessed via reflection. - Use the
KeepApi
annotation to preserve API declarations in library modules.
Tip
You may add annotations only for the necessary declarations. Parent declarations will be added automatically.
@KeepApi
class TestClass
class TestParentClass {
@KeepApi
class TestInnerClass
}
@KeepApi
object TestObject
class TestClass {
@KeepApi
companion object
}
class TestClass {
@KeepApi
companion object TestCompanionObject
}
class TestClass {
@get:KeepApi
val testValue: Int = 0
}
class TestClass {
@set:KeepApi
var testValue: Int = 0
}
class TestClass @KeepApi constructor(val testParameter: Int)
class TestClass(@get:KeepApi val testParameter: Int)
class TestClass {
@KeepApi
fun testMethod() {
}
}
- Constructor parameter types
- Field types
- Method parameter types
@JvmStatic
and@JvmField
support