AAR Obfuscator: How to Obfuscate an Android Library (2026)
What an AAR obfuscator actually has to do
An AAR (Android Archive) is the package format for an Android library. Unzip one and you find classes.jar (your compiled code), res/, AndroidManifest.xml, R.txt, optional jni/ native libraries and assets, and proguard.txt, which holds the keep rules the library ships to apps. When people search for an "AAR obfuscator", they usually want one of two things:
- Protect the code inside the library before handing it to customers or partners, so class and method names are not readable in a decompiler.
- Make sure the library survives obfuscation when the app that uses it runs R8, without crashes and without blocking the app's own optimisation.
Android's tooling treats these as separate jobs, and Google's guidance for library authors (developer.android.com, "Optimization for library authors") is clear that the second one matters more. This guide covers both, with R8 as the default tool and commercial protectors for cases where renaming is not enough.
Option 1: Let the app's R8 do the work (recommended)
For most libraries, you do not need to obfuscate the AAR at all. When an app enables R8, it shrinks, optimises and renames your library's code together with the app's code. Google's library guidance explains why this beats library-level obfuscation: only during the app build can R8 see how each method of your library is actually used, so whole-program optimisation removes and renames far more than a library build can.
What you must ship is consumer keep rules: rules that keep only what your library reaches through reflection or JNI. They are packaged into the AAR and applied automatically in every consuming app:
// library/build.gradle.kts
android {
defaultConfig {
consumerProguardFiles("consumer-rules.pro")
}
}Rules for writing consumer-rules.pro, from Google's requirements for library authors:
- No package-wide keeps such as
-keep class com.mylibrary.** { *; }. They protect nothing and block optimisation of your library in every app that uses it. - No global options such as
-dontobfuscate,-allowaccessmodificationor-repackageclassesin consumer rules. They change the whole app's build. By default, starting with AGP 9.0, publishing a library fails if its consumer rules contain global options. - Targeted rules only: keep the specific classes or members you reflect on or call from native code. Jetpack Keep annotations can generate these rules for you.
- Test under R8 full mode, the default since AGP 8.0, using a sample app with minification on.
Option 2: Obfuscate the AAR itself with R8
If you distribute a closed-source SDK and want the published AAR to be renamed already, the Android Gradle plugin supports minifying a library module. Google labels this "advanced" and notes that build-time optimisation of a library is limited, but it does rename internal code:
android {
buildTypes {
release {
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
configureEach {
consumerProguardFiles("consumer-rules.pro")
}
}
}Two rule files now do different jobs:
|
File |
Applied when |
Must contain |
|---|---|---|
|
|
Building the AAR |
At minimum your entire public API, otherwise it is renamed or removed and customers cannot call it |
|
|
Building each app that uses the AAR |
Targeted keeps for reflection and JNI entry points |
A typical library build rule set keeps the public API and lets everything else be renamed:
# proguard-rules.pro (library build)
-keep public class com.example.sdk.api.** { public protected *; }
-keepattributes Signature, InnerClasses, EnclosingMethod, Exceptions, *Annotation*
-repackageclasses 'com.example.sdk.internal'Put internal classes in a separate package from the public API so a single keep rule covers the API, and, if you repackage, use a sub-package of your own library as shown, never a bare name like internal. Keep the attributes your customers need, such as Signature for generics, in the library build rules rather than the consumer rules.
One trap from Google's guidance: do not tell app developers to keep your whole package because "the AAR is already obfuscated". That disables the much stronger app-level optimisation. Ship targeted consumer rules either way.
Option 3: Commercial protection beyond renaming
R8 only renames, shrinks and optimises. It does not encrypt strings, hide class names in resources, protect native code or detect tampering. If your SDK contains licence checks, proprietary algorithms or anti-fraud logic, a commercial protector adds those layers. Guardsquare's DexGuard, for example, can protect libraries as well as apps, adding string and class encryption and runtime checks. Our roundup of Android obfuscation tools compares what each tool protects.
Use these layers for legitimate protection of your own intellectual property. Obfuscation used to hide what an app really does from store review violates Google Play's Developer Program Policies, and apps that ship such code can be rejected or removed.
How to check that your AAR is obfuscated
- Unzip the AAR and open
classes.jarin a decompiler such as jadx, which also opens.aarfiles directly. Internal classes should appear as short names likea,b,cunder your repackaged package; public API names should be intact. - Read the mapping file. The library build writes
mapping.txtunderbuild/outputs/mapping/release/. Archive it per release so you can deobfuscate crash reports from customers with theretracetool. - Check
proguard.txtinside the AAR. It should contain only your targeted consumer rules, with no global options. - Build a sample app with R8 full mode and run your integration tests. Crashes with
ClassNotFoundExceptionorNoSuchMethodExceptionpoint to a missing targeted keep rule, not to a need for a package-wide one.
Common problems
|
Symptom |
Cause |
Fix |
|---|---|---|
|
Customers cannot see your API classes |
Public API not kept in the library build rules |
Add |
|
App crashes only in release builds |
Reflection or JNI target renamed by the app's R8 |
Add a targeted consumer rule for that class or member |
|
Generics show as raw types in the IDE |
|
Keep |
|
AGP 9 refuses to publish the library |
Global option in consumer rules |
Move options like |
|
Crash stack traces are unreadable |
No mapping file kept for that version |
Archive |
For the difference between renaming and simple minification, see code obfuscation vs minification, and for choosing a tool across platforms, our code obfuscator guide.
FAQ
What is the best AAR obfuscator?
R8, built into the Android Gradle plugin, is the standard choice and costs nothing. Use it through the app build, or with isMinifyEnabled on the library module for closed-source SDKs. Add a commercial tool such as DexGuard only when you need string encryption or runtime protection.
Can I obfuscate an Android library module with ProGuard or R8?
Yes. Set isMinifyEnabled = true in the library's release build type and list a rules file that keeps the public API. AGP runs R8 on the library and produces a renamed AAR plus a mapping file.
What is the difference between proguardFiles and consumerProguardFiles?
proguardFiles apply while building your library and must keep its public API. consumerProguardFiles are packaged inside the AAR and apply later, in every app that uses the library.
Does obfuscating my AAR stop reverse engineering?
No. Renaming makes code harder to read but not impossible to understand. It raises the effort needed; for sensitive logic, move it to a server or add commercial protection.
Should app developers keep my obfuscated library entirely?
No. Google advises against keeping a library package because it was already obfuscated. App-level R8 optimises the library far better, so ship targeted consumer rules and let the app's build process the rest.
The short version
For most Android libraries, the right "AAR obfuscator" is the consuming app's R8, fed by narrow consumer keep rules in consumerProguardFiles. If you ship a closed-source SDK, enable isMinifyEnabled on the library, keep the public API in proguard-rules.pro, repackage internals into your own sub-package and archive the mapping file. Reach for commercial tools only when you need more than renaming.

