One-line summary: AutoKeep is an automatic keep-rule generation tool for Android obfuscation scenarios. It infers model dependencies from Retrofit interface return types and generates R8/ProGuard rules.
- Scans methods annotated with
retrofit2.http.*as entry points. - Automatically infers the model dependency graph from Retrofit method return types.
- Recursively analyzes field dependencies and generates corresponding keep rules.
- Supports unwrapping common wrapper and collection types:
Call<T>,Response<T>,List<T>,Set<T>,Map<K, V>, andArray<T>. - Supports additional keep rules for reflection-based classes.
- Supports generating keep rules for JNI methods.
- Supports generating keep configuration related to runtime annotations.
app: Android sample module in this repository, used to demonstrate plugin integration and generated rule output.autokeep-processor: KSP processor responsible for type analysis and keep-rule generation.autokeep-gradle-plugin: Gradle plugin responsible for wiring KSP automatically, adding processor dependencies, and syncing generated rules.
Add the following to your target project's settings.gradle.kts:
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}Apply the plugin in your Android application or library module's build.gradle.kts:
plugins {
id("com.android.application")
kotlin("android")
id("com.opoojkk.autokeep") version "<version>"
}The plugin automatically does the following:
- Applies
com.google.devtools.ksp. - Adds the
autokeep-processordependency automatically. - Passes
reflectClassesand auto-discovered provider rules through as KSP arguments. - Registers the generated
autokeep-rules.proas an R8/ProGuard input for Android variants.
If your project contains classes referenced directly via reflection, you can add them in the extension:
autokeep {
reflectClasses.add("com.xxx.YourReflectClass")
reflectClasses.add("com.xxx.AnotherReflectClass")
}If you want to centralize custom rules behind a reusable provider class, implement AutoKeepRuleProvider:
import com.opoojkk.autokeep.gradle.AutoKeepRuleProvider
open class MyCompanyAutoKeepRules : AutoKeepRuleProvider() {
override fun provideClasses(): List<Class<*>> = listOf(
LegacySdk::class.java,
NativeEntry::class.java
)
}Place the implementation in buildSrc or another shared build-logic module. AutoKeep discovers subclasses automatically during configuration:
// buildSrc/src/main/kotlin/MyCompanyAutoKeepRules.kt
open class MyCompanyAutoKeepRules : AutoKeepRuleProvider() {
override fun provideClasses(): List<Class<*>> = listOf(
LegacySdk::class.java
)
}After the build completes, you can find the generated rules in the KSP output directory:
build/generated/ksp/<variant>/resources/autokeep-rules.pro
The plugin also syncs the rules to a location that can be consumed directly by Android variants, for example:
app/build/intermediates/autokeep/release/autokeep-rules.pro
Example output:
# Retrofit models
-keep class com.opoojkk.autokeep.model.UserResponse { *; }
# Custom rules
-keep class com.xxx.LegacySdk { *; }
# JNI
-keepclasseswithmembers class * { native <methods>; }
# Runtime annotations
-keepattributes *Annotation*
This repository includes a standalone consumer sample project, sample-consumer. Its integration approach is the same as described above and can be used as a reference.
The app module in this repository is also a plugin integration example.
This repository currently uses the GPL-3.0 license. The Gradle publishing metadata has also been written to the POM. If the license changes in the future, update both the LICENSE file and the POM_LICENSE_* configuration in the root gradle.properties.
See docs/IMPLEMENTATION_TRACKER.md for details.