主要分三步:
private static final String BACKUP_PATH = "/sdcard/backup1/";
private static final String APK = ".apk";
private void copyApk(String name, String path) {
String dest = BACKUP_PATH + name + APK;
//path:app程序源文件路径 dest:新的存储路径 name:app名称
new Thread(new CopyRunnable(path, dest, name)).start();
}
private String getApk(String packageName) {
String appDir = null;
try {
//通过包名获取程序源文件路径
appDir = getPackageManager().getApplicationInfo(packageName, 0).sourceDir;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appDir;
}
/**
* 将程序源文件Copy到指定目录
*/
private class CopyRunnable implements Runnable {
private String source;
private String dest;
private String key;
public CopyRunnable(String source, String dest, String key) {
this.source = source;
this.dest = dest;
this.key = key;
}
@SuppressLint("StringFormatInvalid")
@Override
public void run() {
// TODO Auto-generated method stub
try {
int length = 1024 * 1024;
if (!new File(BACKUP_PATH).exists()) {
boolean mk = new File(BACKUP_PATH).mkdirs();
if (mk) {
System.out.println("true");
}
}
File fDest = new File(dest);
if (fDest.exists()) {
fDest.delete();
}
fDest.createNewFile();
FileInputStream in = new FileInputStream(new File(source));
FileOutputStream out = new FileOutputStream(fDest);
FileChannel inC = in.getChannel();
FileChannel outC = out.getChannel();
int i = 0;
while (true) {
if (inC.position() == inC.size()) {
inC.close();
outC.close();
//成功
break;
}
if ((inC.size() - inC.position()) < 1024 * 1024) {
length = (int) (inC.size() - inC.position());
} else {
length = 1024 * 1024;
}
inC.transferTo(inC.position(), length, outC);
inC.position(inC.position() + length);
i++;
}
} catch (Exception e) {
// TODO: handle exception
Log.e("TAG", e.toString());
}
}
}
调用
CharSequence label = applicationInfo.loadLabel(getPackageManager());
//百度地图_1.0.1
copyApk(label.toString() + "_" + versionName, getApk(applicationInfo.packageName));
adb shell pm list packages #打印系统中所有apk的包名
adb shell pm list packages -3 #列出除了系统应用的第三方应用包名
adb shell pm path com.xxx.xxx #进入对应包名的apk文件目录
// 成功后会出现:package:data/data/(包名)/.base.apk或者package:/system/app/xxx/xxx.apk 或者 package:/data/app/xxx/xxx.apk
// 在apk文件目录中将对应apk提取出来
adb pull /system/app/xxx/xxx.apk 或者 adb pull /data/app/xxx/xxx.apk
// 提取出来的文件在adb的配置目录下,一般是在 C:\Users\Administrator> 目录下
// apk文件位置在执行adb命令的目录
或者手机位置复制
cp apk的位置 /sdcard/xxx.apk
这里推荐几个应用
MT管理器
? 其中 免费版 提供安装包提取、分享和应用列表等大部分核心功能,付费版 可以解锁应用统计、分析与界面自定义功能
My APK
ES文件管理器
等等,好多应用都支持提取apk