Php automatically extracts apk package information after uploading the apk (sample download)

Source: Internet
Author: User

The first project to enter the company is to create a market. Therefore, the background must upload APK software. For convenience, after an APK is uploaded, the system automatically extracts information about the APK file, such as the apk package name, product name, version information, APK Code, program size, and ICON. Initial Processing Method

Run the following command:Java-jar AXMLPrinter2.jar AndroidManifest. xml> cmdAfter. xml
Obtain the cmdAfter. xml file, and analyze the cmdAfter. xml file to obtain relevant information.

Unfortunately, the apk package name can be obtained from this file, but the ico icon file name and other related information cannot be obtained. As shown in

, Such as label, icon, and so on are all flag values, and the expected results cannot be obtained directly. I have analyzed the relationship between this value and the files in the APK file, but different APK structures make implementation too troublesome. In fact, when you upload an APK, you can extract the APK package name, ICON, size, and other information from some websites such as the security market. Therefore, when others can implement it, I want to solve it. So after research, expected results are obtained. Here, we will record the method. Please contact us.

Core extract APK Information Code
Copy codeThe Code is as follows :/***
* Analyze the uploaded APK file and extract the required data
*/
Function upAPK (){
Global $ _ config_product_apktool_count; // The number of times apktool. jar is decompressed. The reasons are described below.
If ($ this-> msg! = '') Return; // if an error exists, return
$ Dir = $ this-> upload_path; // upload path
$ StringsXML_exists = false;
If (file_exists ($ dir. 'package/res/values/strings. xml') unlink ($ dir. 'package/res/values/strings. xml ');
For ($ I = 0; $ I <$ _ config_product_apktool_count &&! $ StringsXML_exists; $ I ++ ){
// For the uc apk package or its similar APK package, unzipping once does not completely obtain the strings. xml file or related files. Currently, this method is only used.
// Run java-jar... In the cmd system to decompress the package. Sometimes the strings. xml file can be obtained, and sometimes the file cannot be obtained.
Exec ('java-jar .. /apktool. jar d-F '. $ this-> tmpFile. ''. $ dir. 'package'); // note: the package is decompressed and then executed.
$ StringsXML_exists = file_exists ($ dir. 'package/res/values/strings. xml ');
}
// Check whether the AndroidManifest. xml file exists. If it does not exist, it is not a legal APK File
If (! File_exists ($ dir. 'package/AndroidManifest. xml') {$ this-> msg = 'is not a legal APK file. please upload it again! '; Return ;}
$ AndroidManifestXML = file_get_contents ($ dir. 'package/AndroidManifest. xml'); // read AndroidManifest. xml

If (preg_match ('/package = \ "([^ \"] *) \ "/I', $ AndroidManifestXML, $ package )) $ returnVal ['package'] = $ package [1]; // if there is a package name, return to the array

// Add versionCode
If (preg_match ('/versionCode = \ "([^ \"] *) \ "/I', $ AndroidManifestXML, $ versionCode )) $ returnVal ['versioncode'] = $ versionCode [1]; // If any version code exists, return to the array

// Check whether the database already exists after detecting the package name.
If ($ this-> id = 0) {// detect when adding a new product. Modify the product to not detect
If ($ returnVal ['package']! = ''){
$ SQL = 'select id from product where package = '. SqlEncode ($ package [1]);
$ Result = mysql_query ($ SQL );
If (mysql_num_rows ($ result)> 0 ){
$ This-> msg = 'this APK already exists. Please change it! ';
Return;
}
} Else {
$ This-> msg = 'the system cannot detect the APK information. Please contact the administrator! ';
Return;
}
}

If ($ stringsXML_exists) $ stringXML = file_get_contents ($ dir. 'package/res/values/strings. xml'); // if strings. xml exists, the strings. xml file is read.
If (preg_match ('/versionName = \ "([^ \"] *) \ "/I', $ AndroidManifestXML, $ ver )) $ returnVal ['ver '] = $ ver [1]; // If a version number exists, return to the array
// There are currently two types of version numbers: 1. The version numbers are listed directly in AndroidManifest. xml. The above regular expressions can be extracted.
// 2. the version number is the same as the label and put it in the strings. xml file.
// 2011-11-23 add
If ($ stringXML! = ''& Strstr ($ ver [1], '@') {
If (preg_match ('/^ @ string \/(. *)/I', $ ver [1], $ findVer )){
If (preg_match ('/<string name = \"'. $ findVer [1]. '\ "> ([^ <] *) <\/string>/', $ stringXML, $ a) $ returnVal ['ver '] = $ a [1];
}
}
//////////////////////////////////////// ////
If (preg_match ('/<application [\ s \ S] *? Android: icon = "@ drawable \/([^"] *) "/I ', $ AndroidManifestXML, $ icon )) $ returnVal ['thumbimg '] = $ icon [1]; // If an icon exists, return to the array
If ($ stringsXML_exists & preg_match ('/<application [\ s \ S] *? Android: label = "@ string \/([^"] *) "/I ', $ AndroidManifestXML, $ label )){
If (preg_match ('/<string name = \"'. $ label [1]. '\ "> ([^ <] *) <\/string>/', $ stringXML, $ name )){
$ ReturnVal ['name'] = $ name [1]; // if there is a product name, return to the array
/**
Baidu: strings. xml
Special Case 1: <string name = "app_name"> "pocket Baidu" </string>
*/
$ ReturnVal ['name'] = preg_replace ('/\ s | "/','', $ returnVal ['name']);
}
}
// $ This-> msg = $ returnVal ['package']. '--'. $ returnVal ['ver ']. '--'. $ returnVal ['thumbimg ']. '--'. $ returnVal ['name'];
If ($ this-> oldAPK! = '') {// Delete the original APK file and icon.png image after re-Uploading
Unlink ($ dir. $ this-> oldAPK );
Unlink ($ dir. $ this-> oldAPK.'.png ');
}
// Traverse the directory [drawable | drawable-hdpi | drawable-nodpi | drawable-ldpi | drawable-mdpi] under the package/res directory
// The system obtains the icon with the largest icon size.
$ TmpArr [0] = 0; $ tmpArr [1] = 0; $ tmpArr [2] = 'drawable ';
$ Dirs = opendir ($ dir. 'package/res ');
While ($ file = readdir ($ dirs ))){
Preg_match ('/(drawable (-.*? Dpi )?) /I ', $ file, $ drawable_folder );
$ IconPath = $ dir. 'package/res/'. $ drawable_folder [1].'/'.{returnval}'thumbimg'{.'.png ';
If (file_exists ($ iconPath )){
$ IconInfo = getimagesize ($ iconPath );
If ($ iconInfo [0]> $ tmpArr [0] & $ iconInfo [1]> $ tmpArr [1]) {
$ TmpArr [0] = $ iconInfo [0]; $ tmpArr [1] = $ iconInfo [1]; $ tmpArr [2] = $ drawable_folder [1];
}
}
}
// $ This-> msg = $ iconInfo [0]. '---'. $ iconInfo [1];
Closedir ($ dirs );
If (rename ($ dir. 'package/res /'. $ tmpArr [2]. '/'.{returnval}'thumbimg'{.'.png', $ dir. $ this-> iframe_key.'.apk.png ') {// locate the Directory and move it
$ ReturnVal ['thumbimg '] = $ this-> iframe_key.'.apk.png ';
}
If (! Move_uploaded_file ($ this-> tmpFile, $ dir. $ this-> iframe_key.'.apk ') {$ this-> msg = 'upload failed! '; Return;} // transfer the apk File
$ ReturnVal ['filename'] = $ this-> iframe_key.'.apk ';
$ ReturnVal ['SIZE'] = $ this-> size;
$ This-> result = $ returnVal;
}

Information Extraction Process

1. First, use the apktool. jar command to extract the package/res/values/string. xml file from the apk file. For some reason, sometimes the string. xml file is not necessarily obtained when the apk file is released. Therefore, the $ _ config_product_apktool_count parameter is added to the backend to control the maximum number of releases.

2. Read and release the AndroidManifest. xml file under the root directory. You can obtain the APK package name and version information from this file.

3. Check whether the package name exists in the database for the newly uploaded APK. It is forbidden to upload APK with the same package name. Not detected during modification.

4. obtain the required information through regular expressions.

Why extract the string. xml file?

Because not all information is in AndroidManifest. xml. Some information is only used as a reference in AndroidManifest. xml, and the actual record is in string. xml. For example

The Label and icon values in AndroidManifest. xml.

Medium: label = "@ string/app_name" indicates that. in xml, the value of the string name attribute is app_name, that is, the "software name" of the APK. Here is "Market", as shown in:

@ Drawable/quickflick_icon, which indicates that quickflick_icon is the name of the ICON file.

For special needs, I need to find the largest ICON. See the following code:

Copy codeThe Code is as follows: // traverse the directory [drawable | drawable-hdpi | drawable-nodpi | drawable-ldpi | drawable-mdpi] under the package/res directory
// The system obtains the icon with the largest icon size.
$ TmpArr [0] = 0; $ tmpArr [1] = 0; $ tmpArr [2] = 'drawable ';
$ Dirs = opendir ($ dir. 'package/res ');
While ($ file = readdir ($ dirs ))){
Preg_match ('/(drawable (-.*? Dpi )?) /I ', $ file, $ drawable_folder );
$ IconPath = $ dir. 'package/res/'. $ drawable_folder [1].'/'.{returnval}'thumbimg'{.'.png ';
If (file_exists ($ iconPath )){
$ IconInfo = getimagesize ($ iconPath );
If ($ iconInfo [0]> $ tmpArr [0] & $ iconInfo [1]> $ tmpArr [1]) {
$ TmpArr [0] = $ iconInfo [0]; $ tmpArr [1] = $ iconInfo [1]; $ tmpArr [2] = $ drawable_folder [1];
}
}
}
// $ This-> msg = $ iconInfo [0]. '---'. $ iconInfo [1];
Closedir ($ dirs );

After analysis, the ICON in the APK is usually stored in the following directories: drawable | drawable-hdpi | drawable-nodpi | drawable-ldpi | drawable-mdpi, retrieve the maximum ICON by traversing the comparison method and move it to the temporary directory.

Store all the information to be extracted in an array and write it to the form using javascript. As shown in:

Extract APK information Summary

The above code can be used to extract information from the uploaded APK. No error is found. In the comments of the above code, we can see that the APK "pocket Baidu" cannot extract information because of its special processing method, namely: <string name = "app_name"> "" </string>: Double quotation marks are added to the name. This is a special case. I have not found more special cases yet, so there may be special cases, which need to analyze the APK data and perform special processing in the program.

In implementing this APK extraction function, the key is to find the Organization rules of the APK package. Only by finding the rules, the program implementation is not natural.

Notes for releasing an APK File

Exec ('java-jar ../apktool. jar d-F'. $ this-> tmpFile. '. $ dir. 'package ');

The preceding statement must meet the following conditions:

1. Install the java package and grant the following permissions to the java directory and users user group: Read and run, list folder directories, and read

The users user group has the following permissions: read, run, and read.

3. PHP allows you to call exec

4. ensure that you have the permission to write files to the upload directory.

If you have better extraction methods, please contact us and learn from each other.

PHP extract APK information DEMO download

: Http://xiazai.jb51.net/201304/yuanma/php_apk_jb51net.rar

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.