[英]PHP: How to get version from android .apk file?
我正在嘗試創建一個PHP腳本,以從Android APK文件中獲取應用程序版本。 從APK(zip)文件中提取XML文件然后解析XML是一種方法,但我想它應該更簡單。 像PHP手冊 ,例如#3。 任何想法如何創建腳本?
如果您在服務器上安裝了Android SDK,則可以使用PHP的exec(或類似)執行aapt
工具(在$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格式。 APK zip結構中的AndroidManifest.xml
文件不是純文本。
您需要將類似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++;
}
}
?>
aapt dump badging ./apkfile.apk | grep sdkVersion -i
你將獲得一個人類可讀的形式。
sdkVersion:'14'
targetSdkVersion:'14'
如果您安裝了Android SDK,只需在系統中查找。 我在:
<SDKPATH>/build-tools/19.0.3/aapt
轉儲格式有點奇怪,並不是最容易使用的。 只是為了擴展其他一些答案,這是一個shell腳本,我用它來解析APK文件的名稱和版本。
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 }'
在CLI中使用它:
apktool if 1.apk
aapt dump badging 1.apk
您可以使用exec
或shell_exec
在PHP中使用這些命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.