Skip to content

Commit

Permalink
修复输入流没有关闭问题
Browse files Browse the repository at this point in the history
  • Loading branch information
bihe0832 committed Mar 1, 2021
1 parent 58195e5 commit 575de60
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 47 deletions.
Binary file modified GetAPKInfo.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion GetApkInfo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jar{
//项目名,也是生成的jar的名字
baseName = "GetAPKInfo"
//项目版本号,这部分内容会写进manifest
version = "1.0"
version = "2.0.2"
//项目的manifest定义,其中就包含最关键的入口类定义
manifest {
attributes 'Main-Class': 'com.bihe0832.packageinfo.Main'
Expand Down
6 changes: 3 additions & 3 deletions GetApkInfo/src/main/java/com/bihe0832/packageinfo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

public class Main {

private static final int VERSION_CODE = 7;
private static final String VERSION_NAME = "2.0.1";
private static final int VERSION_CODE = 8;
private static final String VERSION_NAME = "2.0.2";
private static final String HELP_PAGE_GENERAL = "help.txt";
private static final String VERSION_PAGE_GENERAL = "help_version.txt";
private static boolean sShowDebug = true;
Expand Down Expand Up @@ -52,7 +52,7 @@ public static void main(String[] params) throws Exception {
private static void getApkInfo(String filePath){
ApkInfo info = new ApkInfo();
try {
ApkUtil.getApkInfo(filePath, info, sShowDebug);
ApkUtil.updateAPKInfo(filePath, info, sShowDebug);
} catch(Exception e){
showFailedCheckResult(RET_GET_INFO_BAD,"get apkinfo failed, throw an Exception ;please use --debug get more info");
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,63 @@
package com.bihe0832.packageinfo.utils;

import com.bihe0832.packageinfo.bean.ApkInfo;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;


import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import com.bihe0832.packageinfo.bean.ApkInfo;

import java.util.Iterator;
import java.util.List;


public class ApkUtil {

private static final Namespace NS = Namespace.getNamespace("http://schemas.android.com/apk/res/android");

public static void getApkInfo(String apkPath, ApkInfo info, boolean showException){
SAXBuilder builder = new SAXBuilder();
Document document = null;
try{
InputStream stream = new ByteArrayInputStream(AXMLPrinter.getManifestXMLFromAPK(apkPath).getBytes(StandardCharsets.UTF_8));
document = builder.build(stream);
}catch (Exception e) {
if(showException){
e.printStackTrace();
}
}
Element root = document.getRootElement();
info.versionCode = root.getAttributeValue("versionCode",NS);
info.versionName = root.getAttributeValue("versionName", NS);
String s = root.getAttributes().toString();
String c[] = s.split(",");
for(String a: c){
if(a.contains("package")){
info.packageName = a.substring(a.indexOf("package=\"")+9, a.lastIndexOf("\""));
}
}

List booklist=root.getChildren("uses-sdk");
Element book = (Element) booklist.get(0);
info.minSdkVersion = book.getAttributeValue("minSdkVersion", NS);
info.targetSdkVersion = book.getAttributeValue("targetSdkVersion", NS);

booklist=root.getChildren("uses-permission");
for (Iterator iter = booklist.iterator(); iter.hasNext();) {
Element tempBook = (Element) iter.next();
info.permissions.add(tempBook.getAttributeValue("name", NS));
}
}

private static final Namespace NS = Namespace.getNamespace("http://schemas.android.com/apk/res/android");

public static void updateAPKInfo(String apkPath, ApkInfo info, boolean showException) {
SAXBuilder builder = new SAXBuilder();
Document document = null;
InputStream stream = null;
try {
stream = new ByteArrayInputStream(
AXMLPrinter.getManifestXMLFromAPK(apkPath).getBytes(StandardCharsets.UTF_8));
document = builder.build(stream);
Element root = document.getRootElement();
info.versionCode = root.getAttributeValue("versionCode", NS);
info.versionName = root.getAttributeValue("versionName", NS);
String s = root.getAttributes().toString();
String c[] = s.split(",");
for (String a : c) {
if (a.contains("package")) {
info.packageName = a.substring(a.indexOf("package=\"") + 9, a.lastIndexOf("\""));
}
}

List booklist = root.getChildren("uses-sdk");
Element book = (Element) booklist.get(0);
info.minSdkVersion = book.getAttributeValue("minSdkVersion", NS);
info.targetSdkVersion = book.getAttributeValue("targetSdkVersion", NS);

booklist = root.getChildren("uses-permission");
for (Iterator iter = booklist.iterator(); iter.hasNext(); ) {
Element tempBook = (Element) iter.next();
info.permissions.add(tempBook.getAttributeValue("name", NS));
}
} catch (Exception e) {
if (showException) {
e.printStackTrace();
}
} finally {
if (null != stream) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Binary file modified GetMoreAPKInfo.jar
Binary file not shown.

0 comments on commit 575de60

Please sign in to comment.