Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android design/architecture: Create different Apps from same code base

I want to create two apps from same code base. They differ in color and some data coming from back end. Situation is like, Lets say I want to creat apps App1 and App2. Both have similar flow. Can I,

  1. Select a Header color Blue if it is App1 and Red for other?
  2. In code, if(running App == App1) { execute this part} else {other part}?
  3. If App is App1 select layout1.xml for activity else select layout2.xml?

I am not sure how to go about this. Any help would be great.

Thank you.

like image 886
Sagar Avatar asked Jul 12 '26 17:07

Sagar


2 Answers

If you are using Android Studio, what you are describing is product flavors. Quoting the documentation:

A product flavor defines a customized version of the application build by the project. A single project can have different flavors which change the generated application.

For the purposes of this answer, let's imagine that you have one project with two product flavors, flavorOne and flavorTwo. Your project would also have three sourcesets:

  • src/main/, where most of your stuff goes
  • src/flavorOne/, where stuff for one flavor goes
  • src/flavorTwo/, where stuff for the other flavor goes

With that in mind...

Select a Header color Blue if it is App1 and Red for other?

In src/main/res/values/colors.xml, define a color named header, and give it a blue value. In src/flavorTwo/res/values/colors.xml, define a color named header, and give it a red value. The rest of your app refers to the header color (e.g., @color/header, R.color.header). When you build flavorOne, the main color will be used. When you build flavorTwo, the flavorTwo value will override the main value.

If App is App1 select layout1.xml for activity else select layout2.xml?

In src/main/res/layout/thingy.xml, define your default layout implementation. In src/flavorTwo/res/layout/thingy.xml, define the layout you want to use for flavorTwo. Your Java code would refer to R.layout.thingy. When you build flavorOne, the main layout will be used. When you build flavorTwo, the flavorTwo layout will override the main layout.

In code, if(running App == App1) { execute this part} else {other part}?

This would depend a bit on how much code you have that varies.

If it is substantial, you can use the strategy pattern:

  • Have one implementation of StuffStrategy in src/flavorOne/java/

  • Have another implementation of StuffStrategy in src/flavorTwo/java/

  • In your src/main/java code, refer to StuffStrategy, and it will pull in the right implementation based upon the flavor that you are building

If the code variation is really trivial, you could use buildConfigField to provide a value for some new field (e.g., STUFF) to be added to the BuildConfig class, where that value would vary by flavor. This Stack Overflow answer shows the details.

like image 118
CommonsWare Avatar answered Jul 14 '26 08:07

CommonsWare


If you are not using Android Studio, then I will mention about another possibility. For the main functionality you can create a Library project as the base of your two applications (Check it as library project, and include it as library to both of your separated projects) and then extend activities, classes that you want to end up with different behaviours.

For example (simple one), If you have two Applications with different splash layout, let's say splash.xml, you can create this default one in Library project, and create a different one in layout folder with the same name in other project's resource directory. Second project will use the one in it's own layout folder.

I would recommend Android Studio and Gradle with flavors, as in @CommonsWare's answer, but if you are not using it, you can make it this way as well.

like image 25
osayilgan Avatar answered Jul 14 '26 07:07

osayilgan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!