Setting Minimum SDK Version to Gingerbread (SOLVED)

Overview

Corona apps are, by default, Android 2.2+ compatible. We had a need to make our Android APK 2.3+ compatible. Basically a thing wasn’t working and instead of risking bad reviews for, or spending time to support, a version that’s barely used anymore we just didn’t want to support anything lower than 2.3.

It seems like Corona itself could offer a build option for the supported versions. In fact, it actually has a “Minimum SDK Version” option in the Android Build Setup dialog when building from the simulator, but this option seems to be hardcoded at 2.2.

We went through a few ideas on how to make this happen, but finally settled on a method that seems to work successfully. After setup, it simply gets applied during each APK build. The setup is a little goofy (see below), but it solves a problem that, while seemingly straightforward, isn’t supported out of the box by Corona.

Disclaimers

  • This has only been tested on Windows. There isn’t anything Windows-specific, being all Java, but…that’s no guarantee.
  • If this doesn’t work for you…unless there’s some motivating factor (like me being bored or feeling extremely nice), no support will be forthcoming. This solution works for us, and this is just to inform the community and hopefully illustrate how to solve an obnoxious problem for someone out there.
  • As coded, this will only update an Android APK’s minmum supported SDK version to 9 (2.3, Gingerbread). It does nothing else. The way it works, though, could be used as a template to update other AndroidManifest.xml values.

Installation

  1. Go to the manifest-updater repo and either (1) get the source code and compile it or (2) under downloads, download manifest-updater-1.0.0.jar. Note that to compile from source, the axml-1.0.jar (AXML library) will have to be available. This is a Maven project, but the binaries aren’t hosted anywhere public (that I could find), so I included it manually in my local Maven repo.

  2. Put manifest-updater-1.0.0.jar somewhere on your box (the desktop or your home directory works fine). Let’s assume you put it in C:\Users\bloop\manifest-updater-1.0.0.jar.

  3. Edit the Android Ant build script at C:\Program Files (x86)\Corona Labs\Corona SDK\Resources\build.xml.

  4. Put the following lines in the build.xml in the unzip-apk-template target. The target starts on or around line 76 and you’ll want to put the new directive after the second unzip directive, on or around line 82.

    <java jar=“C:/Users/bloop/manifest-updater-1.0.0.jar” fork=“true” failonerror=“true”>   <arg file="${TEMP_DIR}/output/AndroidManifest.xml" /> </java>  

The path in jar="<path>" needs to be the path to wherever you put the manifest-updater-1.0.0.jar.

The full Ant target will look something like:

\<target name="unzip-apk-template"\> &nbsp; \<!-- Unzip the "output.zip" file received from Corona Labs' server. This contains the APK template. --\> &nbsp; \<unzip src="${TEMP\_DIR}/output.zip" dest="${TEMP\_DIR}" /\> &nbsp; \<!-- Extract the pre-built APK template's to a temporary directory. --\> &nbsp; \<unzip src="${TEMP\_DIR}/template.apk" dest="${TEMP\_DIR}/output" /\> &nbsp; \<java jar="C:/Users/bloop/manifest-updater-1.0.0.jar" fork="true" failonerror="true"\> &nbsp; &nbsp; \<arg file="${TEMP\_DIR}/output/AndroidManifest.xml" /\> &nbsp; \</java\> \</target\>&nbsp;
  1. At this point you should be set. Build an APK and the minimum SDK version should be set to 9 (2.3 or Gingerbread). If you want to verify it worked, run

    aapt list -a package.apk

where package.apk is the APK you built. This will dump a bunch of stuff about the APK, including 

android:minSdkVersion(0x0101020c)=(type 0x10)0x09

where 0x9 is the minimum SDK version in hex.

How does it work?

The way the Corona build process seems to work is (simplified and highlighting only the parts that we care about):

  1. The Lua project is bundled up and sent to the Corona server as input.zip.
  2. An APK template is passed back as output.zip.
  3. This output.zip is unzipped to a temporary location.
  4. This temporary directory is zipped (jarred), signed, zipaligned and all the other fun stuff that goes into making an APK.

Between steps 3 and 4, there’s an AndroidManifest.xml just sitting on the file system. This is, unfortunately, Android’s goofy binary xml format. The code in the manifest-updater just uses the handy AXML library to read and modify the AndroidManifest.xml in the specific way we want before the process continues.

Note that if you’re having trouble getting anything to work, a log file named apkUpdateLog.txt is output into the user’s home directory whenever an apk is built. If this file isn’t there, the Ant directive isn’t being run. If it is being run and something is going wrong, some useful information might be in this file.

Conclusion

Note that the only reason any of this is necessary is because Corona doesn’t provide enough access to the underlying AndroidManifest.xml settable values. It seems like it would be far better to just offer access to setting them through build.settings or something. Even just actually using the values in C:\Program Files (x86)\Corona Labs\Corona SDK\Resources\build.properties (which, amusingly, all have minimum SDK version set to 9) wouldn’t be bad.

However, until then, this is a (vaguely) simple and reliable way to get an APK that is compatible with the specific minimum SDK you want it to be compatible with. Since it works within the normal build of an APK, there’s no need to screw around with unpacking an existing APK, which leads to issues with re-building an re-signing.

  1. This is super-technical but nonetheless useful!

  2. Corona Labs folks; any chance of being able to set minimum version in the future?

  1. This is super-technical but nonetheless useful!

  2. Corona Labs folks; any chance of being able to set minimum version in the future?

@richard9 - Yes with the blog post today 16 APR about minV now being 2.3.3 - db2014.2264. (getting ready for future changes down the line) The ability to do something similar to 

MinimumOSVersion -  i.e. developer set minVer (2.3.3 - or higher) would give people some flexibility.

However i must declare that due to my coding experience i’m a beggar - So I cannot be a chooser.

T. 

@richard9 - Yes with the blog post today 16 APR about minV now being 2.3.3 - db2014.2264. (getting ready for future changes down the line) The ability to do something similar to 

MinimumOSVersion -  i.e. developer set minVer (2.3.3 - or higher) would give people some flexibility.

However i must declare that due to my coding experience i’m a beggar - So I cannot be a chooser.

T.