Php reads apk package information and extracts application icons

Source: Internet
Author: User
Php reads apk package information and extracts application icons
In the project, you need to automatically analyze the APK package name, name, and other information after uploading the APK, extract the application icon, and return it to the server immediately. after trying many methods, it is difficult to meet the requirements. the most well-known method is ApkParser, but it cannot obtain the application ICON Information. In addition, we recommend that you use apktool to decompile the apk package, however, the speed is too slow. it takes more than five seconds to release a package, which is not suitable for real-time programs. my final solution is: 1. use aapt to read the package information aapt dump badging. /xxx.apk aapt is included in the build-tools folder of the android sdk. you can copy the file to the server and use it separately. The size is only 1.2 MB. Note that, this program is a 32-bit Environment Program. to run a 64-bit system, you may need to install about three extension packages (if not). The CentOS server can run directly, I will not elaborate on the command execution very quickly, instantly. PHP real-time return data no problem here the returned information already contains the package name \ APP name and the position of the APP icon in the apk package (similar to res /Drawable-mdpi/icon.png ). the information returned by the command line is plain text and cannot be directly used by PHP. in my PHP source code, it will be converted into an array, for ease of use 2. use unzip to directly extract the desired icon file # extract the specified image unzip from the apk package. /xxx.apk res/drawable-mdpi/icon.png-d/tmp # Move the extracted image to the desired location mv/tmp/res/drawable-mdpi/icon.png/tmp/temp.png because apk itself is just renaming the zip file, therefore, you do not need to change the name. you can decompress the package directly. in addition, unzip can be used to extract only one file in the compressed package, saving a lot of resources. 3. execute these command lines in PHP. for details, see the following source code/*** Android package handler * Class Service_Android ** @ author: wangshuai * @ website: www.enjoyphp.com * /Class Service_Android {/*** get Apk package information ** required/usr/bin/aapt ** @ param $ apkFile * @ return array */public function getApkInfo ($ apkFile) {try {exec ('/usr/bin/aapt dump badging '. $ apkFile, $ out, $ return); $ apkInfo = array (); foreach ($ out as $ line) {$ lineana = array (); $ a = explode (": ", $ line); $ key = trim ($ a [0]); $ value = trim ($ a [1]); preg_match_all ('/((? P
 
  
\ S +) = )? \'(? P
  
   
.*?) \ '/', $ Value, $ matches, PREG_SET_ORDER); foreach ($ matches as $ match) {if ($ match ['key']) {$ lineana [$ match ['key'] = $ match ['value'];} else {$ lineana [] = $ match ['value'];} $ apkInfo [$ key] [] = $ lineana;} // checkRet sorts out the configurations read above $ ret = $ this-> checkRet ($ apkInfo );} catch (Exception $ e) {echo $ e-> getMessage (); $ ret = array ();} return $ ret ;} /*** extract the specified file from the Apk package, move to the $ toFile ** @ param $ apkFile apk file * @ param $ sourceFile apk file to the corresponding file path * @ param $ toFile output file * @ return bool */function getfilefromapks ($ apkFile, $ sourceFile, $ toFile) {exec ('unzip '. $ apkFile. '$ sourceFile-d/tmp', $ out, $ return); if (rename ("/tmp /". $ sourceFile, $ toFile) {return true;} else {return false;}/*** helper function, array of Apk information processing ** @ param $ info * @ return mixed */function checkRet ($ info) {foreach ($ info as $ key => $ lineana) {if (is_array ($ lineana) {$ info [$ key] = $ this-> checkRet ($ lineana); if (count ($ info [$ key]) = 1) {$ info [$ key] = current ($ info [$ key]) ;}} else {}} return $ info ;}} example:
   GetApkInfo ($ apk); var_dump ($ res); $ packageName = $ res ['package'] ['name']; $ appName = $ res ['Application-label']; $ android-> getfilefrospark ($ apk, $ res ['application'] ['Icon '], $ iconFile ); note: 1. to run this program, PHP requires the exec permission. 2. in the apk package information obtained, the APP name may have multiple languages, and the ICON may have multiple sizes, modify the settings as needed. only the default information is described above.
  
 

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.