2

The problem is how to build multiple APK with different package names from a single source tree, for installation side by side. Each installation might be an internal beta alongside the production release, or it might be different versions of the same app with different options, or it might be for A-B testing purposes. Selecting different source code options for the build is easy, but outputting different package names is not.

The package name is specified in the AndroidManifest.xml and then turns up in the R.java and as a namespace in the XML. According to some searching, it also could be a factor in dynamic loading of resources, intent naming, etc. It's not easy to find all the places where it needs to be renamed.

It seems the AAPT command has a --rename-manifest-package` switch which might do the job. There are various older questions on SO, but this is a recent option. This link shows how to do it with Ant, but we use Eclipse and make so that's not much help. And this recent question is Ant only.

So the question is: (a) is this a viable solution (b) can it be done with an Eclipse build (c) what will it miss, requiring awareness in the running app?

Community
  • 1
  • 1
david.pfx
  • 10,520
  • 3
  • 30
  • 63

1 Answers1

2

Selecting different source code options for the build is easy, but outputting different package names is not.

It is if you start using Gradle for Android for your builds.

Each installation might be an internal beta alongside the production release

This maps to the Gradle for Android concept of "build types".

or it might be different versions of the same app with different options

This maps to the Gradle for Android concept of "product flavors".

or it might be for A-B testing purposes

I think that the conventional approach here isn't to have separate APKs, but rather a single APK that determines whether it is A or B based upon other criteria and behaves accordingly.

The package name is specified in the AndroidManifest.xml and then turns up in the R.java and as a namespace in the XML. According to some searching, it also could be a factor in dynamic loading of resources, intent naming, etc. It's not easy to find all the places where it needs to be renamed.

Gradle for Android helps split the role of the package name into:

  • the application ID, used to determine whether this app and another app can be installed at the same time on the same device

  • the package name used for R.java generation

Build types allow you to add a suffix to the package name for the purposes of the application ID role. Product flavors allow you to replace the package name for the purposes of the application ID role. Neither affects the package name as is used for your resources.

(a) is this a viable solution

If I had to guess, that switch is what Gradle for Android uses for the aforementioned application ID change, and so it should be viable for use elsewhere.

(b) can it be done with an Eclipse build

I am not aware that you can alter switches for those sorts of things in Eclipse. Gradle for Android would also not run from inside Eclipse either, at least not at the moment. But what you are seeking does not make much sense for an IDE, anyway, IMHO. Use Ant directly, Gradle for Android directly, or a continuous integration server that uses Ant or Gradle for Android.

(c) what will it miss, requiring awareness in the running app?

If by "it" you mean the renamed application ID, it should have no effect on most of your code. getPackageName() should return the application ID, though, so things that depend upon it (e.g., Maps V2 API keys) will need to take the various possible values into account.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • android studio seems amazing I better switch from ecllipse – Illegal Argument Jun 14 '14 at 14:31
  • 1
    @IllegalArgument: Note that while Gradle for Android is used by Android Studio by default, you do not need Android Studio to use Gradle for Android. For example, I do most of my Android development right now in Eclipse, but I also use Gradle for Android for preparing AAR files from my CWAC libraries, to make it easier for Gradle for Android/Android Studio developers to use those libraries. I also have my Gradle build scripts creating my JARs for those libraries that can be packaged that way. – CommonsWare Jun 14 '14 at 14:34
  • Thanks for the lengthy answer. However (a) switching build platform is a major investment (b) we really do need it to play well with Eclipse (c) I would prefer to resolve this particular issue in isolation, and choose a build platform (or a different IDE) on other grounds. – david.pfx Jun 14 '14 at 14:35
  • Thats all great, but it doesn't actually answer the question. – Brill Pappin Oct 27 '15 at 13:34