2

This post explain how to include different dependencies in different productFlavors of the project.

I did that, but now how can I handle my own code calling code from a jar not included in one flavor of my project ?

example

admob.jar have a Aaa.class and is only included in the free flavor. When I compile pro flavor, it fails because Aaa.class is not in the classpath.

I don't wan't use reflection (it's inefficient and time-consuming).

Any suggestion?

Community
  • 1
  • 1
Henry Gu
  • 51
  • 5

1 Answers1

1

As mentionned in the linked post, you can use flavors to include different dependencies, BUT you can also include specific src files in a flavor:

Create directories src/pro/java/ and src/free/java/ .

All source files in src/pro/java will be compiled only in the "pro" flavor using the dependencies defined for the "pro" flavor.

All source files in src/free/java will be compiled only in the "free" flavor using the dependencies defined for the "free" flavor.

Now assume that you have a java source file SomeClass.java calling some code in admob.jar. You can the the following :

  1. put SomeClass.java in src/free/java/SomeClass.java
  2. duplicate SomeClass.java in src/pro/java/SomeClass.java
  3. remove all calls to admob.jar from src/pro/java/SomeClass.java
  4. if you did it properly : both flavors of your project should compile

Tip: try to organize your code in such a way that duplicated class are reduced to the strict minimum.

Community
  • 1
  • 1
ben75
  • 29,217
  • 10
  • 88
  • 134
  • thanks for you answer, it is a solution, but it make code structure seems disorderly, I am reading grandle use guide, find if have any machine like annotation, pass the class compiled. – Henry Gu May 29 '15 at 02:13
  • sourceSets { main { java { exclude 'some/unwanted/package/**' } } } we can exclude packages and files which we donot need, then can compiled. – Henry Gu May 29 '15 at 04:57