Full version of Java read apk, IPA package name, version name, version number and other information

Source: Internet
Author: User

有时候,我们上传apk或者是ipa文件的时候,是需要读取到里面的一些信息的,比如软件的包名,以及其版本信息等。在网上搜索了一下资料 , 找了很多版本,对于apk文件的版本号,一直读取不到,在这里,笔者自己总结了,读取apk、ipa文件的一些代码,大家可以参考下,去其糟粕,取其精华。以便适用于自己的需求。下面会提供源码给大家,我用的开发工具是eclipse,直接导入就可以,jar包也是我已经下载好的,大家可以免积分拿去。本来里面是有2个apk、2个ipa文件提供测试的,但是由于文件太大,上传不了源码,所以就删除了一个最大的ipa包

APK jar Package Address: Java parsing apk required jar pack download
IPA jar Package Address: Java Parsing IPA required jar package download

First look at my project directory

Below, please see the code

 Packagecom.zsl.cn;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJava.util.Enumeration;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipFile;ImportJava.util.zip.ZipInputStream;ImportOrg.apkinfo.api.util.AXmlResourceParser;ImportOrg.apkinfo.api.util.TypedValue;ImportOrg.apkinfo.api.util.XmlPullParser;ImportCom.dd.plist.NSDictionary;Importcom.dd.plist.NSString;ImportCom.dd.plist.PropertyListParser;/** * * @author ZSL * * * * Public Final  class readutil {    /** * Read apk * @param apkurl * @return * *     Public StaticMap<string,object>readapk(String Apkurl)        {ZipFile zipfile; map<string,object> map =NewHashmap<string, object> ();Try{ZipFile =NewZipFile (Apkurl);            Enumeration<?> enumeration = Zipfile.entries (); ZipEntry ZipEntry =NULL; while(Enumeration.hasmoreelements ()) {zipentry = (zipentry) enumeration.nextelement ();if(Zipentry.isdirectory ()) {                }Else{if("Androidmanifest.xml". Equals (Zipentry.getname (). toLowerCase ())) {Axmlresourceparser parser =NewAxmlresourceparser (); Parser.open (Zipfile.getinputstream (zipentry)); while(true) {intType = Parser.next ();if(type = = xmlpullparser.end_document) { Break; } String name = Parser.getname ();if(NULL! = name && name.tolowercase (). Equals ("Manifest")){ for(inti =0; I! = Parser.getattributecount (); i++) {if("Versionname". Equals (Parser.getattributename (i)) {String versionname = Getattributevalue (parse R, I);if(NULL= = Versionname) {Versionname =""; } map.put ("Versionname", versionname); }Else if("Package". Equals (Parser.getattributename (i)) {String PackageName = Getattributevalue (parse R, I);if(NULL= = PackageName) {PackageName =""; } map.put ("Package", PackageName); }Else if("Versioncode". Equals (Parser.getattributename (i))) {String Versioncode = getattributevalue (parser , i);if(NULL= = Versioncode) {Versioncode =""; } map.put ("Versioncode", Versioncode); }                                } Break;        }}}}} zipfile.close (); }Catch(Exception e) {Map.put ("Code","Fail"); Map.put ("Error","failed to read APK"); }returnMap }Private StaticStringGetattributevalue(Axmlresourceparser parser,intIndex) {intType = Parser.getattributevaluetype (index);intdata = Parser.getattributevaluedata (index);if(type = = typedvalue.type_string) {returnParser.getattributevalue (index); }if(type = = Typedvalue.type_attribute) {returnString.Format ("?%s%08x", Getpackage (data), data); }if(type = = Typedvalue.type_reference) {returnString.Format ("@%s%08x", Getpackage (data), data); }if(type = = Typedvalue.type_float) {returnString.valueof (float.intbitstofloat (data)); }if(type = = Typedvalue.type_int_hex) {returnString.Format ("0x%08X", data); }if(type = = Typedvalue.type_int_boolean) {returnData! =0?"true":"false"; }if(type = = typedvalue.type_dimension) {returnFloat.tostring (complextofloat (data)) + Dimension_units[data & Typedvalue.complex_unit_mask]; }if(type = = typedvalue.type_fraction) {returnFloat.tostring (complextofloat (data)) + Fraction_units[data & Typedvalue.complex_unit_mask]; }if(Type >= typedvalue.type_first_color_int && type <= typedvalue.type_last_color_int) {returnString.Format ("#%08x", data); }if(Type >= typedvalue.type_first_int && type <= typedvalue.type_last_int) {returnstring.valueof (data); }returnString.Format ("<0x%x, type 0x%02x>", data, type); }Private StaticStringGetpackage(intID) {if(ID >>> -==1) {return "Android:"; }return ""; }///////////////////////////////////illegal STUFF, DONT look:)     Public Static float complextofloat(intComplex) {return(float) (Complex &0xffffff00) * radix_mults[(complex >>4) &3]; }Private Static Final floatRadix_mults[] = {0.00390625F3.051758E-005F1.192093E-007F4.656613E-010F};Private Static FinalString dimension_units[] = {"px","Dip","SP","PT","in","MM","",""};Private Static FinalString fraction_units[] = {"%","%p","","","","","",""};/** * Read IPA * /     Public StaticMap<string,object>Readipa(String Ipaurl) {Map<string,object> Map =NewHashmap<string,object> ();Try{File File =NewFile (Ipaurl); InputStream is =NewFileInputStream (file); Zipinputstream Zipins =NewZipinputstream (IS);            ZipEntry ze; InputStream Infois =NULL; while((Ze = zipins.getnextentry ())! =NULL) {if(!ze.isdirectory ()) {String name = Ze.getname ();if(NULL! = name && name.tolowercase (). Contains ("Info.plist") {Bytearrayoutputstream _copy =NewBytearrayoutputstream ();intChunk =0;byte[] data =New byte[1024x768]; while(-1! = (chunk=zipins.read (data))) {_copy.write (data,0, chunk); } Infois =NewBytearrayinputstream (_copy.tobytearray ()); Break; }}} Nsdictionary rootdict = (nsdictionary) property Listparser.parse (Infois);////////////////////////////////////////////////////////////////            //If you want to see what keys you have, you can release the comments below//For (String KeyName:rootDict.allKeys ()) {//System.out.println (KeyName + ":" + rootdict.get (keyName). toString ());//            }            //Application package nameNSString parameters = (nsstring) rootdict.get ("Cfbundleidentifier"); Map.put ("Package", parameters.tostring ());//Application version nameParameters = (nsstring) Rootdict.objectforkey ("Cfbundleshortversionstring"); Map.put ("Versionname", parameters.tostring ());//Application version numberParameters = (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","failed to read IPA file"); }returnMap } Public Static void Main(string[] args) {System.out.println ("======apk========="); String Apkurl ="src/shenmiaotaowang_966.apk"; map<string,object> mapapk = readutil.readapk (Apkurl); for(String Key:mapApk.keySet ()) {System.out.println (key +":"+ mapapk.get (key)); } System.out.println ("======ipa=========="); String Ipaurl ="Src/im.ipa"; map<string,object> Mapipa = Readutil.readipa (Ipaurl); for(String Key:mapIpa.keySet ()) {System.out.println (key +":"+ mapipa.get (key)); }    }}

The following is a copy of the source code: Java read apk, IPA source full version





Full version of Java read apk, IPA package name, version name, version number and other information

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.