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


i need create demo flavor in android studio app. in app level gradle file have created flavor called demo , default flavor of full of course. looks 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 image of project structure in have created demo flavor directory:

enter image description here

now onto issue. have 2 classes called hello.java. both in there respective flavors , print different things. i'll show both files now:

import android.util.log;  /** demo flavor directory**/  public class hello {      hello(){          log.v("","hello demo");     }      public string getname();         return "im demo";      };  } 

and here other hello:

package com.example.uen229.myapplication;  import android.util.log;   /** full or main flavor directory**/ public class hello {       hello(){          log.v("", "hello main");     }      public string getname(){          return "im main";      }; } 

notice how first hello.java not have package, if had package ide wont compile. @ photo:

enter image description here

now lets @ mainactivity.java see when switch build variants toast "im main" need print 'im demo" if use demodebug build variant. if switch build variant demodebug still prints "im main". can :

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 says:

if want have different version of same class in 2 flavor you'll need create in both flavors.

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

and code in src/main/java can do:

import com.foo.a 

depending on flavor selected, right version of com.foo.a used.

this want accomplish hello class

i think can't have same class in main flavor , other flavor. should create flavor, move hello class main flavor new flavor. rule .java files. mean can have xml file in main flavor , version in custom flavor can't java files.

here useful link further explanation.


Comments

Popular posts from this blog

c# - Where does the .ToList() go in LINQ query result -

Listeners to visualise results of load test in JMeter -