Skip to content

The Gradle plugin for protecting necessary declarations from obfuscation

License

Notifications You must be signed in to change notification settings

g000sha256/keep

Repository files navigation

Keep

Maven Central

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.

Initialization

Add plugin repository

repositories {
    mavenCentral()
}

Apply plugin

plugins {
    id("dev.g000sha256.keep") version "1.0.0"
}

Plugin customization

Library module

keep {
    outputDirectory = <File>
    type = <KeepLibraryExtension.Type> // All / ApiOnly / ReflectionOnly
}

Application module

Annotation and rules for API don't apply to the application module.

keep {
    outputDirectory = <File>
}

Generate rules

Apply annotations and generate rules with the command:

./gradlew keepGenerate

Annotations usage

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.

Classes

@KeepApi
class TestClass
class TestParentClass {

    @KeepApi
    class TestInnerClass

}

Objects

@KeepApi
object TestObject

Companion objects

class TestClass {

    @KeepApi
    companion object

}
class TestClass {

    @KeepApi
    companion object TestCompanionObject

}

Getters

class TestClass {

    @get:KeepApi
    val testValue: Int = 0

}

Setters

class TestClass {

    @set:KeepApi
    var testValue: Int = 0

}

Constructors

class TestClass @KeepApi constructor(val testParameter: Int)

Constructor parameters

class TestClass(@get:KeepApi val testParameter: Int)

Methods

class TestClass {

    @KeepApi
    fun testMethod() {
    }

}

Roadmap

  • Constructor parameter types
  • Field types
  • Method parameter types
  • @JvmStatic and @JvmField support