-
Extract ALL shared code into a single library module
• Move your “real” Java/Kotlin sources (and any annotation‐processors) into a standalone Android or Java library (e.g. :common)
• Turn your :app (or :feature…) modules into essentially resource-only apps that just depend on :common
• Result: the code in :common is compiled exactly once. Each “flavor-app” only needs to merge-packaging resources and manifest.
Example setup:
settings.gradle
include ':common', ':appFlavorA', ':appFlavorB', ...
common/build.gradle
apply plugin: 'com.android.library'
android { ... all your code lives here ... }
appFlavorA/build.gradle
apply plugin: 'com.android.application'
android {
defaultConfig { applicationId "com.example.a" }
// no java/kotlin dirs here
sourceSets {
main {
java.srcDirs = [] // no code
res.srcDirs = ['src/main/res'] // flavor A resources
}
}
}
dependencies {
implementation project(':common')
}
// repeat for appFlavorB, etc.
▶ Building :common happens once, then each “thin” app just packages its resources.
▶ Almost always the biggest win for multi-flavor code-identical apps.
-
Turn on Gradle “configuration on demand” + parallel + build cache
Even without re-architecting, you can get better concurrency and caching:
• In gradle.properties (project or global ~/.gradle/gradle.properties):
org.gradle.configureondemand=true
org.gradle.parallel=true
org.gradle.caching=true
• Then invoke:
./gradlew clean assembleAllFlavors
• Gradle will configure only the modules it actually needs, run independent variant builds in parallel, and reuse cached outputs if you’ve built that flavor before.
-
Leverage the Android Gradle Plugin’s build cache
• AGP 7.x+ has built-in task output caching: make sure org.gradle.caching=true.
• Once you’ve built a given flavor once, a subsequent build of that same flavor (even in the same multi-flavor invocation) will fetch the compile/R-gen steps from cache.
-
If you really can’t split modules…
• There’s no magic flag to tell AGP “compile this Java code once and share across flavors” within one app module.
• Every flavor variant is considered a separate Gradle task graph with its own compile tasks.
• The only way to “not recompile” is to turn that common code into a shareable library (see #1).
-
Bonus: Variant filters & resource overlays
If your flavor differences are purely resource values (colors, strings, drawables), consider:
• A single “app” module + a tiny Resource-only language module via dynamic feature or Gradle resource overlays
• Or use resValue / buildConfigField + Gradle properties to tweak behavior at build time rather than whole-flavor res directories.