php for android是什么版本号,PHP:如何从android.apk文件获取版本?

我创建了一组PHP函数,它们只会查找APK的版本代码。这是基于该AndroidMainfest.xml文件中包含的版本代码作为第一个标签的事实,并基于axml(二进制的Android XML格式)为described here

$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++;

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值