16

I had developed 3 applications in android where the major functionalities are the same but the UI looks different. Images and the background color of the screens are different.

NOw, i want to create a single code base from which i can generate multiple .apk files for the 3 apps.

I tried creating 3 different packages for src folder for the 3 apps. But i dont know how to set the res folder for these apps.

Need pointers on creating a single code base from which we can generate multiple .apk files which includes only the respective src and res folders.

Prem
  • 4,823
  • 4
  • 31
  • 63
  • 1
    Why not create a single apk that works on all three types of devices? – Michell Bak Nov 15 '11 at 12:47
  • It is not possible because each of the 3 applications have different images. – Prem Nov 15 '11 at 12:52
  • Yeah - it is possible. I'm guessing the images are based on the size of the devices, right? – Michell Bak Nov 15 '11 at 12:53
  • Check this question http://stackoverflow.com/questions/7552033/android-build-configurations-for-multiple-customers – peceps Nov 15 '11 at 12:59
  • No sorry. Let me explain. Application 1 - Home page has welcome text - "welcome to first application" with image 123 and button. On click of the button a toast appears with text "Success". Application 2 - Home page has welcome text - "welcome to second application" with image abc and button. On click of the button a toast appears with text "Success". Application 3 - Home page has welcome text - "welcome to third application" with image xyz and button. On click of the button a toast appears with text "Success". With the same code base 3 apks for these 3 differnt apps should be generated. – Prem Nov 15 '11 at 13:01
  • @peceps - Thanks. My question is also the same. But i have no idea of using Mavens. Is there any other way of accomplishing the same task ? – Prem Nov 15 '11 at 13:08
  • I wrote a [tutorial](http://www.devgems.net/?p=547) how to do that in a clean way with Maven. – Bevor Apr 15 '13 at 07:55

5 Answers5

9
  • Use an Android Library Project that contains all your common code.
  • Create separate Android projects that reference the Library Project (you will need to copy your Manifest into each of these and make sure all components are declared with their full Java package name).
  • Put any resources specific to each app (drawables, colors etc) into the individual project resource folders and they will override similarly named resources in the library project at build time.
Jeff Gilfelt
  • 26,131
  • 7
  • 48
  • 47
  • Hi, When the application is installed, will it be creating 3 different applications – DAS Jun 14 '12 at 11:07
  • Hi Das, When the application is installed we will have only one application. Reason is ... We first place all the common code in library project. In other projects, we refer to the code. So, we have 3 projects in eclipse which corresponds to 3 apk files. – Prem Jun 29 '12 at 04:05
  • "The requested URL was not found on this server" on link :( – pRaNaY Jun 15 '16 at 12:23
1

i think the best option is to use ant, you'll need to add an ant target for each build and change the resource folder. if you use the generated build.xml, the res folder is defined like this <property name="resource.absolute.dir" location="res" /> so you'll want to override that

tbg
  • 495
  • 1
  • 4
  • 6
  • Thanks But i want to create output apk files and not war files. Is this possible using the ant option ? – Prem Nov 15 '11 at 13:14
  • 1
    with ant you can do a lot more than just create war files :) Android support for ant is really good, you generate the build.xml file with one command. Read this for more details: http://developer.android.com/guide/developing/building/building-cmdline.html – tbg Nov 15 '11 at 13:47
  • that's not a good idea. you want to use a library project and resource files there. – meredrica Feb 27 '13 at 14:41
1

Can't you put all of your common code into a library project and then just reference that project from each of the 3 unique projects that each contain the relevant resources.

Update: This answer is now obsolete when using the Gradle build system.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
0

I would suggest using Gradle flavors.

It seems to explain all the basics really well. I just finished converting to Gradle today, and it works great. Custom app icons, names, and strings, etc.

As the website explains, part of the purpose behind this design was to make it more dynamic and more easily allow multiple APKs to be created with essentially the same code, which sounds similar what you're doing.

Also see a recent question I had, referring to your project structure and using custom code for each app.

Community
  • 1
  • 1
craned
  • 2,991
  • 2
  • 34
  • 38
0

Why don't you use a single application, that does three different things based on SharedPreferences values set by the user, or from context at install time. If you really want to separate, you can have three different activities, and you decide which one to launch from a silent main Activity that redirects to either of the different ones.

An alternative is to have a unique activity that inflates itself dynamically from 3 different layouts at onCreate time.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (...custom check for layout... equals(layout1)) {
        setContentView(R.layout.main_layout1);
    } else if (... equals(layout2)) {
        setContentView(R.layout.main_layout2);
    } else if (... equals(layout3)) {
        setContentView(R.layout.main_layout3);
    } else {
        throw new IllegalStateException("Unknown layout!");
    }
    ... your onCreate stuff....
}

It will make code maintenance easier (only one code source to modify, only one version-list and changeset to maintain)

Check here: How to use SharedPreferences in Android to store, fetch and edit values

Community
  • 1
  • 1
Guillaume
  • 22,694
  • 14
  • 56
  • 70