matt5784
matt5784

Reputation: 3085

Can I use AAPT to add a version code to an APK?

I have been told that there is a way to use aapt's --version-code option to add/insert a version code into an APK whose manifest doesn't define one. However, I haven't found any examples of people doing this (except for https://groups.google.com/forum/#!msg/adt-dev/XjehZDzfXhk/g2sWBC37_I4J, which links to a dead page and isn't even the same thing I am trying to do) if indeed it is possible to do.

Currently aapt d badging example.apk run on the apk in question returns something like

package: name='com.example.core' versionCode='' versionName='1.0.0' sdkVersion:'8'

In my manifest I currently have the versionName defined as a string in an xml file, so I can pull it in with something like android:versionName="@string/app_version_name" (inside the manifest tag). However, if I try this with the version code it doesn't seem to want to resolve the reference. It tries to literally set the version code to the path I enter and then fails as it isn't an integer.

I've tried to do something like aapt p --version-code 100 example.apk but it seems to want to package up a whole project and expects a directory of files as input, not an apk file. (I assume that is the default behavior of aapt p[ackage])

Is it possible to set the version code of an APK after it has been built using the aapt tool? If so, what is the correct syntax?

Upvotes: 1

Views: 3299

Answers (1)

André Oriani
André Oriani

Reputation: 3613

This solution will not use aapt directly, but it will behind the scenes.

You can use Android APKTool to open you APK, alter the AndroidManifest.xml file, adding the version info; and then use APKTool again to repack. That tool is opensource and uses aapt. Thus you can look at its code to figure out how to do it using aapt directly.

Upvotes: 2

Related Questions