I have a project and it has 2 product flavors with their own directories:
build.gradle:
android {
...
productFlavors {
free {
applicationId "com.sample.free"
buildConfigField "boolean", "free", "true"
}
paid {
applicationId "com.sample"
buildConfigField "boolean", "free", "false"
}
}
}
And I have a class (such as PaidOperationClass
) which is only used at paid
flavor. So, I put that class under src/paid/java
directory. And I have another class(such as CommonClass
) which is used by both flavors. So I put that under src/main/java
directory:
src/main/ --> has CommonClass
src/free/
src/paid/ --> has PaidOperationClass
In CommonClass
I have a method such as:
if(!BuildConfig.FREE) {
PaidOperationClass.doSomeStuff();
}
So the PaidOperationClass
is referenced(has an import) but never used by free build.
If I build the application for paid
flavor everything works perfect. But if I try to build it for free
flavor, it fails because referenced but unnecessary class is not found. How do you solve this without code (class/method) replication (such as putting a dummy PaidOperationClass
under free
flavor)? Are there any gradle options which ignores this kind of build errors while building?
Edit: I need a gradle solution which won't need code replication. An annotation based custom script maybe, which removes unnecessary code for product flavors at compile time.
123 let's go Simply put, a product flavor is a variant of your app. It is very useful when you want to create multiple versions of your app. This means you can generate different versions or variants of your app using a single codebase.
There is no "current flavor". The Gradle build file is building an object model of the build process. It is not an interpreted script only being used once a build is underway and a "current flavor" is known.
Android Product Flavors are also known as Android build types or Android build variants are the native Android app development way to implement different versions of the same application with minor changes.
A flavorDimension is something like a flavor category and every combination of a flavor from each dimension will produce a variant. In your case, you must define one flavorDimension named "type" and another dimension named "organization".
You can't do it, because the import statement is inside the CommonClass
and it is not able to resolve PaidOperationClass
in the free flavor.
One way to achieve it is:
Create an empty class in Free flavor:
public class PaidOperationClass{
public static void doSomeStuff(){
//do nothing..
}
}
The easiest solution might be to create a class: IPaidOperations that is housed in the /src/main directory that the PaidOperationClass implements. Then just use the interface in the CommonClass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With