elifekiz
elifekiz

Reputation: 1506

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?

Upvotes: 2

Views: 2811

Answers (4)

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

Create class in something() in productFlavor :

Main Common source :

main\java\com.exmaple.test\class.java - > something()

Flavor1 source :

flavor\java\com.exmaple.test\class.java - > something()

Flavor2 source :

flavor\java\com.exmaple.test\class.java - > something()

Upvotes: 0

inkedTechie
inkedTechie

Reputation: 684

define two string.xml for the two product-flavors separately and add a string of common name in each, for example,add <string name="flavor">Flavor One</string>in the string.xml of 1st flavor and add <string name="flavor">Flavor Two</string>in the string.xml of 2nd flavor. Now in the method, get the value of string flavor and add lines of code accordingly. Because, for the two flavors, the string flavor will have two different values.

Upvotes: 0

elifekiz
elifekiz

Reputation: 1506

thank you for your answer @GaborNovak

In fact, I searched something like that for common class:

if("flavor-name".equals(getApplicationContext().getPackageName())){
                myIntent.setClass(thisClass.this, nextClass.class);
            }
            else {
                myIntent.setClass(thisClass.this, nextClass.class);
            }

I think it is a bit confusing. When I write getApplicationContext.getpackageName -> it gets defined application_id of selected flavor in build gradle. When I write BuildConfig.APPLICATION_ID -> it gets package name. Is there anybody who can explain behind of that logic? Isn't it confusing?

Upvotes: 0

Gabor Novak
Gabor Novak

Reputation: 286

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

Upvotes: 1

Related Questions