( @Parameters(paramLabel = "packageName", description = "Package to download") String package_name )
| 173 | * and put it in a folder with the same name as [package_name] |
| 174 | */ |
| 175 | @Command(name = "download", description = "Download an apk from device") |
| 176 | void Download( |
| 177 | |
| 178 | @Parameters(paramLabel = "packageName", description = "Package to download") String package_name |
| 179 | |
| 180 | ) { |
| 181 | String downloadDir = package_name; |
| 182 | // if folder with name [package_name] exist |
| 183 | // then remove it and recreate an empty one |
| 184 | File downloadFile = new File(downloadDir); |
| 185 | if (downloadFile.exists() && downloadFile.isDirectory()) { |
| 186 | try { |
| 187 | System.out.printf("directory %s exist, removing it...\n", downloadDir); |
| 188 | FileUtils.deleteDirectory(downloadFile); |
| 189 | } catch (IOException e) { |
| 190 | System.out.printf("Error while deleting directory \n"); |
| 191 | System.out.println(e.getMessage()); |
| 192 | } |
| 193 | } |
| 194 | // create dir for storing downloaded apk |
| 195 | (new File(downloadDir)).mkdirs(); |
| 196 | System.out.printf("created directory %s for storing downloaded apk\n", downloadDir); |
| 197 | |
| 198 | // |
| 199 | Adb adb = new Adb(); |
| 200 | // check if [package_name] exists |
| 201 | Adb.Output out = adb.ListApk(); |
| 202 | if (out.error != Adb.Error.ok) { |
| 203 | ShowAdbShellError(out); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | if (!out.strings.contains(package_name)) { |
| 208 | System.out.printf("package %s doesn't exist in the device\n", package_name); |
| 209 | System.out.println("use listApk command to list installed packages"); |
| 210 | return; |
| 211 | |
| 212 | } |
| 213 | out = adb.GetApkPathAtDevice(package_name); |
| 214 | if (out.error != Adb.Error.ok) { |
| 215 | ShowAdbShellError(out); |
| 216 | return; |
| 217 | } |
| 218 | // we need to loop when downloading the app |
| 219 | // in case the apk is splitted apks (have multiple paths) |
| 220 | System.out.println("Downloading apks ..."); |
| 221 | for (int i = 0; i < out.strings.size(); i++) { |
| 222 | |
| 223 | String apkPath = out.strings.get(i); |
| 224 | System.out.printf("Downloading apk (%d/%d) at %s", i + 1, out.strings.size(), apkPath); |
| 225 | Adb.Output downloadOut = adb.DownloadApk(apkPath, downloadDir); |
| 226 | if (downloadOut.error != Adb.Error.ok) { |
| 227 | ShowAdbShellError(out); |
| 228 | return; |
| 229 | } |
| 230 | downloadOut.strings.forEach(System.out::println); |
| 231 | System.out.printf("...done\n"); |
| 232 | } |
nothing calls this directly
no test coverage detected