Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build multiple (test/prod) versions of Android APKs in Eclipse

I'm looking to optimize generating of slightly different APKs of the same Android app, the only difference being the http API server it's using (dev/staging/prod).

Ideally, I'd just want my Eclipse to build 2 APKs, one with the prod server and one with the dev one.

I'm even OK with having 2 Run configurations, but I haven't been able to figure out how to pass parameters to the app and read them from the code.

I want to target 1.5, BTW, and I'd like to use Eclipse auto-build tools, so I'm looking for the most generic solution.

Thank you.

like image 905
Artem Russakovskii Avatar asked Oct 21 '10 00:10

Artem Russakovskii


3 Answers

I think using ant build script would be the easiest solution. Eclipse supports ant build, so you can run ant command in eclipse.

You can solve your problem with ant like this.

  1. prepare two xml android resource file.
  2. build a package with resource #1
  3. overwrite resource #1 with content of resource #2
  4. build another package

xml would be like this:

resource #1:

<resources>
    <string name="target">dev</string>
</resources>

resource #2:

<resources>
    <string name="target">staging</string>
</resources>

and ant script would be like this:

<project>
  <target name="build_all">
     <copy file="res1.xml" to="res/values/target.xml"/>
     <ant antfile="build.xml" target="debug"/>
     <copy file="res2.xml" to="res/values/target.xml"/>
     <ant antfile="build.xml" target="debug"/>
  </target>
</project>
like image 92
kingori Avatar answered Oct 17 '22 01:10

kingori


Move all you code to a library project see http://developer.android.com/guide/developing/projects/projects-eclipse.html#SettingUpLibraryProject

Then create separate projects in eclipse for test and production each with a unique package name. You can then use the package name to distinguish between versions.

Something like:

public static boolean isProductionVersion(){
  return context.getPackageName().toLowerCase().contains("production");
}

This may seem like overkill for managing different http end points but it will make the code more manageable. You can also do useful things like:

  • flag the test version with a different application icon
  • run test and production versions side by side on one device

This can all be done in eclipse without using and third party tools.

like image 44
railwayparade Avatar answered Oct 17 '22 03:10

railwayparade


Its not really what you want:

private static Boolean isSignedWithDebugKey = null;
    protected boolean signedWithDebug() {
        if(isSignedWithDebugKey == null) {
            PackageManager pm = getPackageManager();
            try {
                PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
                isSignedWithDebugKey = (pi.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
            }
            catch(NameNotFoundException nnfe) {
                nnfe.printStackTrace();
                isSignedWithDebugKey = false;
            }
        }

        return isSignedWithDebugKey;
    }

You could then hit a dev/staging server if the app is signed with a debug key, and production with a release certificate.

like image 1
FunkTheMonk Avatar answered Oct 17 '22 01:10

FunkTheMonk