在 APK 應用商城專案中,需在用戶上傳 APK 後自動提取包名、版本號、圖標等元資料。本文記錄在 CentOS 6.x 上安裝 aapt(Android Asset Packaging Tool)並解決動態庫依賴的完整過程,供 PHP exec()/shell_exec() 調用。
將 aapt、apktool 複製至 /usr/bin/(需執行權限 chmod +x)。
| 錯誤訊息 | 缺失庫 | 解決命令 |
|---|---|---|
/lib/ld-linux.so.2: bad ELF interpreter | glibc 32-bit | yum -y install glibc.i686(或指定版本 glibc-2.12-1.132.el6_5.1.i686) |
libz.so.1: cannot open shared object file | zlib 32-bit | yum -y install zlib.i686 |
libstdc++.so.6: cannot open shared object file | libstdc++ 32-bit | yum -y install libstdc++.i686 |
若遇版本衝突,可先 yum -y install libstdc++* 再裝具體版本;或使用 --skip-broken 跳過衝突包。
# aapt dump badging your_app.apk package: name='com.example.app' versionCode='1' versionName='1.0' application-label:'Example App' application-icon-160:'res/drawable/icon.png' launchable-activity: name='com.example.app.MainActivity' label='' icon='' ...
<?php
function parseApk($apkPath) {
if (!file_exists($apkPath)) return false;
$cmd = "aapt dump badging " . escapeshellarg($apkPath) . " 2>&1";
$output = shell_exec($cmd);
if (!$output) return false;
$info = [];
// 解析關鍵字段
if (preg_match("/package: name='([^']+)' versionCode='(\d+)' versionName='([^']+)'/", $output, $m)) {
$info['package'] = $m[1];
$info['version_code'] = (int)$m[2];
$info['version_name'] = $m[3];
}
if (preg_match("/application-label:'([^']+)'/", $output, $m)) {
$info['app_name'] = $m[1];
}
if (preg_match("/application-icon-\d+:'([^']+)'/", $output, $m)) {
$info['icon_path'] = $m[1]; // APK 內部路徑,需配合 apktool 解壓提取
}
return $info;
}
// 使用
$apkInfo = parseApk('/var/www/uploads/app_v1.0.apk');
var_dump($apkInfo);
?>
百度網盤:https://pan.baidu.com/s/1pJkEqcb(含 32/64 位元版本)
AndroidManifest.xml 完整結構,建議改用 Python axmlparser 或 Node.js apk-parser,避免正則維護成本。解決 32 位元動態庫依賴是 Linux 64 位元系統運行 aapt 關鍵,完成後即可穩定為 PHP 提供 APK 元資料解析服務。