From android-apk file parsing, talk about user experience!

Source: Internet
Author: User

One: Scene analysis

Project an Android App admin template to start the app's add interface as follows:



operation, only to find that this operation is cumbersome, unfriendly. So check the information, see how others do, just know upload apk file, you can parse the apk file, from the APK can get version number, version name, package name, permissions, supported Android platform minimum version number, supported SDK version, recommended SDK version, An application name, a program's icon, and a series of information.


The beginning of the version, also need to manually enter the size of the APK, this user experience is simply deceptive! It should be calculated by the program!

The application type indicates whether it is a company application or another application. also do not need to enter manually, because the package name through the APK can be distinguished!

The APK name is also not required because the applicationlable app name is available through the APK!

  

We are determined to optimize the user experience and optimize the operation!

Here is the tool class for Java parsing apk:

The test output information is as follows:



Java Parsing apk Tool class:

Import Java.io.BufferedReader;
Import java.io.Closeable;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;


Import Com.saving.platform.util.PropertiesUtil;
Import Com.saving.platform.util.apk.bean.ApkInfo;
Import Com.saving.platform.util.apk.bean.ImpliedFeature;


/**
* @author lyl
* @version 1.0
*/
public class Apkutil {
public static final String Version_code = "Versioncode";
public static final String version_name = "Versionname";
public static final String sdk_version = "Sdkversion";
public static final String target_sdk_version = "Targetsdkversion";
public static final String uses_permission = "Uses-permission";
public static final String Application_label = "Application-label";
public static final String Application_icon = "Application-icon";
public static final String uses_feature = "Uses-feature";
public static final String uses_implied_feature = "Uses-implied-feature";
public static final String Supports_screens = "Supports-screens";
public static final String supports_any_density = "supports-any-density";
public static final String densities = "densities";
public static final String, package = "package";
public static final String application = "Application:";
public static final String launchable_activity = "launchable-activity";


Private Processbuilder Mbuilder;
private static final String Split_regex = "(:) | (= ') | (') | ' ";
private static final String Feature_split_regex = "(: ') | (', ') | ' ";
/**
* The directory where the AAPT is located.
*/
Private String Maaptpath = "Apk/aapt";


Public Apkutil () {
Mbuilder = new Processbuilder ();
Mbuilder.redirecterrorstream (TRUE);
}


/**
* Returns information for an APK program.
*
* @param Apkpath
* The path of the APK.
* @return Apkinfo an apk information.
*/
Public Apkinfo Getapkinfo (String apkpath) throws Exception {
String Aaptdir = Propertiesutil.getproperty ("Sysconfig.properties", "Aaptdir");
Aaptdir = Aaptdir + "/aapt";
Process process = Mbuilder.command (Aaptdir, "D", "badging", Apkpath). Start ();
InputStream is = null;
is = Process.getinputstream ();
BufferedReader br = new BufferedReader (
New InputStreamReader (IS, "UTF8"));
String tmp = Br.readline ();
try {
if (TMP = = NULL | |!tmp.startswith ("package")) {
throw new Exception ("argument is incorrect, unable to parse APK package properly. The output is: \ n "+ tmp +" ... ");
}
Apkinfo apkinfo = new Apkinfo ();
do {
Setapkinfoproperty (Apkinfo, TMP);
} while ((TMP = Br.readline ()) = null);
return apkinfo;
} catch (Exception e) {
Throw e;
} finally {
Process.destroy ();
Closeio (IS);
Closeio (BR);
}
}


/**
* Set the APK property information.
*
* @param apkinfo
* @param source
*/
private void Setapkinfoproperty (Apkinfo apkinfo, String source) {
if (Source.startswith (package)) {
Splitpackageinfo (apkinfo, source);
} else if (Source.startswith (launchable_activity)) {
Apkinfo.setlaunchableactivity (Getpropertyinquote (source));
} else if (Source.startswith (sdk_version)) {
Apkinfo.setsdkversion (Getpropertyinquote (source));
} else if (Source.startswith (target_sdk_version)) {
Apkinfo.settargetsdkversion (Getpropertyinquote (source));
} else if (Source.startswith (uses_permission)) {
Apkinfo.addtousespermissions (Getpropertyinquote (source));
} else if (Source.startswith (Application_label)) {
Apkinfo.setapplicationlable (Getpropertyinquote (source));
} else if (Source.startswith (Application_icon)) {
Apkinfo.addtoapplicationicons (Getkeybeforecolon (source),
Getpropertyinquote (source));
} else if (Source.startswith (application)) {
String[] rs = Source.split ("(icon= ') | '");
Apkinfo.setapplicationicon (Rs[rs.length-1]);
} else if (Source.startswith (uses_feature)) {
Apkinfo.addtofeatures (Getpropertyinquote (source));
} else if (Source.startswith (uses_implied_feature)) {
Apkinfo.addtoimpliedfeatures (getfeature (source));
} else {
// SYSTEM.OUT.PRINTLN (source);
}
}


Private Impliedfeature Getfeature (String source) {
String[] result = Source.split (Feature_split_regex);
Impliedfeature impliedfeature = new Impliedfeature (result[1], result[2]);
return impliedfeature;
}


/**
* Returns the value content in the format name: ' Value '.
*
* @param source
* @return
*/
private string Getpropertyinquote (string source) {
int index = Source.indexof ("'") + 1;
Return source.substring (Index, source.indexof (' \ ', index));
}


/**
* Returns the property name before the colon
*
* @param source
* @return
*/
private string Getkeybeforecolon (string source) {
Return source.substring (0, Source.indexof (': '));
}


/**
* Separate the package name, version and other information.
*
* @param apkinfo
* @param packagesource
*/
private void Splitpackageinfo (Apkinfo apkinfo, String packagesource) {
string[] PackageInfo = Packagesource.split (Split_regex);
Apkinfo.setpackagename (packageinfo[2]);
Apkinfo.setversioncode (Packageinfo[4]);
Apkinfo.setversionname (Packageinfo[6]);
}


/**
* Release resources.
*
* @param c
* Resources that will be closed
*/
Private final void Closeio (closeable c) {
if (c! = null) {
try {
C.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}


public static void Main (string[] args) {
try {
String demo = "d:/test.apk";
// if (Args.length > 0) {
// if (Args[0].equals ("-version") | | | args[0].equals ("-V") {
// System.out.println ("apkutil-by geek_soledad");
// System.out.println ("Version:" + version.getversion ());
// Return
// }
// Demo = Args[0];
// }
Apkinfo apkinfo = new Apkutil (). Getapkinfo (demo);
System.out.println (Apkinfo);
} catch (Exception e) {
E.printstacktrace ();
}
}


Public String Getmaaptpath () {
return maaptpath;
}


public void Setmaaptpath (String maaptpath) {
This.maaptpath = Maaptpath;
}
}


Get the size of the APK file:

1: Convert the file stream from apk to byte array

/**
* Data Flow byte array
*
* @param fileinputstream
* @return
*/

public static byte[] Getbytebyinputstream (InputStream fileinputstream) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
int read = 0;
byte[] bytes = new byte[1024];


try {
while (read = Fileinputstream.read (bytes))! =-1) {
if (Baos.size () > 16 * 1024 * 1024-1) {
throw new RuntimeException ("File size is larger than 16M");
}
Baos.write (bytes, 0, read);


}
} catch (IOException e) {
E.printstacktrace ();
}


byte[] array0 = Baos.tobytearray ();


return array0;


}


2: Convert byte bytes to KB or MB

 public Static String BYTES2KB (long bytes) {
BigDecimal filesize = new BigDecimal (bytes);
BigDecimal megabyte = new BigDecimal (1024 * 1024);
float returnvalue = Filesize.divide (megabyte, 2, bigdecimal.round_up)
Span style= "White-space:pre" > . Floatvalue ();
//if (ReturnValue > 1)
return (ReturnValue + "MB");
//BigDecimal kilobyte = new BigDecimal (1024);
//returnvalue = Filesize.divide (kilobyte, 2, bigdecimal.round_up)
Span style= "White-space:pre" > //. Floatvalue ();
//return (returnvalue + "KB");
}



From android-apk file parsing, talk about user experience!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.