I. Introduction
Apk Parser
It is an excellent decoding binary files and access a variety of information apk open source library , with many useful features:
- Apk get various metadata information, such as application name, icon, package name, version number, etc.
- Parsing binary XML file and convert it to text format
- Dex obtain class information from documents, such as: class name, modifiers, etc.
- Apk obtain signature information, including v1, v2 signature
However,
Apk Parser
both the author of the open source library or some online tutorials, just say java se how to use the open source library android tutorial does not depend on the library, so this may be a whole network alone android callApk Parser
to Oh, parse apk tutorial
Second, the Apk Parser
need to modify the part
1, to obtain apk-parser dependent libraries
- To get through Gradle
Maven Central repo download link from open source libraries provided by the authors:
<dependency>
<groupId>net.dongliu</groupId>
<artifactId>apk-parser</artifactId>
<version>2.6.9</version>
</dependency>
It can be concluded, the build system to download the configuration dependent libraries use gradle in Android Studio: implementation 'net.dongliu:apk-parser:2.6.9'
- or the entire project downloaded directly from github down, integrated into your project go
2, after the download of the project down from the GitHub, the /src/main/resources/
two directory .ini
copy the file to assets
the directory
3, modified Apk-parser reads these two .ini embodiment, the global search, the search can be in net.dongliu.apk.parser.utils.ResourceLoader
this class, the original code is:
private static BufferedReader toReader(String path) {
return new BufferedReader(new InputStreamReader(
ResourceLoader.class.getResourceAsStream(path)));
}
change into:
private static BufferedReader toReader(String path) {
InputStream inputStream = null;
try {
Context context = ActivityLifecycle.curActivityRef.get();
if(context != null){
inputStream = context.getAssets().open(path);
}
} catch (IOException e) {
e.printStackTrace();
}
return new BufferedReader(new InputStreamReader(inputStream));
}
After the configuration of the above steps, android project will be able to run normally
Third, using the tutorial
1, the metadata acquiring apk
ApkFile apkFile = new ApkFile(apkInfo.getApkPath());
apkFile.setPreferredLocale(Locale.SIMPLIFIED_CHINESE);
ApkMeta apkMeta = apkFile.getApkMeta();
apkInfo.setLabel(apkMeta.getLabel());
apkInfo.setPackageName(apkMeta.getPackageName());
apkInfo.setVersionName(apkMeta.getVersionName());
apkInfo.setVersionCode(String.valueOf(apkMeta.getVersionCode()));
apkInfo.setTargetSdkVersion(apkMeta.getTargetSdkVersion());
apkInfo.setMinSdkVersion(apkMeta.getMinSdkVersion());
2, parse and acquire content AndroidManifest.xml and other xml resource files
try (ApkFile apkFile = new ApkFile(new File(filePath))) {
String manifestXml = apkFile.getManifestXml();
String xml = apkFile.transBinaryXml("res/menu/main.xml");
}
3, to get a class of information dex
DexClass[] dexClasses = apkFile.getDexClasses();
if(dexClasses != null && dexClasses.length > 0) {
for(DexClass dexClass : dexClasses){
if(dexClass.getClassType().equals("Lcom/ss/android/common/lib/EventUtils;")){
apkInfo.setHasTodayTopClass(true);
}else if(dexClass.getClassType().equals("Lcom/qq/gdt/action/GDTAction;")){
apkInfo.setHasGdtClass(true);
}else if(dexClass.getClassType().equals("Lcom/alibaba/sdk/android/push/noonesdk/PushServiceFactory;")){
apkInfo.setHasAlipushClass(true);
}else if(dexClass.getClassType().equals("Lcom/tendcloud/tenddata/TCAgent;")){
apkInfo.setHasTalkingDataClass(true);
}
}
}
4, obtain signature information apk
List<ApkSigner> apkSingers = apkFile.getApkSingers();
if(apkSingers != null && apkSingers.size()>0) {
for (ApkSigner apkSigner : apkSingers) {
List<CertificateMeta> certificateMetas = apkSigner.getCertificateMetas();
for (CertificateMeta certificateMeta : certificateMetas) {
Log.i("min77", certificateMeta.getCertMd5());
if (!TextUtils.isEmpty(certificateMeta.getCertMd5())) {
String desc = signatures.optString(certificateMeta.getCertMd5());
if(!TextUtils.isEmpty(desc)){
apkInfo.setSignatureMd5(certificateMeta.getCertMd5()+"("+desc+")");
}else {
apkInfo.setSignatureMd5(certificateMeta.getCertMd5());
}
}
}
}
}
5, set the preferred Locales
Apk may have different information (title, icon, etc.) for different regions and languages, or we could call Locale. If the locale is not set, the default is "en_US" locale (Locale.US). You can set a preferred locale in the following ways
try (ApkFile apkFile = new ApkFile(new File(filePath))) {
apkFile.setPreferredLocale(Locale.SIMPLIFIED_CHINESE);
ApkMeta apkMeta = apkFile.getApkMeta();
}