Skip to content

Add sample project for showcasing different R8 scenarios - #369

Draft
AjeshRPai wants to merge 1 commit into
mainfrom
R8-Samples
Draft

AjeshRPai wants to merge 1 commit into
mainfrom
R8-Samples

Conversation

@AjeshRPai

Copy link
Copy Markdown
Contributor

This pull request introduces a comprehensive Android sample project (UsedByNativeSample) that demonstrates the use of the androidx.annotation.keep Gradle plugin and best practices for keeping code reachable when using reflection, JNI, and serialization with R8/Proguard. It includes a fully configured app module, native C++ integration, multiple Kotlin and Java examples illustrating reflection and code retention scenarios, and detailed documentation for setup and testing.

The most important changes are:

Project Setup and Documentation

  • Added a new sample project (UsedByNativeSample) with a detailed README.md explaining how to use the androidx.annotation.keep plugin, including instructions for both Android and Java/Kotlin modules.
  • Added .gitignore files for both the project root and app module to exclude build artifacts and IDE files. [1] [2]

App Module Configuration

  • Created a complete build.gradle.kts for the app, enabling Compose, native builds, Kotlin Parcelize, and integrating the annotation-keep plugin.
  • Added a standard AndroidManifest.xml and instrumented test (ExampleInstrumentedTest.kt). [1] [2]

JNI and Native Code Integration

  • Added C++ JNI code (native-lib.cpp and CMakeLists.txt) demonstrating both downcalls (Kotlin→C++) and upcalls (C++→Kotlin), with comments on required keep rules. [1] [2]
  • Implemented Kotlin classes (JniBridge, NativeLib, etc.) to interact with the native library and show JNI usage in the app UI.

Reflection and Code Retention Examples

  • Added multiple Kotlin examples showing common reflection scenarios that require keep rules, such as accessing methods and fields via reflection, loading classes, and using annotations to drive reflection logic. [1] [2] [3] [4] [5]
  • Included a Java annotation (UsedByNative) to mark members used by native code for retention by R8/Proguard.

Serialization and Parcelable

  • Added examples for custom Java serialization (SerializableExample.kt) and Kotlin Parcelable (ParcelableExample.kt), demonstrating the need for keep rules to preserve serialization methods. [1] [2]

These changes together provide a robust reference for handling code shrinking and reflection in modern Android development.

References: [1] [2] [3] [4] [5]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant