我需要得到一个Android APK的包名。我已经尝试解压缩APK和读取AndroidManifest.xml文件的内容,但它似乎不是一个文本文件。

如何提取APK的包名?


当前回答

我认为在Linux中只提取包名的最好和最简单的方法是

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

解释:

AAPT提取APK信息 Grep“package”只保留一行关于包的信息 让sed只使用以下正则表达式将整行替换为包名:package: name='([a-z0-9.]*)'。*并替换为第一个(也是唯一一个)匹配组。

其他回答

对于Windows,以下对我来说是有效的:

:: // 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

请根据adb、aapt路径和apk在系统中的位置,编辑adb、aapt、APKPath路径。

工作:

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.

从本质上讲,上面的代码将apk从桌面“desktop \ apk \ instant_install \”中的给定位置安装到设备并启动应用程序。

在Mac:

方式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'
......

方式2:

/Users/zgong/Library/Android/sdk/build-tools/29.0.3/aapt2 dump packagename ~/Downloads/NonSIMCC-151-app-release-signed.apk
com.A.B

如果你使用MS Notepad打开AndroidManifest.xml,搜索短语包,你会发现以下内容:

package manifest $xxx.xxxxxxx.xxxxxxx |

其中xxx.xxxxxxx.xxxxxxx是您的包名,只是在每个字符后面加上一个空格。

当您没有安装任何特定的工具时,这是一种有用的方法。

如果你想读取应用中典型APK文件的包名,有一个简单的方法来分析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
    }

根据@hackbod的回答…但与窗户有关。

aapt命令位于Android\SDK\build-tools\version。 如果你需要更多关于什么是appt命令(Android资产打包工具)的信息,请阅读https://stackoverflow.com/a/28234956/812915

aapt的dump子命令用于显示包中各个元素或部分的值:

apt转储标记<path-to-apk>

如果只希望看到package: name info这一行,请使用findstr

apt dump badging <path-to-apk> | findstr -n "package: name" | findstr "1:"

希望对其他windows用户有所帮助!