Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fixes an Android APK built by PhoneGap Build to correct the problem of xhdpi images not being properly added ( Requires apktool and jarsigner to already be installed.
#!/usr/bin/env python
# fix an Android APK built by PhoneGap Build to correct the problem of
# xhdpi images not being correctly added (
# REQUIRES apktool and jarsigner to already be installed and on the PATH.
# usage: www/config.xml InputFile.apk OutputFile.apk
import os,sys,shutil,tempfile,xml.dom.minidom
(configXml, inputApk, outputApk) = sys.argv[1:]
assert outputApk.endswith('.apk')
exit("usage: www/config.xml InputFile.apk OutputFile.apk")
outputSignedApk = outputApk.replace('.apk','-signed.apk')
if os.path.exists(outputApk): exit("error: %s already exists" % (outputApk))
if os.path.exists(outputSignedApk): exit("error: %s already exists" % (outputSignedApk))
xmldoc = xml.dom.minidom.parse(configXml)
xhdpiFilter = lambda x: ('gap:platform' in x.attributes.keys() and x.attributes['gap:platform'].value == 'android' and 'gap:density' in x.attributes.keys() and x.attributes['gap:density'].value == 'xhdpi')
iconfile = filter(xhdpiFilter, xmldoc.getElementsByTagName('icon'))[0].attributes['src'].value
splashfile = filter(xhdpiFilter, xmldoc.getElementsByTagName('gap:splash'))[0].attributes['src'].value
tmpdir = tempfile.mkdtemp()
os.system('apktool decode --force %s %s' % (inputApk, tmpdir))
shutil.copyfile(os.path.join(os.path.dirname(configXml), iconfile),os.path.join(tmpdir, 'res/drawable-xhdpi/icon.png'))
shutil.copyfile(os.path.join(os.path.dirname(configXml), splashfile),os.path.join(tmpdir, 'res/drawable-xhdpi/splash.png'))
os.system('apktool build %s %s' % (tmpdir,outputApk))
os.system('jarsigner -verbose -keystore %s/.android/debug.keystore -storepass android -keypass android -digestalg SHA1 -sigalg MD5withRSA -sigfile CERT -signedjar %s %s androiddebugkey' % (os.path.expanduser('~'),outputSignedApk, outputApk))
print "Built %s and %s" % (outputApk, outputSignedApk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment