Search code examples
androidandroid-productflavorsandroid-build-flavors

how to call different method for different product-flavor from common java class


I need something like this:

from any method:

if(for one flavor)

   do something

else

   do something

How can I handle flavors from java?


Solution

  • You can specify files for flavors. If I were you I would do something like this: put a file in the main/java/your/package source, lets call it CustomClass.java. It contains something like this:

    public class CustomClass {
       public static void myCustomMethod() {
          //Do something here...
       }
    }
    

    After that you can create a flavor specific version of this class in the flavor name folder (next to the main folder). So put a CustomClass.java in here: your_flavor_name/java/your/package And do something else:

      public class CustomClass {
           public static void myCustomMethod() {
              //Do something here for that specific flavor!
           }
        }
    

    The build will merge the files and use the specific ones for you flavor, and use the main version if no flavor specific file found. You can use it for drawables, and every other file (assets, ...).

    You only have to create a new flavor after this. In your gradle add this:

    productFlavors {
            your_flavor_name {
                applicationId "your.package"
            }
    }
    

    More stuff here: http://developer.android.com/tools/building/configuring-gradle.html