[android] Read the package name of an Android APK

A Programmatic Answer

If you need to do this programmatically, it's a little more involved than just getting the answer into your brain. I have a script that I use to sign all of our apps, but each use a different key. Here are 2 ways to get just the Package Name as output so you can put it in a variable or do whatever you need with it.

Example output: com.example.appname (and nothing more)

Requirements

aapt - Android Asset Packaging Tool, part of the SDK Tools download

Solution 1

Using awk specify ' as the Field Separator, search for a line with package: name=, and print only the 2nd "field" in the line:

aapt dump badging /path/to/file.apk | awk -v FS="'" '/package: name=/{print $2}'

A weakness of this method is that it relies on aapt to output the package information fields in the same order:

package: name='com.example.appname' versionCode='3461' versionName='2.2.4' platformBuildVersionName='4.2.2-1425461'

We have no commitments from the developers to maintain this format.

Solution 2

Using awk specify " as the Field Separator, search for a line with package=, and print only the 2nd "field" in the line:

aapt list -a /path/to/file.apk | awk -v FS='"' '/package=/{print $2}'

A weakness of this method is that it relies on aapt to output package= only in the Android Manifest: section of the output. We have no commitments from the developers to maintain this format.

Solution 3

Expand the apk file with apktool d and read the AndroidManifest.xml.

This would be the best method, but the AndroidManifest.xml is a binary file and all the SO answers I see for converting it to text do not work. (Using apktool d instead of a simple unzip is supposed to do this for you, but it does not.) Please comment if you have an solution to this issue