PHP read APK package information, extract app icon

Source: Internet
Author: User
The project needs to automatically analyze the APK package name \ Name and other information after uploading the APK, and extract the app icon, and immediately return to the server. Tried many methods, it is difficult to meet the demand, the most well-known should be apkparser, but it can not get application icon information; It is also recommended to use Apktool for anti-compilation APK package, but the speed is too slow, the solution of a packet about 5 seconds or more, not suitable for real-time program. My final plan is: 1 read the package information with AAPT AAPT dump badging./xxx.apkaapt included on Android The Build-tools folder in the SDK, you can copy this file to the server alone, the size of only 1.2M, it is important to note that the program is a 32-bit environment program, 64-bit system operation may need to install about three expansion packs (if not itself), My server CentOS can be run directly, without elaborating on the execution of the command quickly and instantaneously. PHP Real-time return data no problem. The information returned here already contains the package name \app name and the location of the app icon in the APK package (similar to res/drawable-mdpi/icon.png). The command line returns information that is plain text and PHP cannot be used directly. I will be in the following PHP source code will be converted to an array, to facilitate the use of 2 unzip directly extract the required icon file # Unzip the APK package to specify the image unzip./xxx.apk res/drawable-mdpi/icon.png-d/tmp# Move the extracted image to the location we need mv/tmp/res/drawable-mdpi/icon.png/tmp/temp.png because the APK itself is a zip file rename, so no renaming, you can directly unzip. Another unzip can specify that only one of the files in the compressed package is decompressed, and also saves a lot of resources. 3 Finally, the command line is executed in PHP with exec, see the following source/*** Android Package Handler * Class service_android** @author: wangshuai* @website: Www.enjoyphp.com*/class service_android{/** * Get APK Package Info * * Requires/USR/BIN/AAPT * * @par AM $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 will tidy up the configuration read out $ret = $this->checkret ($apkInfo);        } catch (Exception $e) {echo $e->getmessage ();        $ret = Array ();    } return $ret; /** * Extract the specified file from the APK package and move to $tofile * * @param $apkFile apk file * @param $sourceFile the corresponding file path in the apk file * @para M $toFile output File * @return bool */function getfilefromapk ($apkFile, $sourceFile, $toFile) {exec (' unzip ' . $apkFile.        ' $sourceFile-D/tmp ', $out, $return);        if (rename ("/tmp/". $sourceFile, $toFile)) {return true;        } else {return false;  }}/** * helper function, processing apk information array * * @param $info * @return Mixed      */function Checkret ($info) {foreach ($info as $key = = $lineana) {if (Is_array ($lin        Eana) {$info [$key] = $this->checkret ($lineana);        if (count ($info [$key]) = = 1) {$info [$key] = current ($info [$key]);    }} else {}} return $info; }} A sample of use:
   Getapkinfo ($apk); Var_dump ($res); $packageName = $res [' package '] [' name ']; $appName = $res [' Application-label ']; $android->getfilefromapk ($apk, $res [' Application '] [' icon '], $iconFile); Note: 1 Running the program requires PHP to have EXEC permission 2 to get the APK package information, The app name may be in a multi-lingual name, and icon will have multiple sizes; If you need to, please modify it as necessary, only the use of 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.