I have four build variants, A, B, C and D. For a specific activity I have common code in a base class in the main source sets. I want to additional functionality such that A & B have the same additionally functionality and C & D have the same functionality which is different from A & B. I have been able to do this by copy and pasting the extending class in the source sets for A & B and C & D respectively; however, that makes maintenance an issue. I cannot merge variants A & B or C & D because they do have other important differences. How do I include the class such that it is only in A and B references it and only C and D references it. I tried adding java source sets but then it couldn't find any other classes or other modules.
Asked
Active
Viewed 734 times
2 Answers
0
I was able to solve this issue by creating two new directories E & F. Then I added source sets for A & B to be their current source sets and E and for C & D I added F.
sourceSets {
A {
java.srcDirs = ['src/A/java', 'src/public/E']
}
B {
java.srcDirs = ['src/B/java', 'src/public/E']
}
C {
java.srcDirs = ['src/C/java', 'src/internal/F']
}
D {
java.srcDirs = ['src/D/java', 'src/internal/F']
}
}

Zachary Sweigart
- 1,051
- 9
- 23
0
You should checkout this: https://developer.android.com/studio/build/build-variants#resolve_matching_errors
// In the app's build.gradle file.
android {
flavorDimensions 'tier'
productFlavors {
paid {
dimension 'tier'
// Because the dependency already includes a "paid" flavor in its
// "tier" dimension, you don't need to provide a list of fallbacks
// for the "paid" flavor.
}
free {
dimension 'tier'
// Specifies a sorted list of fallback flavors that the plugin
// should try to use when a dependency's matching dimension does
// not include a "free" flavor. You may specify as many
// fallbacks as you like, and the plugin selects the first flavor
// that's available in the dependency's "tier" dimension.
matchingFallbacks = ['demo', 'trial']
}
}
}
and more documentation at https://developer.android.com/reference/tools/gradle-api/7.0/com/android/build/api/dsl/ProductFlavor#matchingfallbacks

AA_PV
- 1,339
- 1
- 16
- 24