PHP:如何从android .apk文件获取版本?
我正在尝试创建一个 PHP 脚本来从 Android APK 文件获取应用程序版本。 从APK(zip)文件中提取XML文件然后解析XML是一种方法,但我想它应该更简单。类似于 PHP 手册,示例 #3。 关于如何创建脚本有什么想法吗?
I am trying to create a PHP script to get the app version from Android APK file.
Extracting XML file from the APK (zip) file and then parsing XML is one way, but I guess it should be simpler. Something like PHP Manual, example #3.
Any ideas how to create the script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果服务器上安装了 Android SDK,则可以使用 PHP 的 exec(或类似工具)来执行
aapt
工具(位于$ANDROID_HOME/platforms/android-X/tools
)。输出应包括:
如果您无法安装 Android SDK,无论出于何种原因,那么您将需要解析 Android 的二进制 XML 格式。 APK zip 结构中的
AndroidManifest.xml
文件不是纯文本。您需要移植类似 AXMLParser 的实用程序 从 Java 到 PHP。
If you have the Android SDK installed on the server, you can use PHP's exec (or similar) to execute the
aapt
tool (in$ANDROID_HOME/platforms/android-X/tools
).And the output should include:
If you can't install the Android SDK, for whatever reason, then you will need to parse Android's binary XML format. The
AndroidManifest.xml
file inside the APK zip structure is not plain text.You would need to port a utility like AXMLParser from Java to PHP.
我创建了一组 PHP 函数,可以仅查找 APK 的版本代码。这是基于以下事实:AndroidMainfest.xml 文件包含版本代码作为第一个标签,并且基于 axml(二进制 Android XML 格式)为 此处描述
I've created a set of PHP functions that will find just the Version Code of an APK. This is based on the fact that the AndroidMainfest.xml file contains the version code as the first tag, and based on the axml (binary Android XML format) as described here
在 CLI 中使用它:
您可以在 PHP 中使用
exec
或shell_exec
使用这些命令。Use this in the CLI:
You can use these commands in PHP using
exec
orshell_exec
.您将获得人类可读的形式。
如果您安装了 Android SDK,只需在系统中查找 aapt 即可。
我的在:
You will get a human readable form.
Just look for aapt in your system if you have Android SDK installed.
Mine is in:
转储格式有点奇怪,而且不是最容易使用的。只是为了扩展一些其他答案,这是一个 shell 脚本,我用它来从 APK 文件中解析出名称和版本。
以下是适用于 gawk 和 mawk 的变体(如果转储格式发生变化,耐用性会稍差,但应该没问题):
match($0, /^application-label:\'([^\']*)\'/, a) { n = a[1] } match($0, /versionName=\'([^\']*)\'/, b) { v=b[1] } END { if ( length(n)>0 && length(v)>0 ) { print n, v } }'如果您只想要版本,那么显然它可以简单得多。
以下是适用于 gawk 和 mawk 的变体(如果转储格式发生变化,耐用性会稍差,但应该没问题):
The dump format is a little odd and not the easiest to work with. Just to expand on some of the other answers, this is a shell script that I am using to parse out name and version from APK files.
Here are variations suitable for both gawk and mawk (a little less durable in case the dump format changes but should be fine):
match($0, /^application-label:\'([^\']*)\'/, a) { n = a[1] } match($0, /versionName=\'([^\']*)\'/, b) { v=b[1] } END { if ( length(n)>0 && length(v)>0 ) { print n, v } }'If you just want the version then obviously it can be much simpler.
Here are variations suitable for both gawk and mawk (a little less durable in case the dump format changes but should be fine):