Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio build flavors - How to have same source files in diverse flavors

Tags:

I need to create a demo flavor in android studio for an app. In my app level gradle file i have created another flavor called demo and the default flavor of full of course. It looks like this:

apply plugin: 'com.android.application'  android {     compileSdkVersion 22     buildToolsVersion "21.1.2"      defaultConfig {         applicationId "com.example.uen229.myapplication"         minSdkVersion 17         targetSdkVersion 22         versionCode 1         versionName "1.0"     }     buildTypes {         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'         }     }      productFlavors {         demo {             applicationId "com.buildsystemexample.app.demo"             versionName "1.0-demo"         }         full {             applicationId "com.buildsystemexample.app.full"             versionName "1.0-full"         }     }  }  dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     compile 'com.android.support:appcompat-v7:22.2.0' } 

and here is a image of my project structure in which I have created a demo flavor directory:

enter image description here

Now onto the issue. I have two classes called Hello.java. Both are in there respective flavors and print different things. I'll show you both files now:

import android.util.Log;  /** this is from demo flavor directory**/  public class Hello {      Hello(){          Log.v("","hello from demo");     }      public String getName();         return "im from demo";      };  } 

And here is the other Hello:

package com.example.uen229.myapplication;  import android.util.Log;   /** this is from full or main flavor directory**/ public class Hello {       Hello(){          Log.v("", "hello from main");     }      public String getName(){          return "im from main";      }; } 

notice how the first hello.java does not have package, even if i had a package the IDE wont compile. look at this photo:

enter image description here

Now finally lets look at mainActivity.java to see that when i switch build variants it only does a toast for "im from main" but i need it to print 'im from demo" if i use the demoDebug build variant. If i switch the build variant to demoDebug it still prints "im from main". can anyone help :

public class MainActivity extends ActionBarActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          Hello h = new Hello();         Toast.makeText(this, h.getName(), Toast.LENGTH_LONG).show();      } } 

UPDATE

From stackoverflow it says:

If you want to have a different version of the same class in the two flavor you'll need to create it in both flavors.

src/flavor1/java/com/foo/A.java  src/flavor2/java/com/foo/A.java 

And then your code in src/main/java can do:

import com.foo.A 

depending on the flavor selected, the right version of com.foo.A is used.

This is what I want to accomplish with the Hello class

like image 686
j2emanue Avatar asked Jun 24 '15 01:06

j2emanue


People also ask

How do you make different flavors on android?

Creating Product Flavor is pretty much easy in Android Studio, we simply need to add productFlavors block inside the Android block in the application-level build. gradle file. It's that simple to create product flavors.

How do I change a variant in build?

NOTE: By default, the Android Studio will generate "debug" and "release" Build Types for your project. So, to change a Build Type, all you need to do is just select your Build Type from the Build Variant and after the project sync, you are good to go.

When would you use product flavor in your build setup?

You use same core ingredients to make the base but will use different toppings for each one to have a different taste. Similarly, android apps can have the same base functionalities with changes to some of the features like styles, logo etc. This can be achieved using product flavours.

What is missingDimensionStrategy?

void missingDimensionStrategy ( String dimension, String requestedValue) Specifies a flavor that the plugin should try to use from a given dimension in a dependency. Android plugin 3.0. 0 and higher try to match each variant of your module with the same one from its dependencies.


2 Answers

I think you can't have same class in main flavor and your other flavor. you should just create another flavor, then move your Hello class from main flavor to that new flavor. this rule is just for .java files. I mean you can have an xml file in main flavor and another version in your custom flavor but you can't do this with java files.

here is a useful link with further explanation.

like image 193
Mohammad Rahchamani Avatar answered Oct 19 '22 14:10

Mohammad Rahchamani


I would advice to create 3 source sets:

  • main - which would contain common classes
  • demo - contains demo specific classes
  • pro - contains classes for pro versions

and declare them using:

sourceSets {     main {         manifest.srcFile 'AndroidManifest.xml'         java.srcDirs = ['src/main/java']         res.srcDirs = ['src/main/res']         assets.srcDirs = ['src/main/assets']     }     pro {         manifest.srcFile 'src/pro/AndroidManifestPro.xml'         java.srcDirs = ['src/main/java', 'src/pro/java']     }     demo {         manifest.srcFile 'src/oldlite/AndroidManifestDemo.xml'         java.srcDirs = ['src/main/java', 'src/demo/java']             } } 

P.S. not really sure about syntax of java.srcDirs content - please double check yourself

like image 35
Barmaley Avatar answered Oct 19 '22 12:10

Barmaley