"Java" Project reads version information for apk files and IPA files

Source: Internet
Author: User

In a company project, when versioning is done, you encounter situations where you need to identify the managed apk (Android application package) and IPA (Apple application package), and you need to identify and import some of the package's information into the database and manage it when uploading.

first, how to identify information


When I started working on this solution, my colleague told me that there was a ready-made tool class on the web that could read information about a variety of installation packages directly. So the internet search a bit, and sure enough solution, not to say good or bad, from the solution to the idea, you can find that their solutions are stereotyped.

Like many projects, there are a lot of version information on Apple and Android projects, so where are they stored? In each of our packed bags, there is actually a special note version information and copyright, description of the file, if we use WinRAR or other decompression software to unzip the installation package, we can get a lot of information inside, Then generally we will find an. xml file in the outermost layer of the software to see our version information (usually encrypted).

So the source of our version information is to extract the XML files from the installation package and then get the information we need by parsing the special nodes in the XML file. The idea is to unpack (like a zip)-> read the file-> put the information into the structured entity-> get the data from the entity

Since the Androidstudio and SDK used in various versions of Android are different, we will have a slight difference in parsing, which is compatible, and is handled for various versions of the identification Web.


Second, start


Before we start, we need to introduce two jar packages, namely Dd-plist-1.16.jar (iOS identification) and Apk-parser-2.5.3.jar (Android identification).

Download Address: https://download.csdn.net/download/tr1912/10383172

We can just put it in the Lib folder when we introduce the jar package. MAVEN's words need to be uploaded to the central warehouse before they can be mounted.

Then create a new tool class called Apkutil, and then glue to the following code:

Import Java.io.File;
Import java.io.IOException;
Import Java.util.HashMap;


Import Java.util.Map;
Import Net.dongliu.apk.parser.ApkFile;
Import Net.dongliu.apk.parser.bean.ApkMeta;
  
Import Net.dongliu.apk.parser.bean.UseFeature; public class Apkutil {    public static void main (string[] arg) {String FilePath = "D:\\apk\\farmcli

        ENT.APK ";
            Try (apkfile apkfile = new Apkfile (new File (FilePath)) {Apkmeta Apkmeta = Apkfile.getapkmeta ();
            System.out.println (Apkmeta.getname ());
            System.out.println (Apkmeta.getpackagename ());
            System.out.println (Apkmeta.getversioncode ());
            System.out.println (Apkmeta.getversionname ());
            For (Usefeature feature:apkMeta.getUsesFeatures ()) {System.out.println (Feature.getname ());
        } catch (IOException e) {e.printstacktrace ();
}    &nbsp}     /**    * Get apk information-Wang Yu-April 18, 2018 13:05:52     * @param apkurl     * @return     */    public static map<string,object> readApk (String
	Apkurl) {map<string,object> resmap=new hashmap<string,object> ();
            Try (apkfile apkfile = new Apkfile (new File (Apkurl)) {Apkmeta Apkmeta = Apkfile.getapkmeta ();
            Resmap.put ("Versioncode", Apkmeta.getversioncode ());      
            Resmap.put ("Versionname", Apkmeta.getversionname ());
            For (Usefeature feature:apkMeta.getUsesFeatures ()) {System.out.println (Feature.getname ());
        } catch (IOException e) {e.printstacktrace ();
}         return Resmap; &NBSP;&NBSP;&NBSP;&NBSP}}

Where filepath for us to put the path of the apk file, and then run directly, we can see the version we want to see information:

The picture above is I identified a QQ installation APK appearance.

IPA file Similarly, after the introduction of the jar package, write the following code:

/** * Read IPA */public static map<string,object> Readipa (String ipaurl) {map<string,object> Map = new has
    Hmap<string,object> ();
        try {File File = new file (Ipaurl);
        InputStream is = new FileInputStream (file);
        InputStream Is2 = new FileInputStream (file);
        Zipinputstream zipins = new Zipinputstream (IS);
        Zipinputstream zipIns2 = new Zipinputstream (IS2);
        ZipEntry ze;
        ZipEntry ze2;
        InputStream Infois = null;
        Nsdictionary rootdict = null;
        String icon = null; while (ze = zipins.getnextentry ())!= null) {if (!ze.isdirectory ()) {String name = Ze.getna
                Me (); if (null!= name && name.tolowercase (). Contains (". App/info.plist")) {Bytearr
                    Ayoutputstream _copy = new Bytearrayoutputstream ();
                    int chunk = 0; byte[] data = new byte[1024];
                    while ( -1!= (Chunk=zipins.read (data)) {_copy.write (data, 0, chunk);
                    } Infois = new Bytearrayinputstream (_copy.tobytearray ());

                    Rootdict = (nsdictionary) propertylistparser.parse (Infois);
                    We can get whatever we need according to the info.plist structure. For example, I get the icon name, the directory structure of the icon, please follow the picture//Get icon Name

                    Nsdictionary icondict = (nsdictionary) rootdict.get ("Cfbundleicons");
                        while (null!= icondict) {if (Icondict.containskey ("Cfbundleprimaryicon")) { 
                            Nsdictionary Cfbundleprimaryicon = (nsdictionary) icondict.get ("Cfbundleprimaryicon"); if (Cfbundleprimaryicon.containskey ("Cfbundleiconfiles")) {Nsarray cfbundleiconfiles = (nsarray) 
                            Cfbundleprimaryicon.get ("Cfbundleiconfiles"); icon = Cfbundleiconfiles.getaRray () [0].tostring ();
                                if (Icon.contains (". png")) {icon = Icon.replace (". png", "");
                                System.out.println ("Get Icon Name:" + icon);
                            Break
                }} break;
            Download icon file from icon name to specified location while (ze2 = Zipins2.getnextentry ())!= null) {
                if (!ze2.isdirectory ()) {String name = Ze2.getname ();
                SYSTEM.OUT.PRINTLN (name);
                    if (Name.contains (Icon.trim ())) {System.out.println (11111);
                       FileOutputStream fos = new FileOutputStream (New File ("E:\\img\\icon.png"));
                       int chunk = 0;
                       byte[] data = new byte[1024];
    while ( -1!= (Chunk=zipins2.read (data)) {                       Fos.write (data, 0, chunk);
                    } fos.close ();
                Break  }}//////////////////////////////////////////////////////////////////If you want to see which key , you can release the comments below//for (String KeyName:rootDict.allKeys ()) {//System.out.println (KeyName + ":" + rootdict.ge
T (KeyName). toString ());
        }//Application package name NSString parameters = (nsstring) rootdict.get ("Cfbundleidentifier");
        Map.put ("Package", parameters.tostring ());
        Application Version Name parameters = (nsstring) rootdict.objectforkey ("cfbundleshortversionstring");
        Map.put ("Versionname", parameters.tostring ());
        Application Version Number parameters = (nsstring) rootdict.get ("Cfbundleversion");

        Map.put ("Versioncode", parameters.tostring ());
        Infois.close ();
        Is.close ();

 Zipins.close ();   catch (Exception e) {map.put ("code", "fail");
    Map.put ("Error", "Read IPA file failed");
} return map; }

The test code in main:

System.out.println ("======ipa==========");
String Ipaurl = "D:\\apk\\pasturerecreation.ipa";
map<string,object> Mapipa = Apkutil.readipa (ipaurl);
For (String Key:mapIpa.keySet ()) {
System.out.println (key + ":" + mapipa.get (key));
}

IPA documents are more complex, but can also parse out a lot of information, because the information is similar, so no longer demo, their own experience to go.

Another iOS package can parse out the application of the icon, you can try.

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.