Last active
July 20, 2022 10:08
-
-
Save jarkko-hautakorpi/a1b28ff1eeab49c969ffe821f39f7b86 to your computer and use it in GitHub Desktop.
Download all APK files from android device
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$list = "adb shell pm list packages"; | |
$path = "adb shell pm path %s"; // adb shell pm path com.example.someapp | |
$get = "adb pull %s %s"; // adb pull /data/app/com.example.someapp-2.apk path/to/desired/destination | |
$apkdir = getcwd() . "/apk"; | |
if (!is_dir($apkdir)) { | |
if (mkdir($apkdir)) { | |
echo "Created directory $apkdir\n"; | |
} else { | |
echo "Could not create directory $apkdir\n"; | |
exit(1); | |
} | |
} | |
$out = null; | |
$out = shell_exec($list); | |
$out = explode("\n", $out); | |
foreach ($out as $line) { | |
$line = trim($line); | |
if (strlen($line) == 0) continue; | |
$lineArr = explode(":", $line); | |
echo $line . PHP_EOL; | |
$package = $lineArr[1]; | |
if (is_array($package)) { | |
echo "Package is an array" . PHP_EOL; | |
continue; | |
} | |
$pathLine = sprintf($path, $package); | |
$pathLine = trim(shell_exec($pathLine)); | |
$isMulti = explode("\n", trim($pathLine)); | |
if (count($isMulti) > 1) { | |
echo "Package" . $line . " is multi-part" . PHP_EOL; | |
foreach ($isMulti as $pathChild) { | |
downloadApkPath($pathChild, $package, $apkdir, $get); | |
} | |
} else { | |
downloadApkPath($pathLine, $package, $apkdir, $get); | |
} | |
} | |
function downloadApkPath($pathArray, $package, $apkdir, $get) | |
{ | |
$pathArray = explode(":", trim($pathArray)); | |
if (count($pathArray) == 2) { | |
$path = trim($pathArray[1]); | |
$nameArr = explode("/", $path); | |
$name = end($nameArr); | |
$app = $apkdir . "/" . $package . "-" . $name; | |
$command = sprintf($get, $path, $app); | |
echo $command . PHP_EOL; | |
shell_exec($command); | |
} else { | |
echo "Error: path not found:" . $pathArray . PHP_EOL; | |
exit(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment