Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve build error for product flavor, when a referenced but unnecessary source/resource not found?

Tags:

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.

like image 564
Devrim Avatar asked Dec 11 '15 14:12

Devrim


People also ask

When would you use a product flavor in your build setup?

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.

How do I get the current flavor in Gradle?

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.

What is build flavor in Android?

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.

What is Flavordimensions?

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".


2 Answers

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..    
  }
}
like image 61
Gabriele Mariotti Avatar answered Oct 01 '22 01:10

Gabriele Mariotti


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.

like image 28
Kaediil Avatar answered Oct 01 '22 00:10

Kaediil