Thursday, 10 November 2011

Edit AndroidManifest.xml in Android Phonegap Build App

Phonegap build is an awesome service that allows you to convert a bunch of HTML files into actual mobile apps for iPhone, Android, Blackberry, and more. It's great, so great in fact that Adobe recently bought the company behind it (Nitobi).

You can read more about the details at their site, I won't repeat it all here.

I've been playing around with the Android version of this for a client project I've been working on, and came up with a bit of a problem.  There's a file called AndroidManifest.xml that every Android app includes that sets up loads of important stuff such as the intent filters and the permissions needed to run the app.

Phonegap build allows you to change some of these settings using it's config.xml system.  But for other settings you are, it seems, stuck.  I needed to change the intent filter for one of my activities, and there was no way that the config.xml could do this. I posted on their forums and the very helpful guy said they may add the feature in a future release.

In the meantime though, I found another way.

It's possible to decompile the apk file that you download from Phonegap Build, change the AndroidManifest.xml file, re-compile, re-sign, and install it onto your device.

Getting the tools we need
I'm assuming that you have the Android SDK installed, if not install it and follow the instructions to put the build tools into your path.
First you need to download apktool.  This great tool allows you to decode and re-encode apk files. Download it and follow the instructions to install for your platform.
We're also going to use:

  • jarsigner - part of the Java JVN (I have a binary at /usr/bin/jarsigner) 
  • adb - part of the Android SDK installation (my binary is at ~/android/android-sdk-mac_x86/platform-tools/adb)
Extracting the APK file

  • Download the apk file for your app from Phonegap build.
  • Open up the terminal and type (substituting your folders for path/to and filenames where appropriate)
apktool d path/to/yourApp.apk path/to/output-folder
  • This will extract the contents of the app into the folder output-folder.
  • The AndroidManifest.xml file will be at the root of that folder.
Now you can make whatever changes to the AndroidManifest you need (setting intent filters etc).

Re-building the APK file

  • Once you've finished making your changes, you can rebuild the APK file from the contents of the output-folder
apktool b path/to/output-folder path/to/yourAppV2.apk 
Re-signing the APK
If you try to install the new apk file you'll find that it fails with an invalid certificate error.
This is because apktool doesn't re-sign the apk.  It's pretty easy to re-sign the APK with the development certificate (which is what Eclipse uses when making APKs for testing).

Here's how:
jarsigner -verbose -keystore ~/.android/debug.keystore path/to/yourAppV2.apk androiddebugkey
By default the development certificate is in the ~/.android folder. When you run this, it'll ask for a passphrase - use android.

Your APK is now ready for installation!
adb install path/to/yourAppV2.apk

13 comments:

  1. This is a HUGE help, thank you!

    ReplyDelete
  2. Am I correct in assuming that once you've deconstructed and reconstructed the APK, you can no longer use PhoneGap Build to make updates?

    ReplyDelete
  3. You're a genius.. You just saved me 3 days

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I use the apktool to Decryption my "AndroidManifest.xml",but finally I install the apk file to the Android machine that I met some problem...

    Let me explain the purpose of each file and folder first
    http://www.littlebau.com/apktool/01.gif

    The "phonegap_decode" folder is the result location after I decode the original phonegap.apk file

    The "phonegap_encode" folder is the result location after I modify the Decryption "AndroidManifest.xml" and save and rebuilt to the new phonegap.apk file

    The "phonegap_resign" folder is the result location after I re-sign the encode "phonegap.apk" file

    The "android.keystore" is my Android key,and it is no problem with the phonegap build

    The "aapt.exe"、"apktool.bat"、"apktool.jar" are all the apktool related file,and I use 1.52 version
    https://code.google.com/p/android-apktool/downloads/list

    The "phonegap.apk" is my original Android .apk file that downloaded from phonegap build,and it is no problem install on the Android phone or tablet

    =====

    first step,I use this command line to decode my phonegap.apk to "phonegap_decode" folder location,and it is success
    apktool d -f phonegap.apk phonegap_decode/
    http://www.littlebau.com/apktool/02.gif
    http://www.littlebau.com/apktool/03.gif

    second step,I use this command line to encode my "phonegap_decode" folder to a new encode phonegap.apk that location to "phonegap_encode" folder,and it is success but seems have a little problem
    apktool b -f phonegap_decode/ phonegap_encode/phonegap.apk
    http://www.littlebau.com/apktool/04.gif

    third step,I use this command line to resign my "phonegap_encode" folder's phonegap.apk to a new resign phonegap.apk that location to "phonegap_resign" folder,and it is success
    jarsigner -verbose -keystore android.keystore phonegap_resign/phonegap.apk littlebau
    http://www.littlebau.com/apktool/05.gif

    final step,I upload the "phonegap_resign/phonegap.apk" to my Android machine,and install it,but still can't install
    http://www.littlebau.com/apktool/06.png

    ReplyDelete
  6. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    Android training in chennai with placement | Android Training in chennai |Android Training in Velachery

    ReplyDelete
  7. PhoneGap Build aims to take away the pains of configuring SDKs and compiling native applications so you can focus on writing great code. As part of this, we obfuscate management of the platform configuration files – namely your Android Manifest and your iOS Property List. Android Training in chennai | Best Android Training in chennai|Android Training in chennai with placement | Android Training in velachery

    ReplyDelete
  8. PhoneGap Mobile application aims to take away from some valuable pains of configuration PhoneGap application tools SDKs and it's complaining to a native application and it's you focus on the code is really great.In these file one of the obfuscate management platform configuration system..Net Training in Chennai | Selenium Training in Chennai | Best Hadoop Training in Chennai | Android Training in Chennai with Placement

    ReplyDelete
  9. Your blog has given me that thing which I never expect to get from all over the websites. Nice post guys!

    Melbourne Web Developer

    ReplyDelete