1

Currently, I have an app that contains three features, which is published on the store.

I would like to create a separate app for each feature, but I don't want to copy and paste, since I will then have to maintain multiple codebases.

I have read about Product Flavors, which can be used to create different applications from the same codebase. How do I go about creating four separate apps, while maintaining only one codebase?

Specifically: where do I place the source of the main app that contains all the features?

Can anyone suggest how I can organise my code?

AesSedai101
  • 1,502
  • 2
  • 23
  • 37

2 Answers2

1

You can create product flavours in gradle file as mentioned in above link you shared moreover you need to create different package name while creating those flavours e.g.

In gradle

    productFlavors {

            demo{
                applicationId "com.mycompany.myproject.demo"
                versionCode getCustomBuildVersion()
                versionName VERSION_NAME
            }
            full{
                applicationId "com.mycompany.myproject.full"
                versionCode getCustomBuildVersion()
                versionName VERSION_NAME
            }
}

In this way you can generate different apps from same code base. You can use and set flags for enabling or disabling certain features. check out this link for further help: Android using Gradle Build flavors in the code like an if case

In activities you want to change do something like this in onCreate():

if (BuildConfig.FLAVOR.equals("demo")){
switchToDemoHomePage()
}
else if(BuildConfig.FLAVOR.equals("full")){
switchToFullHomePage()
}

In this way you can show/hide/change any feature/options/flows based on flavour of the app

Sara Tirmizi
  • 417
  • 4
  • 10
  • i know the basics of it , but i am not understanding where do i keep my MAIN app which contains all three Features ,how do i organise it,because if i make separate code base(flavor) for each feature than what should i do with MAIN app which needs to access all three features – Kulwinder Singh Rahal Nov 16 '17 at 11:10
  • Since I am assuming your features must be scattered throughout the app therefore one neat solution is create flags in a class where your constants are available, and create small methods dividing these features I am writing a pseudo code here, `boolean enableFeature1 = false` likewise create these booleans and using build flavour check you can mark them true and lets suppose if app is full feature mark all true and set your app's feature visibility accordingly.. I hope you are getting my point – Sara Tirmizi Nov 16 '17 at 11:17
  • actually i have Astrology app which contains features like -Match making, Astrology,Horoscope etc each have seprate Android Activity but i also have accounts in app ,so App user can view Horoscope etc according to his account and create new – Kulwinder Singh Rahal Nov 16 '17 at 11:22
  • Scenario 1: You need single app for multiple purpose then you need to do role based development like upon login you can update the role on app level and based on user rules you can allow or restrict the features within app for example `if (UserRole.isMatchMaking()){ showFeature()} else if(UserRole.isAstrlogy()){ showThisFeature() else{ showThatFeature()} }`. **You can also decide which activity to open after login based on role** Scenario 2: you want separate apps for these type of users then follow my above comment – Sara Tirmizi Nov 16 '17 at 11:29
  • but in 1 Scenario i think you are saying to use Main App and check run time for feature to use , but in my case i want to use different apps and upload them to play store by specific name and applicationId so user can have all apps – Kulwinder Singh Rahal Nov 16 '17 at 11:36
  • okay If you want to achieve this then follow my comment from that you can create different applicationIds to upload on playstore and in code you can always hide any feature or open different activity based on Build flavour throughout app like it is being done on role based. – Sara Tirmizi Nov 16 '17 at 11:41
  • can you please explain this in more detail ,it looks like it will work ,but I'm confused how to use it , where to check for flag , please update your answer if you like to ? – Kulwinder Singh Rahal Nov 16 '17 at 11:43
  • thanks , your answer really helped me, but how can I reduce extra code that is coming of different flavour that is not used ? – Kulwinder Singh Rahal Nov 20 '17 at 01:11
  • it is working ,but how can i reducd extras of other features like resources images ,which are not useful but increases my app size – Kulwinder Singh Rahal Nov 22 '17 at 03:59
1

You may want to define compiler switch in product flavor and code base in MAIN and access using BuildConfig.feature1 like

productFlavors {

            flavor1{
                 buildConfigField "Boolean", "feature1", true;
                 buildConfigField "Boolean", "feature2", false;

                }
          flavor2{
                 buildConfigField "Boolean", "feature1", false;
                 buildConfigField "Boolean", "feature2", true;

                }
           main{
                 buildConfigField "Boolean", "feature1", true;
                 buildConfigField "Boolean", "feature2", true;

                }
}
Mahesh
  • 1,257
  • 1
  • 14
  • 24
  • i didn't understand properly but what i understanding is that i have to check `BuildConfig` and show appropriate Activity ? – Kulwinder Singh Rahal Nov 16 '17 at 11:27
  • can you explain more how to use it – Kulwinder Singh Rahal Nov 16 '17 at 11:37
  • declare flags for your feaures in each flavor and in activity declare two methods for each like{ showMatchmaking() and hideMatchMaking()}. and in oncreate check for flag status like { if(BuildConfig.matchmaking) showMatchmaking() else hideMatchMaking()}. you can have different applicationId for different flavor as well – Mahesh Nov 16 '17 at 12:19
  • but how can i reducd extras of other features like resources images ,which are not useful but increases my app size – Kulwinder Singh Rahal Nov 22 '17 at 03:54
  • put resource in respective flavor/res folder . this way only selected files gets added in build – Mahesh Nov 23 '17 at 09:37
  • i have done this, after doing getting https://stackoverflow.com/questions/47427485/why-android-suorceset-res-excludes-not-working-as-expected# , duplicate resources error – Kulwinder Singh Rahal Nov 23 '17 at 15:24