PHP upload apk After the use of automatic extraction APK packet information (sample download) _php instance

Source: Internet
Author: User
Tags comments file permissions
The first project to enter the company is to do market market. So backstage to upload apk software and so on. In order to facilitate, upload APK after the system automatically extracts APK files related information, such as: APK package name, product name, version information, APK code, program size, icon and so on. The first way to handle

by command:java-jar axmlprinter2.jar androidmanifest.xml > Cmdafter.xml
Get the Cmdafter.xml file, and then analyze the Cmdafter.xml file to get the relevant information.

Unfortunately, the APK package name can be obtained from this file, but the ICO icon filename and other related information cannot be obtained. As shown in the following figure

Above, such as label, icon, etc. are flag values, can not directly obtain the desired results. Once analyzed the relationship between this value and apk file internal file, but different apk constructs are different, the implementation is too troublesome. In fact, some Internet sites such as the table market, when you upload apk, in addition to extract APK package name, but also include icon icons, size and other information. So if someone else can do it, I think there is a way to solve it. So after the study, the expected results were obtained. Here is a record of the method, welcome to Exchange.

Core extraction APK Information code
Copy Code code as follows:

/***
* Analysis of uploaded apk files, extract the required data
*/
function upapk () {
The number of times the global $_config_product_apktool_count;//uses Apktool.jar decompression is explained below.
if ($this->msg!= ') return;//if there is an error, 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 UC APK packages or their similar apk packages, decompression once does not completely get strings.xml files or related files. This is the only way to do so.
Use Java-jar directly under the system cmd ... Perform decompression, sometimes you can get strings.xml files, sometimes do not get, do not know is not a jar package problem.
EXEC (' Java-jar. /apktool.jar d-f '. $this->tmpfile. ' '. $dir. ' package ');//Comments: Decompression finished and then down execution
$stringsXML _exists=file_exists ($dir. ' Package/res/values/strings.xml ');
}
Check that the Androidmanifest.xml file exists, and if it does not exist, it is not a valid APK file
if (!file_exists ($dir. ' Package/androidmanifest.xml ')) {$this->msg= ' is not a valid 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 there is a version code, return to the array

   //To determine whether the database already exists after the package name has been detected.
    if ($this->id==0) {//Detect when adding new products, modify product 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= ' The apk already exists, please replace 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 '); Read Strings.xml file if you have Strings.xml
if (Preg_match ('/versionname=\ "([^\"]*) \/I ', $AndroidManifestXML, $ver)) $returnVal [' ver ']= $ver [1];//If there is a version number, return to the array
There are two types of version numbers: 1, the version number is listed directly in the Androidmanifest.xml;
2, the version number is the same as the label, placed 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 there is an icon, 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 a product name is available, return to the array
/**
Baidu: Strings.xml
Special case 1:<string Name= "app_name" > "Palm Baidu" </string>
*/
$returnVal [' Name ']=preg_replace ('/\s| ') /', ', $returnVal [' name ']];
}
}
$this->msg= $returnVal [' package ']. -'. $returnVal [' ver ']. ' -'. $returnVal [' thumbimg ']. ' -'. $returnVal [' name '];
if ($this->oldapk!= ') {//re-uploading delete original apk file and icon.png picture
Unlink ($dir. $this->oldapk);
Unlink ($dir. $this->oldapk. PNG ');
}
Traversing directories under the Package/res directory [drawable|drawable-hdpi|drawable-nodpi|drawable-ldpi|drawable-mdpi]
The system takes the icon of the biggest 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 directory and move successfully
$returnVal [' thumbimg ']= $this->iframe_key.] Apk.png ';
}
if (!move_uploaded_file ($this->tmpfile, $dir. $this->iframe_key.) APK ') {$this->msg= ' upload failed! '; return;} Transfer apk files
$returnVal [' filename ']= $this->iframe_key. ' APK ';
$returnVal [' Size ']= $this->size;
$this->result= $returnVal;
}


Extract information flow

1, first, through the Apktool.jar command to extract the apk file Package/res/values/string.xml files. For some reason, releasing apk files sometimes does not necessarily get string.xml files. So, background increment: $_config_product_apktool_count parameters to control the maximum number of releases.

2. Read and release the Androidmanifest.xml file under the root directory. From this file, you can get the APK package name and version information.

3, detection, if the new uploaded apk, its package name in the database exists. is to prohibit the upload of the same package name APK. does not detect when modified.

4, through the regular access to the required information.

Why do you want to extract the String.xml file here?

Because not all information is in the Androidmanifest.xml. Some information in the Androidmanifest.xml only as a "reference", the real record is in the String.xml. Like what

The value of the label and icon in the Androidmanifest.xml.

Above: label= "@string/app_name" indicates that the string's Name property is App_name in String.xml, the "software name" for that apk, and this is the "market market", as shown in the following illustration:

@drawable/quickflick_icon, representing the filename of Quickflick_icon icon.

For special needs, I need to find the largest icon icons, see the following code:

Copy Code code as follows:

Traversing directories under the Package/res directory [drawable|drawable-hdpi|drawable-nodpi|drawable-ldpi|drawable-mdpi]
The system takes the icon of the biggest 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 general APK store icon in the following directories: DRAWABLE|DRAWABLE-HDPI|DRAWABLE-NODPI|DRAWABLE-LDPI|DRAWABLE-MDPI, Get the maximum icon by traversing the comparison and move to the temp directory.

Save all the information you need to extract into an array and write it to the form through JavaScript. As shown in the following illustration:

Summary of extracting APK information

The above code, so far, in the extraction of uploaded apk, can be normal extraction of information, no errors found. In the above code comments also see, about "palm Baidu" This apk, extract information is due to his special treatment, namely: <string name= "app_name" > "Palm Baidu" </STRING> He added double quotes to the name , it's a special case. More special cases I have not yet found, so there may be a special case, which requires the analysis of APK data, and in the program to do special processing.

In the realization of this apk extraction function, the key is to find APK packet organization rules, only to find the law, program implementation is in the natural thing.

Release APK file Note content

EXEC (' Java-jar. /apktool.jar d-f '. $this->tmpfile. ' '. $dir. ' Package ');

To execute the above statement smoothly, the following conditions are met:

1, install Java package, to the Java directory, users user group permissions are: Read and run, List folder directory, read

2, Cmd.exe files, users of the user group's permissions are: Read and run, read

3. PHP allows calling exec

4, upload the directory to ensure that there is write file permissions

If there is a better way to extract, welcome to communicate, learn from each other.

PHP Extract apk information demo demo download

Download Address: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.