-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInstallApkToDevices.php
51 lines (43 loc) · 1.59 KB
/
InstallApkToDevices.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
define('APK_PATH', isset($argv[1]) ? $argv[1] : false);
if (!APK_PATH) {
echo "error: 請輸入 apk 路徑!";
exit;
}
$devices = explode("\n", shell_exec("adb devices"));
$path = preg_replace("/([\w]*)InstallApkToDevices.php$/", '$1',$argv[0]);
if ($path == '') {
$path = trim(shell_exec("pwd"));
}
$zip = new ZipArchive;
if ($zip->open(APK_PATH) === TRUE) {
$zip->extractTo("$path", 'AndroidManifest.xml');
$zip->close();
} else {
echo "apk 不存在\n";
exit;
}
$AndroidManifest = shell_exec("java -jar {$path}/AXMLPrinter2.jar {$path}/AndroidManifest.xml");
$AndroidManifest = simplexml_load_string($AndroidManifest);
$package = $AndroidManifest->attributes()->package;
foreach ($AndroidManifest->application->activity as $activity) {
$category = $activity->{'intent-filter'}->category;
if ($category && $category->attributes('android', true)->name == 'android.intent.category.LAUNCHER') {
$className = $activity->attributes('android', true)->name;
break;
}
}
$pattern = "/([a-zA-Z0-9]+)\s+device/";
for ($i = 1; $i < count($devices); $i++) {
if (preg_match("/device$/", $devices[$i])) {
// 裝置編號
$deviceNum = preg_replace($pattern, "$1", $devices[$i]);
echo "----------" . $deviceNum . "----------\n";
// 安裝 apk 到指定手機
echo shell_exec("adb -s $deviceNum install -r " . APK_PATH);
// 在特定手機上執行特定程式
$adbStartAppShell = "adb -s $deviceNum shell am start -a android.intent.action.MAIN -n $package/$className";
echo shell_exec("$adbStartAppShell");
}
}
shell_exec("rm {$path}/AndroidManifest.xml");