2

I need a class or maybe a function to get version name, version code, package name and minsdk level from an apk file.

I found apk parser php class but in server it do not work fine.

I found this function but this function has error too.

function apps_infosfromapk($file)
{
global $AAPT_DIR;

$infos=array();
$retour = array();
exec ($AAPT_DIR." l -a ".realpath($file),$retour);

$txt = "";
for($i = 0; $i < sizeof ($retour); $i++)
    $txt .= $retour[$i];

$t1 = explode('android:versionName(0x0101021c)="',$txt);
$t3 = explode('"',$t1[1]);

$version = $t3[0];

$t2 = explode ('package="', $txt);
$t4 = explode ('"', $t2[1]);

$package = $t4[0];

$t5 = explode('A: android:minSdkVersion(0x0101020c)=(type 0x10)0x',$txt);
$t6 = explode(' ',$t5[1]);

$minSdk = intval($t6[0]);


$txt2=explode('android:name(0x01010003)="android.permission.',$txt);

$it=0;
$permissions="";
for($i=1;$i<sizeof($txt2);$i++)
{
    $tmp=explode('"',$txt2[$i]);
    if($it==0)
        $permissions.=$tmp[0];
    else
        $permissions.=";".$tmp[0];

    $it++;
}



$infos[0]=$version; 
$infos[1]=$package;
$infos[2]=$minSdk;
$infos[3]=$permissions;

return $infos;
}
brasofilo
  • 25,496
  • 15
  • 91
  • 179
Mojtaba GS3
  • 51
  • 1
  • 12
  • It doesn't look like a difficult job to parse the file, it's not even in binary format. The reason why your scripts won't work might be due to linux/windows differences, I wonder why the author does not use PHP built-in file/directory functions which are os independend.. I don't think someone will write the function for you so you might provide the errors you get with the code you already have. [APK Fileformat](http://en.wikipedia.org/wiki/APK_%28file_format%29) – Daniel W. Jan 07 '14 at 12:43

1 Answers1

1

I also tried to find that in PHP, but without good solution. Here are some related threads:

  1. Thread1
  2. Thread2

The most popular solution is to use aapt dump badging myapp.apk instead, you can get all the information in AndroidManifest.xml.

Here is another code in java hope that will help you:

Java

And I have found a PHP script it meets all your needs, but still need the help of aapt.

function readApkInfoFromFile($apk_file, $get_icon = false){
   exec("/opt/android-sdk-linux/build-tools/19.0.0/aapt d badging {$apk_file}", $out, $return);
   $temp_path = FCPATH . 'cache/temp/'.md5($apk_file).'/';

   if($return == 0){
        @mkdir($temp_path);
        $str_out = implode("\n", $out);
        $out = null;

        #icon
        if($get_icon){
            $pattern_icon = "/icon='(.+)'/isU";
            preg_match($pattern_icon, $str_out, $m);
            $info['icon'] = $m[1];
            if($info['icon']){
                //$command = "unzip {$apk_file} {$info['icon']} -d " . $temp_path;
                 $command = '7z x "' . $apk_file . '" -y -aos -o"' . $temp_path . '" ' . $info['icon'];
                 //exit($command);
                //mkdirs("/tmp/".$info['icon'],true);
                exec($command);
                $info['icon'] = $temp_path . $info['icon'];
            }
        }


        $pattern_name = "/application: label='(.*)'/isU";
        preg_match($pattern_name, $str_out,$m);
        $info['lable']=$m[1];


        $pattern_sys_name = "/package: name='(.*)'/isU";
        preg_match($pattern_sys_name, $str_out,$m);
        $info['sys_name']=$m[1];


        $pattern_version_code = "/versionCode='(.*)'/isU";
        preg_match($pattern_version_code, $str_out,$m);
        $info['version_code']=$m[1];


        $pattern_version = "/versionName='(.*)'/isU";
        preg_match($pattern_version, $str_out,$m);
        $info['version']=$m[1];


        $pattern_sdk = "/sdkVersion:'(.*)'/isU";
        if(preg_match($pattern_sdk, $str_out,$m)){
            $info['sdk_version']=$m[1];
            if($info['sdk_version']){
                $sdk_names = array(3=>"1.5",4=>"1.6",7=>"2.1",8=>"2.2",10=>'2.3.3',11=>"3.0",12=>"3.1",13=>"3.2",14=>"4.0");
                if($sdk_names[$info['sdk_version']]){
                    $info['os_req'] = "Android {$sdk_names[$info['sdk_version']]}";
                }
            }
        }


        $pattern_perm = "/uses-permission:'(.*)'/isU";
        preg_match_all($pattern_perm, $str_out,$m);
        if(isset($m[1])){
            foreach($m[1] as $mm){
                $info['permissions'][] = $mm;
            }
        }


        $pattern_features = "/uses-feature:'(.*)'/isU";
        preg_match_all($pattern_features, $str_out,$m);
        if(isset($m[1])){
            foreach($m[1] as $mm){
                $info['features'][] = $mm;
            }
        }

        $info['apk_info'] = $str_out;
        return $info;
    }
    return false;
}
Community
  • 1
  • 1
twlkyao
  • 14,302
  • 7
  • 27
  • 44