PHP:如何从Android .APK文件中获取版本?
题
我正在尝试创建一个PHP脚本来从Android APK文件中获取应用程序版本。从APK(zip)文件中提取XML文件,然后解析XML是一种方法,但我想这应该更简单。就像是 PHP手册, ,示例#3。有什么想法如何创建脚本?
解决方案
如果您在服务器上安装了Android SDK,则可以使用PHP的EXEC(或类似)来执行 aapt
工具(in $ANDROID_HOME/platforms/android-X/tools
).
$ aapt dump badging myapp.apk
输出应包括:
package: name='com.example.myapp' versionCode='1530' versionName='1.5.3'
如果你 不能 安装Android SDK,无论出于何种原因,您都需要解析Android的二进制XML格式。这 AndroidManifest.xml
APK邮政编码结构内的文件是 不是 纯文本。
您需要移植 像axmlparser这样的实用程序 从Java到PHP。
其他提示
我创建了一组PHP函数,这些功能只会找到APK的版本代码。这是基于以下事实:androidMainFest.xml文件包含版本代码作为第一个标签,并基于AXML(二进制Android XML格式)作为 在这里描述
<?php
$APKLocation = "PATH TO APK GOES HERE";
$versionCode = getVersionCodeFromAPK($APKLocation);
echo $versionCode;
//Based on the fact that the Version Code is the first tag in the AndroidManifest.xml file, this will return its value
//PHP implementation based on the AXML format described here: https://stackoverflow.com/questions/2097813/how-to-parse-the-androidmanifest-xml-file-inside-an-apk-package/14814245#14814245
function getVersionCodeFromAPK($APKLocation) {
$versionCode = "N/A";
//AXML LEW 32-bit word (hex) for a start tag
$XMLStartTag = "00100102";
//APK is esentially a zip file, so open it
$zip = zip_open($APKLocation);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
//Look for the AndroidManifest.xml file in the APK root directory
if (zip_entry_name($zip_entry) == "AndroidManifest.xml") {
//Get the contents of the file in hex format
$axml = getHex($zip, $zip_entry);
//Convert AXML hex file into an array of 32-bit words
$axmlArr = convert2wordArray($axml);
//Convert AXML 32-bit word array into Little Endian format 32-bit word array
$axmlArr = convert2LEWwordArray($axmlArr);
//Get first AXML open tag word index
$firstStartTagword = findWord($axmlArr, $XMLStartTag);
//The version code is 13 words after the first open tag word
$versionCode = intval($axmlArr[$firstStartTagword + 13], 16);
break;
}
}
}
zip_close($zip);
return $versionCode;
}
//Get the contents of the file in hex format
function getHex($zip, $zip_entry) {
if (zip_entry_open($zip, $zip_entry, 'r')) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$hex = unpack("H*", $buf);
return current($hex);
}
}
//Given a hex byte stream, return an array of words
function convert2wordArray($hex) {
$wordArr = array();
$numwords = strlen($hex)/8;
for ($i = 0; $i < $numwords; $i++)
$wordArr[] = substr($hex, $i * 8, 8);
return $wordArr;
}
//Given an array of words, convert them to Little Endian format (LSB first)
function convert2LEWwordArray($wordArr) {
$LEWArr = array();
foreach($wordArr as $word) {
$LEWword = "";
for ($i = 0; $i < strlen($word)/2; $i++)
$LEWword .= substr($word, (strlen($word) - ($i*2) - 2), 2);
$LEWArr[] = $LEWword;
}
return $LEWArr;
}
//Find a word in the word array and return its index value
function findWord($wordArr, $wordToFind) {
$currentword = 0;
foreach ($wordArr as $word) {
if ($word == $wordToFind)
return $currentword;
else
$currentword++;
}
}
?>
在CLI中使用它:
apktool if 1.apk
aapt dump badging 1.apk
您可以在PHP中使用这些命令 exec
或者 shell_exec
.
aapt dump badging ./apkfile.apk | grep sdkVersion -i
您将获得人类可读的形式。
sdkVersion:'14'
targetSdkVersion:'14'
如果安装了Android SDK,请在系统中查找AAPT。我的在:
<SDKPATH>/build-tools/19.0.3/aapt
垃圾场格式有点奇怪,也不是最容易使用的。只是为了扩展其他一些答案,这是我用来从APK文件中解析名称和版本的Shell脚本。
aapt d badging PACKAGE | gawk $'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 } }'
如果您只想要版本,那么显然可能会更简单。
aapt d badging PACKAGE | gawk $'match($0, /versionName=\'([^\']*)\'/, v) { print v[1] }'
以下是适合Gawk和Mawk的变体(如果转储格式更改但应该很好,则耐用少一些):
aapt d badging PACKAGE | mawk -F\' '$1 ~ /^application-label:$/ { n=$2 }
$5 ~ /^ versionName=$/ { v=$6 }
END{ if ( length(n)>0 && length(v)>0 ) { print n, v } }'
aapt d badging PACKAGE | mawk -F\' '$5 ~ /^ versionName=$/ { print $6 }'
不隶属于 StackOverflow