-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
53 additions
and
47 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 49 additions & 43 deletions
92
GetApkInfo/src/main/java/com/bihe0832/packageinfo/utils/ApkUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.