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

34 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
  10. Thanks for providing your information. We are Providing AWS Online Training Hyderabad Get more knowledge

    ReplyDelete
  11. I really didn’t know all these things before which you have informed us through your best writing.

    phonegap services in hyderabad

    ReplyDelete
  12. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    rpa online training

    ReplyDelete
  13. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune
    Data science training in kalyan nagar
    selenium training in chennai

    ReplyDelete
  14. I’ve desired to post about something similar to this on one of my blogs and this has given me an idea. Cool Mat.

    java training in chennai | java training in bangalore

    java online training | java training in pune

    java training in chennai | java training in bangalore

    ReplyDelete
  15. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    python training in Bangalore
    python training in pune
    python online training
    python training in chennai

    ReplyDelete
  16. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    Devops training in velachery
    Devops training in annanagar
    Devops training in sholinganallur
    Devops training in tambaram

    ReplyDelete
  17. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    Devops training in velachery
    Devops training in annanagar
    Devops training in sholinganallur
    Devops training in tambaram

    ReplyDelete
  18. Read all the information that i've given in above article. It'll give u the whole idea about it.
    Blueprism training in marathahalli

    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  19. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Blueprism training in marathahalli

    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  20. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    angularjs

    Training in chennai


    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    ReplyDelete
  21. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  22. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  23. Lovely blog with much more interesting article, I will keep on reading your update. Thanks for the share Web Developers in Bangalore | Website Developers in Bangalore | Web Designers in Bangalore Bangalore

    ReplyDelete