Excluded Packages
Excluded packages tell Shield which packages to skip during protection. This prevents issues with framework classes, Kotlin runtime internals, and third-party libraries that are not designed to be obfuscated.
Default Exclusions
At minimum, always exclude the Android and Kotlin runtime packages:
shield {
excludedPackages = [
'android',
'androidx',
'kotlin',
'kotlinx'
]
}shield {
excludedPackages = [
'android',
'androidx',
'kotlin',
'kotlinx'
]
}These packages contain framework code that must retain its original structure for the Android runtime to function correctly.
Why Exclude Packages
Shield transforms bytecode: it renames identifiers, encrypts strings, and restructures control flow. This can break code that relies on:
- Reflection — Frameworks that look up classes or methods by name (Gson, Retrofit, Room)
- Serialization — Libraries that map JSON keys to field names (Moshi, Jackson)
- Annotation processing — Code generated by annotation processors (Dagger, Hilt)
- Native interop — JNI methods that must match specific signatures
Excluding a package tells Shield to process it as-is, without applying any transformations.
Common Exclusions
| Package | Why Exclude |
|---|---|
android | Android framework classes |
androidx | AndroidX support libraries |
kotlin | Kotlin standard library |
kotlinx | Kotlin extensions (coroutines, serialization) |
com.google | Google Play Services, Firebase, Gson |
com.squareup | OkHttp, Retrofit, Moshi |
dagger | Dagger/Hilt dependency injection |
com.fasterxml | Jackson JSON library |
org.slf4j | SLF4J logging |
Example with Common Libraries
shield {
excludedPackages = [
'android',
'androidx',
'kotlin',
'kotlinx',
'com.google',
'com.squareup',
'dagger',
'javax.inject'
]
}shield {
excludedPackages = [
'android',
'androidx',
'kotlin',
'kotlinx',
'com.google',
'com.squareup',
'dagger',
'javax.inject'
]
}Excluding vs Annotations
Package exclusions are broad — they skip entire package trees. For fine-grained control over individual classes or methods, use annotations instead:
| Approach | Scope | Use Case |
|---|---|---|
excludedPackages | Entire package tree | Third-party libraries, framework code |
@Keep | Class, method, field, constructor | Individual elements that use reflection |
@DoNotObfuscate | Class, method, field, constructor | Elements that must keep their name |
@Exclude(protections={...}) | Class, method, field, constructor | Exclude from specific protections only |
Using excludePackages Method
In addition to setting the excludedPackages list, you can use the excludePackages method in variant configurations:
shield {
excludedPackages = ['android', 'androidx', 'kotlin', 'kotlinx']
all {
excludePackages('com.google', 'com.squareup')
}
}shield {
excludedPackages = ['android', 'androidx', 'kotlin', 'kotlinx']
all {
excludePackages('com.google', 'com.squareup')
}
}Related
- Annotations — Fine-grained exclusion with @Keep, @DoNotObfuscate, @Exclude
- Gradle DSL Reference — Complete configuration reference
- Troubleshooting — Diagnosing issues caused by obfuscation