android+Python导出安装的安卓手机应用
1、安卓代码:(收集应用名称、版本号和包名)private void collectAppInfo() {
PackageManager pm = getActivity().getPackageManager();
List<PackageInfo> list2 = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
List<AppInfoModel> allAppInfoList = new ArrayList<>();
List<AppInfoModel> androidAppInfoList = new ArrayList<>();
List<AppInfoModel> huaweiAppInfoList = new ArrayList<>();
List<AppInfoModel> thirdAppInfoList = new ArrayList<>();
for (PackageInfo packageInfo : list2) {
//得到手机上已经安装的应用的名字,即在AndriodMainfest.xml中的app_name。
String appName = packageInfo.applicationInfo.loadLabel(getActivity().getPackageManager()).toString();
//得到应用所在包的名字,即在AndriodMainfest.xml中的package的值。
String packageName = packageInfo.packageName;
String versionName = packageInfo.versionName;
AppInfoModel appInfo = new AppInfoModel();
appInfo.setAppName(appName);
appInfo.setPackageName(packageName);
appInfo.setVersionName(versionName);
allAppInfoList.add(appInfo);
if(packageName.startsWith("com.android.")) {
androidAppInfoList.add(appInfo);
} else if(packageName.startsWith("com.huawei.")) {
huaweiAppInfoList.add(appInfo);
} else {
thirdAppInfoList.add(appInfo);
}
}
Log.e("ch_test", "安卓应用数量: " + androidAppInfoList.size() + ", 华为应用数量: "
+ huaweiAppInfoList.size() + ", 第三方应用数量: " + thirdAppInfoList.size()
+ ", 所有应用数量: " + allAppInfoList.size());
LogUtils.e("ch_test", new Gson().toJson(thirdAppInfoList));
2、Python利用adb批量导出安装包:
def daochu(packageName, outputName):
try:
newCommand = 'adb shell pm path {}'.format(packageName)
# os.system(newCommand)
(status, uploadRes) = subprocess.getstatusoutput(newCommand)
packagePath = str(uploadRes)
if 'package:' in packagePath:
packagePath = packagePath
outputPath = './{}.apk'.format(outputName)
newCommand2 = 'adb pull {}{}'.format(packagePath, outputPath)
os.system(newCommand2)
# (status, uploadRes) = subprocess.getstatusoutput(newCommand2)
# print(str(uploadRes))
except Exception as e2:
print(e2)
print("output error: {}".format(packageName))
return
def allDaochu():
os.chdir('/home/xxx/yyy/apk')
data = ''
with open('package.txt', 'r') as f:
data = f.read()
print(data)
jsonData = json.loads(data)
print(len(jsonData))
for index in range(len(jsonData)):
packageName = jsonData['packageName']
appName = jsonData['appName']
versionName = jsonData['versionName']
if ' ' in appName:
appName = appName.replace(' ', '')
# print(appName)
if ' (' in versionName:
versionName = versionName.replace(' (', '[')
# print(versionName)
if ')' in versionName:
versionName = versionName.replace(')', ']')
# print(versionName)
outputName = '{}-{}'.format(appName, versionName)
print('{}: {}'.format(packageName, outputName))
daochu(packageName, outputName)
谢谢楼主分享这些源码。
非常感谢谢楼主分享这些源码。 这些源码是怎么使用的? mcoekk 发表于 2021-1-25 18:25
这些源码是怎么使用的?
第一部分写一个APP,第二部分直接python跑
页:
[1]