我想創建一個PHP腳本來從Android APK文件中獲取應用程序版本。 從APK(zip)文件中提取XML文件,然後解析XML是一種方法,但我想它應該更簡單。例如PHP Manual,例#3。 任何想法如何創建腳本?PHP:如何從android.apk文件獲取版本?
回答
如果你在服務器上安裝了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
文件是而不是純文本。
您需要將utility like AXMLParser從Java移植到PHP。
在CLI中使用此:
apktool if 1.apk
aapt dump badging 1.apk
可以使用exec
或shell_exec
在PHP中使用這些命令。
我創建了一組PHP函數,它們只會查找APK的版本代碼。這是基於該AndroidMainfest.xml文件中包含的版本代碼作爲第一個標籤的事實,並基於axml(二進制的Android XML格式)爲described here
<?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++;
}
}
?>
hi @ CHeil402如何獲取版本號 – 2015-02-25 13:13:23
aapt dump badging ./apkfile.apk | grep sdkVersion -i
你會得到一個人類可讀形成。
sdkVersion:'14'
targetSdkVersion:'14'
如果您安裝了Android SDK,只需在系統中查找aapt即可。 Mine is in:
<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] }'
這裏有適合於觀看或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 }'
- 1. 如何從gemspec文件獲取版本?
- 2. 如何從php獲取mongodb的版本
- 3. 獲取PHP版本文件Wamp
- 4. 從非執行文件獲取版本
- 5. 如何從php獲取文本?
- 6. 獲取文本文件中的版本
- 7. 如何從Amazon AWS S3版本2獲取文件大小?
- 8. 如何從給定的XSLT文件中獲取版本號。
- 9. 如何從轉儲文件中獲取oracle數據庫版本
- 10. Python如何從.whl文件中獲取版本號
- 11. 如何從apk文件使用JAVA eclipse獲取應用版本
- 12. Git:如何從.gitattributes ident獲取特定版本的文件?
- 13. 如何從DACPAC文件獲取版本號
- 14. 如何從文本文件中獲取列和行。 php/html
- 15. 如何從PHP文件Jquery獲取值?
- 16. PHP:如何從JSON文件獲取值?
- 17. 如何從ASP獲取.NET .dll版本
- 18. 如何從System.Web.HttpRequest獲取協議版本?
- 19. 如何從.NET獲取Windows版本號?
- 20. 如何從emacs獲取windows版本
- 21. 如何從SharpSVN獲取SVN版本?
- 22. 如何從代碼獲取Flutter版本?
- 23. 如何從VSIX獲取SSMS的版本
- 24. PHP - WordPress腳本從文本文件中獲取隨機文本
- 25. 如何從本身內部獲取OSGi組件的版本?
- 26. 如何從代碼獲取PCRE版本(與PHP捆綁)?
- 27. 如何從php.net獲取最新的php版本信息
- 28. 如何從源代碼獲取PHP版本?
- 29. 如何從.htaccess的php腳本獲取源文件Action/AddHandler
- 30. 如何獲取bukkit版本?
你必須在安裝Android SDK的可能性服務器? – 2010-06-10 13:13:17
你想要獲取信息的郵編位於哪裏? – 2ndkauboy 2010-06-10 15:09:04
@Francesco:沒有Android SDK @ Kau-Boy:zip(apk)文件位於服務器上,使用php腳本 是否有可能在我發佈的鏈接(示例#3)中執行類似操作? – Solata 2010-06-11 06:28:50