Read the package name of an Android APK
Asked Answered
O

26

254

I need to get the package name of an Android APK. I have tried to unzip the APK and read the contents of the AndroidManifest.xml file but it seems that it's not a text file.

How can I extract the APK's package name?

Ong answered 9/6, 2011 at 6:57 Comment(1)
sisik.eu/apk-toolDemography
F
386
aapt dump badging <path-to-apk> | grep package:\ name
Forehead answered 9/6, 2011 at 6:59 Comment(12)
Is there a way to call this from within Android code? I want to be able to launch a package of an apk I download through my app.Eno
appt dump badging <path-to-apk> | grep package:\ nameElastomer
@Campiador you can just type up until the pipe |, press enter and then just search the output for your desired parameter. Just remember that aapt needs to be in your pathOstap
See my answer below if you need to get this programmatically (for a script).Valval
@Campiador: for Windows, uses findstr command instead of grepSnub
Upvoted despite that it extracts the whole line instead of the package name only, which unfortunately is not suitable for my script. But it put me on the right path.Chico
@Forehead I have posted a question and would like a comment from someone from google, hope you can spare some time to reply https://mcmap.net/q/66471/-simulate-the-solution-for-security-alert-unsafe-implementation-of-x509trustmanager/1503130 thanks.Joost
aapt command is located under Android\SDK\build-tools\version. on windows command can be aapt dump badging <path-to-apk> | findstr -i "package: name" or aapt dump badging <path-to-apk>Birl
apkanalyzer can be found under Android/sdk/tools/bin folder. Use apkanalyzer manifest application-id <APKFILE> to print package name of apk.Geanticline
aapt on macOS located in ~/Library/Android/sdk/build-tools/{version}Petuu
On Arch, download android-sdk-build-tools from the AURHaig
On mac, if you still see command not found: aapt, try with './' i.e cd ~/Library/Android/sdk/build-tools/{version} and then ./aapt dump badging <path-to-apk> | grep package:\ nameSuomi
A
87

Install the apk on your Android device. Then

you can launch adb shell and execute pm list packages -f, which shows the package name for each installed apk.

This is taken from Find package name for Android apps to use Intent to launch Market app from web.

Abdel answered 9/6, 2011 at 7:0 Comment(2)
Also, you will have to ensure that you are running with elevated privileges. Basically, in the terminal, use the su command first.Albinaalbinism
to search for specific package name pm list packages -f | grep term-to-search-forBigner
B
24

Based on @hackbod answer ... but related to Windows.

aapt command is located on Android\Sdk\build-tools\<build-tools-version>. If you need more info about what is aapt command (Android Asset Packaging Tool) read this https://mcmap.net/q/46223/-what-is-aapt-android-asset-packaging-tool-and-how-does-it-work

The dump sub-command of aapt is used to display the values of individual elements or parts of a package: aapt dump badging <path-to-apk>

To view only the line(s) containing the occurrence of package: name, use findstr (in Windows PowerShell or CMD):

aapt dump badging <path-to-apk> | findstr -n /C:"package: name"

Remember to surround <path-to-apk> with quotes if it contains spaces.

Hope it help other windows user!

Birl answered 19/5, 2016 at 13:25 Comment(0)
C
16

If you are looking at google play and want to know its package name then you can look at url or address bar. You will get package name. Here com.landshark.yaum is the package name

 https://play.google.com/store/apps/details?id=com.landshark.yaum&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5sYW5kc2hhcmsueWF1bSJd
Cushiony answered 5/6, 2012 at 12:23 Comment(0)
K
13

If you open the AndroidManifest.xml using MS Notepad, search for phrase package and you'll find following:

package manifest $xxx.xxxxxxx.xxxxxxx |

where xxx.xxxxxxx.xxxxxxx is your package name, just written with a space after each character.

It's useful way when you don't have any specific tools installed.

Kesha answered 27/9, 2012 at 18:48 Comment(2)
This is especially useful if the code for the app is on github! Much easier than the other methods, thanks!Pettish
I unpacked the APK, but AndroidManifest.xml was a binary file. Weird but true.Oligarchy
F
13

The following bash script will display the package name and the main activity name:

apk_package.sh

package=$(aapt dump badging "$*" | awk '/package/{gsub("name=|'"'"'","");  print $2}')
activity=$(aapt dump badging "$*" | awk '/activity/{gsub("name=|'"'"'","");  print $2}')
echo
echo "   file : $1"
echo "package : $package"
echo "activity: $activity"

run it like so:

apk_package.sh /path/to/my.apk
Feverfew answered 5/7, 2013 at 22:14 Comment(1)
Never combine grep and sed with awk. Using awk is enough.Valval
M
11

Since its mentioned in Android documentation that AAPT has been deprecated, getting the package name using AAPT2 command in Linux is as follows:

./aapt2 dump packagename <path_to_apk>

Since I am using an older version of Gradle build, I had to download a newer version of AAPT2 as mentioned here :

Download AAPT2 from Google Maven

Using the build-tools in my sdk - 25.0.3, 26.0.1 and 27.0.3, executing the aapt2 command shows an error: Unable to open 'packagename': No such file or directory. That's why I went for the newer versions of AAPT2.

I used 3.3.0-5013011 for linux.

Meerkat answered 18/1, 2019 at 18:57 Comment(1)
For aapt2 version 2.19, I had to do aapt2 dump /path/to/apk | grep "Package name"Intracardiac
V
8

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

Valval answered 26/5, 2015 at 19:12 Comment(1)
Thanks for giving more information to understand rather than just giving a single line of command to copy.Spreader
H
7

A simple solution would be Open Android Studio -> Build -> Analyze Apk... browse and select the APK now you can find the package name and pretty much you can read.

Hoarse answered 28/6, 2018 at 8:43 Comment(0)
M
7

You can use Analyze APK... from the Build menu in Android Studio, it will display the package name at the top of new window.

Meander answered 12/2, 2020 at 10:23 Comment(0)
D
4

On Mac:

Way 1:

zgong$ /Users/zgong/Library/Android/sdk/build-tools/29.0.3/aapt dump badging ~/Downloads/NonSIMCC-151-app-release-signed.apk
package: name='com.A.B' versionCode='2020111801' versionName='1.0.40' compileSdkVersion='29' compileSdkVersionCodename='10'
sdkVersion:'24'
targetSdkVersion:'29'
......

Way 2:

/Users/zgong/Library/Android/sdk/build-tools/29.0.3/aapt2 dump packagename ~/Downloads/NonSIMCC-151-app-release-signed.apk
com.A.B
Duramen answered 18/11, 2020 at 15:55 Comment(0)
D
3

If you don't have the Android SDK installed, like in some test scenarios, you can get the package name using the following bash method:

getAppIdFromApk() {
  local apk_path="$1"

  # regular expression (required)
  local re="^\"L.*/MainActivity;"
  # sed substitute expression
  local se="s:^\"L\(.*\)/MainActivity;:\1:p"
  # tr expression
  local te=' / .';

  local app_id="$(unzip -p $apk_path classes.dex | strings | grep -Eo $re | sed -n -e $se | tr $te)"

  echo "$app_id"
}

Tested on a mac. 'strings' and 'unzip' are standard on most linux's, so should work on linux too.

Denumerable answered 24/6, 2019 at 23:54 Comment(1)
This is the java package name of the Mainactivity class though, might not match the app's package name.Geophagy
L
3

A very simple method is to use apkanalyzer.

apkanalyzer manifest application-id "${_path_to_apk}"
Ligetti answered 11/9, 2019 at 12:46 Comment(1)
Works for me, but replacing all "${_path_to_apk}" placeholder by apk path. Example: apkanalyzer manifest application-id /Users/user/..../project/build/app/outputs/flutter-apk/app.apkRift
A
3

You can use Android Studio for this. Select your APK via the GUI menu: enter image description here

And open manifest as usual: enter image description here

Annulation answered 15/8, 2023 at 8:38 Comment(0)
S
2

If you just want to know package name, run adb logcat, launch the activity you want , you will get a hint on the package name.

Screwworm answered 9/6, 2011 at 7:1 Comment(0)
C
2

Another solution is to use aapt list and use sed to parse through that:

aapt list -a $PATH_TO_YOUR_APK | sed -n "/^Package Group[^s]/s/.*name=//p"

Circumscribe answered 11/2, 2014 at 23:33 Comment(1)
Package Group Name is only coincidentally the same as Package Name in some apps. You really need to be using the A:package="com.example.appname" in the Android Manifest: section if you do aapt list -aValval
C
2

I think the best and simplest way to extract only the package name in Linux is

aapt dump badging <APK_path> | grep package | sed -r "s/package: name='([a-z0-9.]*)'.*/\1/"

Explanation:

  1. AAPT extracts the APK information
  2. Grep "package" to keep only the line about the package information
  3. Make sed replace the whole line with the package name only using the following regex: package: name='([a-z0-9.]*)'.* and replacing with the first (and only) matching group.
Chico answered 17/7, 2015 at 8:49 Comment(0)
D
2

There's a very simple way if you got your APK allready on your Smartphone. Just use one of these APPs:

Package Name Viewer Apps

Dichotomize answered 16/3, 2017 at 16:34 Comment(0)
S
1

You can extract AndroidManifest.xml from the APK, remove all NULL bytes, skip everything until after the string 'manifest', and then you are at a length byte followed by the package name (and what comes after it). For the difficult task I use the great GEMA tool, so the command looks like this:

7z e -so MyApp.apk AndroidManifest.xml | gema '\x00=' | gema -match 'manifest<U1><U>=@substring{0;@char-int{$1};$2}'

Of course, you can use any other tool to do the filtering.

Synaesthesia answered 22/1, 2017 at 14:38 Comment(0)
S
1

For Windows following worked for me:

:: // Initializing variables
SET adb="C:\Users\<User name>\AppData\Local\Android\Sdk\platform-tools\adb"
SET aapt="C:\Users\<User name>\AppData\Local\Android\Sdk\build-tools\22.0.0\aapt"
SET APKPath=C:\Users\<User name>\Desktop\APK\Instant_Instal\

CD %APKPath%

:: // Searching for apk file and storing it
FOR /F "delims=" %%f IN ('dir /S /B *.apk') DO SET "APKFullPath=%%f"
SET apk=%APKFullPath%

:: // Command adb install apk, run apk
%adb% install %apk%

:: // Fetching package name from apk
%aapt% dump badging %APKFullPath% | FIND "package: name=" > temp.txt
FOR /F "tokens=2 delims='" %%s IN (temp.txt) DO SET pkgName=%%s
del temp.txt

:: // Launching apk
%adb% shell monkey -p %pkgName% -c android.intent.category.LAUNCHER 1

pause

Note

Please edit the paths of adb, aapt, APKPath according to the paths of adb, aapt, and the apk location in your system.

Working:

  • Here I have added the apk in a folder on Desktop "\Desktop\APK\Instant_Instal\".
  • The command %adb% install %apk% installs the application if the device is connected.
  • This %aapt% dump badging %APKFullPath% | FIND "package: name=" > temp.txt fetches package name and a few other details like version etc. of the apk and stores in a temp.txt file in same location as that of the apk.
  • FOR /F "tokens=2 delims='" %%s IN (temp.txt) DO SET pkgName=%%sextracts the package name and assigns topkgName` variable
  • Finally %adb% shell monkey -p %pkgName% -c android.intent.category.LAUNCHER 1 launches the app.

In essence the above code installs the apk from given location in desktop "Desktop\APK\Instant_Instal\" to the device and launches the application.

Scherle answered 14/5, 2018 at 6:6 Comment(0)
S
1

You can get the package name programmatically by :

packageManager.getPackageArchiveInfo(apkFilePath, 0)?.packageName
Strep answered 16/12, 2020 at 13:57 Comment(1)
other answers, use external tools but by this answer you can extract your package name in your android app.Observer
P
0

you can instal Package_Name_Viewer.apk on your emulator and next you can see package name of all instaled app on your emulator.

Pikestaff answered 27/1, 2015 at 21:19 Comment(0)
D
0

I also tried the de-compilation thing, it works but recently I found the easiest way:

  1. Download and install Appium from Appium website

  2. Open Appium->Android setting, choose the target apk file. And then you get everything you want, the package info, activity info.

Dallman answered 16/7, 2015 at 8:2 Comment(0)
E
0

To use this in batch scripting it's handy to have the script return just the package name (e.g. for uninstalling an app when you have the APK).

Here's the script I use:

# extract the android package id from a built apk file
# usage ./getPackageName.sh <path-to-apk>
line=`aapt dump badging "$1" | grep package:\ name`

# above returns:
# package: name='com.calvium.myapp' versionCode='1' versionName='1.0'
if [[ $line =~ name=\'(.+)\'\ versionCode ]]; then
    echo ${BASH_REMATCH[1]}
else
    echo "Failed to find package name"
    exit 1
fi

available on gist

So you could write:

adb uninstall `./getPackageName.sh file.apk`

Erv answered 26/11, 2015 at 15:37 Comment(0)
C
0

As I don't was able to find the package name in the .apk file with editor (like suggested above), I have checked the functions in the App "ES Datei Explorer" / "ES File Explorer" (free version) that I had installed already. In this tool, the package name is showed properly.
As I think a good file explorer should not be missing on a phone, I suggest to use this tool (if you already have installed the apk on an mobile and have to know the package name).

Cassiopeia answered 8/8, 2018 at 17:15 Comment(0)
O
0

If you want to read the package name of a typical APK file in your app, there's an easy way to analyze the PackageInfo:

fun getAPKPackageName(apkFile: File?): String? {
        if (apkFile == null || !apkFile.isFile || !apkFile.exists()) return null
        val apkFilePath = apkFile.absolutePath
        if (apkFilePath.isNullOrEmpty()) return null
        val packageManager = App.context.packageManager ?: return null
        val packageInfo = packageManager.getPackageArchiveInfo(apkFilePath, 0) ?: return null
        return packageInfo.packageName
    }
Ornithorhynchus answered 4/1, 2023 at 2:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.