Android – multiple custom versions of the same app

Whats the best way to deploy several customized versions of a Android application?

Currently I have a script to exchange the resource folder for getting a customized version of my app. It works great, but all custom versions still have the same package name in the AndroidManifest.xml. Therefore it is not possible to install two customized versions of the app at the same time.

This is one solution for this problem, but that has to be done by hand

Can you think of a more easy solution, or how this could be built into a skript?

(btw: it is not for a porn/spam/whatever app, not even a paid one)


Solution 1:

Perhaps the built-in Android "library" concept was not fully baked at the time of the original post, but it may be the preferred method as of 2011. Follow these steps for an ant build:

Starting from a working app (let's call it directory "myOrigApp", package com.foo.myapp), just add this line to "default.properties" to make it a library:

android.library=true

Now create a new app in a sibling directory in any way you prefer (let's call it directory "sibling", package com.foo.myVariant). Using Intellij Idea, for example, create a project 'from scratch' with directory 'sibling' and it will create all the files/directories you would normally need.

In that new, sibling directory edit "default.properties" to add the dependency:

android.library.reference.1=../myOrigApp

Copy over the Manifest from the original dir:

cd sibling
cp ../myOrigApp/AndroidManifest.xml  ../myOrigApp/local.properties ../myOrigApp/build.properties  .

Edit that copied Manifest file to change its package name to your new variant, "com.foo.myVarient"; that's the only change.

If you just run the ant build scripts, you may be done. (I had to just set up signing keys.)

If you want to set up an IDE like Idea to have the library project as a dependent of the variant project, follow these steps to add a library project to the variant project (assumes you already have a project set up for both):

  • Open the original project, bring up Project Settings, select your Facet and check "Is Library Project" and save.
  • Open the variant project, bring up Project Settings, select Modules
  • Add a module
  • Select “Import existing module”
  • Browse to the Original directory (myOrigApp) and select its .iml file (IntelliJ project source file)
  • Click "Finish." (The library project is added as a module within the variant project.)
  • In the modules list click over the Variant project to select it.
  • On the right hand side select the "Dependencies" tab.
  • Click "Add…"
  • Choose "Module dependency…" (A list should appear that includes the name of the module/library you previously added to the project--perhaps the only entry in the list).
  • Select the library project you added and press OK. (It will be added to the list of dependencies of your project.)
  • Press OK to finish configuring the project. (You should see 2 modules, with the library's resources and classes available and recognized in the Variant project.)

Solution 2:

What I did for something similar to this is to just use an antlib task and then go through all java and xml files to replace my old package string to the new package string. It didn't matter if the files were not in the correct src paths according to the package. Just doing a regex replace for all the files was enough for me to get this working...

For example to replace it in all your java files under the src directory:

 <replaceregexp flags="g" byline="false">
    <regexp pattern="old.package.string" /> 
    <substitution expression="new.package.string" />
    <fileset dir="src" includes="**/*.java" /> 
 </replaceregexp>

Solution 3:

You definitely want to use Gradle flavors that comes natively, encouraged even, on Android Studio.

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.

I probably didn't explain it the best, but that website does a pretty good job.