Home >Backend Development >PHP Tutorial >How to automatically extract apk package information after php uploads apk (example download)_PHP tutorial

How to automatically extract apk package information after php uploads apk (example download)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:12:061151browse

The first project after entering the company is to do marketing. So you need to upload APK software and the like in the background. For convenience, after uploading the APK, the system automatically extracts relevant information of the APK file, such as: apk package name, product name, version information, APK Code, program size, ICON, etc. initial treatment

Get the cmdAfter.xml file through the command: java -jar AXMLPrinter2.jar AndroidManifest.xml > cmdAfter.xml
, and then analyze the cmdAfter.xml file to obtain relevant information.

But 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 the picture below

In the above picture, labels, icons, etc. are all flag values, and the required results cannot be obtained directly. I once analyzed the relationship between this value and the internal files of the APK file, but different APKs have different structures and it is too troublesome to implement. In fact, on some websites such as the Android Market, when you upload an APK, in addition to extracting the APK package name, it also includes ICON icon, size and other information. So since someone else can do it, I figured there must be a way around this. So after research, the expected results were obtained. I will record the method here and welcome exchanges.

Core extraction APK information code
Copy code The code is as follows:

  /***
   * 分析已上传的APK文件,提取所需要的数据
   */
  function upAPK(){
    global $_config_product_apktool_count;//使用apktool.jar解压的次数,原因下面有说明。
    if($this->msg!='')return;//如果有错误,返回
    $dir=$this->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++){
//针对UC的APK包或其类似的APK包,解压一次并不能完全得到strings.xml文件或相关文件。目前只有采用这个办法了。
//在系统cmd下直接使用java -jar ...执行解压,有时可以得到strings.xml文件,有时也得不到,不知道是不是jar包的问题。
exec('java -jar ../apktool.jar d -f '.$this->tmpFile.' '.$dir.'package');//注释:解压完毕再往下执行
      $stringsXML_exists=file_exists($dir.'package/res/values/strings.xml');
    }
    //检查AndroidManifest.xml文件是否存在,如果不存在,则不是合法的APK文件
    if(!file_exists($dir.'package/AndroidManifest.xml')){$this->msg='不是合法的APK文件,请重新上传!';return;}
    $AndroidManifestXML=file_get_contents($dir.'package/AndroidManifest.xml');//读取AndroidManifest.xml

    if(preg_match('/package=\"([^\"]*)\"/i',$AndroidManifestXML,$package))$returnVal['package']=$package[1];//如果有包名,返回到数组

    //增加versionCode
    if(preg_match('/versionCode=\"([^\"]*)\"/i',$AndroidManifestXML,$versionCode))$returnVal['versionCode']=$versionCode[1];//如果有版本代码,返回到数组

    //检测到包名后判断数据库中是否已经存在。
    if($this->id==0){//添加新产品时检测,修改产品不检测
      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='该APK已经存在,请更换!';
          return;
        }
      }else{
        $this->msg='系统无法检测该APK信息,请联系管理员!';
        return;
      }
    }

if($stringsXML_exists)$stringXML=file_get_contents($dir.'package/res/values/strings.xml');//If there is strings.xml, read the strings.xml file
if(preg_match( '/versionName="([^"]*)"/i',$AndroidManifestXML,$ver))$returnVal['ver']=$ver[1];//If there is a version number, return it to the array
//There are currently two types of version numbers: 1. The version number is listed directly in AndroidManifest.xml; it can be extracted through the above regular rules
//2. The version number is the same as the label and is placed in strings.xml In the file
//2011-11-23 add
if($stringXML!='' && strstr($ver[1],'@')){
if(preg_match('/^@ string/(.*)/i',$ver[1],$findVer)){
If(preg_match('/([ ^<]*)/',$stringXML,$a))$returnVal['ver']=$a[1];
}
}
//// ////////////////////////////////////////
if(preg_match('/< ;application[sS]*? android:icon="@drawable/([^"]*)"/i',$AndroidManifestXML,$icon))$returnVal['thumbimg']=$icon[1];// If there is an icon, return to the array
if($stringsXML_exists && preg_match('/ if(preg_match('/([^<]*)/',$stringXML ,$name)){
$returnVal['name']=$name[1];//If there is a product name, return to the array
/**
Baidu: strings.xml
Special case 1: " Pocket Baidu "
*/
$returnVal[ 'name']=preg_replace('/s|"/','',$returnVal['name']);
}
}
//$this->msg=$returnVal[ 'package'].'--'.$returnVal['ver'].'--'.$returnVal['thumbimg'].'--'.$returnVal['name'];
if($ this->oldAPK!=''){//Re-upload will delete the original apk file and icon.png picture
unlink($dir.$this->oldAPK);
unlink($dir.$ this->oldAPK.'.png');
}
//Traverse the directories under the package/res directory [drawable|drawable-hdpi|drawable-nodpi|drawable-ldpi|drawable-mdpi]
//The system takes 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')){//Found directory and successfully moved
$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 file
$returnVal['filename']=$this->iframe_key.'.apk';
$returnVal['size']=$this->size ;
$this->result=$returnVal;
}


Extraction information process

1. First, extract the package/res/values/string.xml file in the apk file through the apktool.jar command. For some unknown reason, when releasing the apk file, sometimes the string.xml file is not necessarily obtained. Therefore, the background adds: $_config_product_apktool_count parameter to control the maximum number of releases.

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

3. Check whether the package name exists in the database if it is a newly uploaded APK. It is forbidden to upload APKs with the same package name. Modification is not detected.

4. Obtain the required information through regular expressions.

Why do we need to extract the string.xml file here?

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 values ​​of Label and icon in AndroidManifest.xml.

In the above picture: label="@string/app_name" indicates that the name attribute of string in string.xml is the value of app_name, which is the "software name" of the APK. Here is the "Market", as shown in the figure below Display:

@drawable/quickflick_icon, indicating that quickflick_icon is the file name of ICON.

Due to special needs, I need to find the largest ICON icon, see the code below:

Copy code The code is as follows:

//Traverse the directories under the package/res directory [drawable|drawable-hdpi|drawable-nodpi|drawable-ldpi|drawable-mdpi]
//The system takes 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, ICON icons are generally stored in the following directories in APK: drawable|drawable-hdpi|drawable-nodpi|drawable-ldpi|drawable-mdpi. The largest ICON icon is obtained through traversal comparison and moved to the temporary Table of contents.

Save all the information that needs to be extracted into an array and write it to the form through javascript. As shown below:

Extract APK information summary

The above code, so far, can extract information normally from the uploaded APK, and no errors have been found. We can also see in the comments of the above code that information cannot be extracted about the "Pocket Baidu" APK because of its special processing method, namely: " Pocket Baidu ", he added double quotes in the name, which is considered a special case. I have not found more special cases yet, so there may be special cases that require analysis of the APK data and special processing in the program.

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

Pay attention to the content when releasing the APK file

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

To successfully execute the above statement, the following conditions must be met:

1. Install the java package. For the java directory, the permissions of the users group are: read and run, list folder directories, and read

2. cmd.exe file, the permissions of the users user group are: read and run, read

3. PHP allows calling exec

4. Make sure the upload directory has permission to write files

If there is a better extraction method, welcome to communicate and learn from each other.

PHP extracts APK information DEMO demo download

Download address: http://xiazai.jb51.net/201304/yuanma/php_apk_jb51net.rar

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326771.htmlTechArticleThe first project when entering the company is to do marketing. So you need to upload APK software and the like in the background. For convenience, after uploading the APK, the system automatically extracts the relevant information of the APK file, such as: apk package name...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn