0% found this document useful (0 votes)
0 views

Android R8

R8 is a compiler that optimizes Java bytecode into DEX code for Android, providing code shrinking, obfuscation, and improved efficiency compared to ProGuard. It maintains compatibility with ProGuard rules, allowing for a seamless transition, and offers features like resource shrinking and performance optimization. R8 operates in compatibility and full modes, with the latter requiring additional configuration for aggressive optimizations, and is the default compiler in Android Gradle plugin 3.4.0 and above.

Uploaded by

itsmemashood
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Android R8

R8 is a compiler that optimizes Java bytecode into DEX code for Android, providing code shrinking, obfuscation, and improved efficiency compared to ProGuard. It maintains compatibility with ProGuard rules, allowing for a seamless transition, and offers features like resource shrinking and performance optimization. R8 operates in compatibility and full modes, with the latter requiring additional configuration for aggressive optimizations, and is the default compiler in Android Gradle plugin 3.4.0 and above.

Uploaded by

itsmemashood
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGUARD vs R8

--------------

1. Introduction to R8

- R8 Overview: R8 is a compiler that converts Java bytecode into optimized DEX


code for Android. It performs multiple tasks including optimization, shrinking, and
obfuscation to ensure your app is efficient and secure.
- Purpose of R8: Protects your code from decompilation and unauthorized access
by obfuscating class names, methods, and variables, making reverse engineering
difficult.

2. R8 vs ProGuard

- Historical Context: Before R8, Android used ProGuard for code shrinking and
obfuscation. ProGuard optimized bytecode, but required additional steps to convert
it to DEX code.
- R8 Advantage: Unlike ProGuard, R8 directly compiles bytecode to optimized DEX
code, reducing the steps involved and improving efficiency.

- ProGuard Rules Compatibility: R8 uses the same configuration format as


ProGuard, ensuring seamless transition without the need to modify existing ProGuard
rules.
- Reflection Consideration: Some parts of the code, particularly those using
reflection, may require specific configuration to avoid being removed or obfuscated
by R8.

3. Key Features of R8

- Code Shrinking: Detects and removes unused classes, methods, and variables,
particularly useful for third-party libraries where only a small portion of it is
used.
- Resource Shrinking: Eliminates unused resources.
- Optimization: Optimizes code for performance, which helps to improve the
runtime performance and reduces DEX file size.
- Obfuscation: Obfuscates the code by renaming classes, methods, and variables
to random names, making it difficult to reverse engineer the app.

4. R8 Modes

- Compatibility mode: This mode is meant to make the transition to R8 from


ProGuard easier by limiting the optimizations performed by R8. This was the default
mode until Android Gradle plugin 8.0.
- Full mode: In this mode, also called non-compat mode, R8 performs more
aggressive optimizations, meaning additional ProGuard configuration rules may be
required. This is the default mode in Android Gradle plugin 8.0+.

5. Proguard rules

- ProGuard rules are the configurations that guide the ProGuard tool in
performing code shrinking, obfuscation, and optimization. R8 uses the same
configuration specification language as ProGuard, and tries to be compatible with
ProGuard.
- Common Proguard Rules
- Keep Rules:
Keep rules prevent specific classes, methods, or fields from
being removed or obfuscated during the optimization process.
// To keep an entire class:
-keep class com.example.MyClass { *; }
// To keep a specific method within a class:
-keep class com.example.MyClass { void myMethod(); }

- Keep Data Classes:


Data classes, often used for serialization and deserialization,
should be preserved during ProGuard optimization. Modifying these classes can lead
to issues with data restoration and access, disrupting the application's
functionality.

- Keep Names:
To retain the original names of specific classes, methods, or
fields during obfuscation.
// To keep the name of a class:
-keepnames class com.example.MyClass
// To keep the name of a specific method within a class:
-keepnames class com.example.MyClass { void myMethod(); }

- Keep Attributes:
To retain certain attributes or annotations during the
optimization process.
// To keep the Signature attribute:
-keepattributes Signature

- Optimization Rules:
To control specific optimizations by enabling or disabling them.
// To disable arithmetic simplification optimization:
-optimizations !code/simplification/arithmetic

- Assume No Side Effects:


To enhance obfuscation by indicating that certain methods have no
side effects.
// To indicate that methods in android.util.Log have no side
effects:
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** e(...);
}

5. To note

- R8 is the default compiler in Android Gradle plugin 3.4.0+


- Full mode is the default mode for R8 in Android Gradle plugin 8.0+, means
you may need to add additional proguard rules when updating to AGP 8.0+
- It is not possible to disable R8 completely, but you can disable the full
mode by adding android.enableR8.fullMode=false in the gradle.properties file (it is
not recommended to do that).

6. Known issues with full mode

- Lets have a look into the known issues with libraries that use reflection.

Add this somewhere if needed...

android {
buildTypes {
getByName("release") {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type. Make sure to use a build
// variant with `isDebuggable=false`.
isMinifyEnabled = true

// Enables resource shrinking, which is performed by the


// Android Gradle plugin.
isShrinkResources = true

// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
...
}

You might also like