如何获取 Android AAB 文件的版本代码和版本名称(标记)?

How do I get the versionCode and versionName (badging) of an Android AAB file?

类似于 Get Android .apk file VersionName or VersionCode WITHOUT installing apk, I want to get the versionCode and/or versionName with just an .aab 文件。

我试过简单地使用 the classic answer for .apk files,但用我的 .aab 文件代替,但它没有用:

$ $ANDROID_HOME/sdk/build-tools/29.0.2/aapt dump badging my_aab.aab
ERROR: dump failed because no AndroidManifest.xml found

我也试过尝试直接转储 xmltree

$ $ANDROID_HOME/sdk/build-tools/29.0.2/aapt dump xmltree my_aab.aab base/manifest/AndroidManifest.xml
W/ResourceType(39872): Bad XML block: header size 664 or total size 118110474 is larger than data size 35953
ERROR: Resource base/manifest/AndroidManifest.xml is corrupt

.aab 文件将它们的 xml 存储在 protocol buffer format:

There is manifest folder having Android Manifest.xml file in the apk it is in binary format but in .aab it is real XML file compiled into a protocol buffer format because this allows to transform it easily.

并且 aapt2 有一个 convert subcommand Converts an apk between binary and proto formats.,它会转换一个 .apk 文件,它只包含一个 AndroidManifest.xml以原型格式。因此:

# Extract the AndroidManifest.xml directly
# without -p, unzip will recreate the directory structure.
unzip -p my_aab.aab base/manifest/AndroidManifest.xml > AndroidManifest.xml
# Create a dummy .apk with the proto-formatted AndroidManifest.xml
zip proto_version.apk AndroidManifest.xml
# Convert the proto-formatted AndroidManifest.xml into an apk-formatted XML
aapt2 convert proto_version.apk -o version.apk
# Now dump the badging
# I don't know why, but dump badging fails, so add `|| true` to make it succeed
aapt dump badging version.apk || true

不幸的是,最后的命令没有成功:

W/ResourceType(42965): No known package when getting value for resource number 0x7f100000
AndroidManifest.xml:47: error: ERROR getting 'android:icon' attribute: attribute value reference does not exist

但它确实按预期打印了 versionNameversionCode。您可以使用 || true 忽略失败,或者您可以使用 dump xmltree 子命令转储原始 XML,这会成功:

aapt dump xmltree version.apk AndroidManifest.xml

Bundletool 有一个命令可以在 XML 中转储 AAB 的清单,甚至可以使用 xpath 提取清单的特定属性。

bundletool dump manifest --bundle bundle.aab

并且只提取版本代码:

bundletool dump manifest --bundle bundle.aab --xpath /manifest/@android:versionCode

希望对您有所帮助。