Home > Backend Development > PHP Tutorial > How to get information in Android package in php

How to get information in Android package in php

WBOY
Release: 2016-07-06 13:52:14
Original
1205 people have browsed it

Now I want to get the information in the xml file in the Android package. Can anyone provide some ideas?

Reply content:

Now I want to get the information in the xml file in the Android package. Can anyone provide some ideas?

First of all, as mentioned above, the apk file is just a zip compressed package, so there is an idea: use php to decompress, and read the decompressed file to achieve the purpose of the question.
Then search for "php execution decompression". There are many related examples on the Internet, such as: http://jb51.net/article/25521.htm. I won’t go into details here

I hope the questioner will try to solve it by himself next time he encounters a problem.

You need to install the apktool tool to decompile the apk file, and then use preg_match to match the parameters you need from the package information. I have done this before, and it was achieved in this way!

Reference: https://github.com/iBotPeaches/Apktool

Update:

I just looked for the code I wrote before, I hope it can help you:

<code class="php">
/**
 * 获取 APK 包信息和应用图标(需要exec支持)
 */
public function apkParseInfo($apk) {

    $aapt = '/usr/bin/aapt';

    if( FALSE === is_file($aapt) ) {
        return FALSE;
    }

    exec("{$aapt} d badging {$apk}", $output, $return);

    // 解析错误
    if ( $return !== 0 ) {
        return FALSE;
    }
    
    $output = implode(PHP_EOL, $output);

    $apkinfo = new stdClass;
    
    // 对外显示名称
    $pattern = "/application: label='(.*)'/isU";
    $results = preg_match($pattern, $output, $res);
    $apkinfo->label = $results ? $res[1] : '';
    
    // 内部名称,软件唯一的
    $pattern = "/package: name='(.*)'/isU";
    $results = preg_match($pattern, $output, $res);
    $apkinfo->sys_name = $results ? $res[1] : '';
    
    // 内部版本名称,用于检查升级
    $pattern = "/versionCode='(.*)'/isU";
    $results = preg_match($pattern, $output, $res);
    $apkinfo->version_code = $results ? $res[1] : 0;
    
    // 对外显示的版本名称
    $pattern = "/versionName='(.*)'/isU";
    $results = preg_match($pattern, $output, $res);
    $apkinfo->version = $results ? $res[1] : '';
    
    // 系统支持
    $pattern = "/sdkVersion:'(.*)'/isU";
    $results = preg_match($pattern, $output, $res);
    $apkinfo->sdk_version = $results ? $res[1] : 0;
    
    // 分辨率支持
    $densities = array(
        "/densities: '(.*)'/isU",
        "/densities: '120' '(.*)'/isU",
        "/densities: '160' '(.*)'/isU",
        "/densities: '240' '(.*)'/isU",
        "/densities: '120' '160' '(.*)'/isU",
        "/densities: '160' '240' '(.*)'/isU",
        "/densities: '120' '160' '240' '(.*)'/isU"
    );

    foreach($densities AS $k=>$v) {
        if( preg_match($v, $output, $res) ) {
            $apkinfo->densities[] = $res[1];
        }
    }

    // 应用权限
    $pattern = "/uses-permission:'(.*)'/isU";
    $results = preg_match_all($pattern, $output, $res);
    $apkinfo->permissions = $results ? $res[1] : '';
    
    // 需要的功能(硬件支持)
    $pattern = "/uses-feature:'(.*)'/isU";
    $results = preg_match_all($pattern, $output, $res);
    $apkinfo->features = $results ? $res[1] : '';
    
    // 应用图标路径
    if( preg_match("/icon='(.+)'/isU", $output, $res) ) {

        $icon_draw = trim( $res[1] );
        $icon_hdpi = 'res/drawable-hdpi/' . basename($icon_draw);

        $temp = $this->_file_save_path . basename($apk, '.apk') . DIRECTORY_SEPARATOR;

        if( @is_dir($temp) === FALSE ) {
            create_dir($temp);
        }
        
        exec("unzip {$apk} {$icon_draw} -d " . $temp);
        exec("unzip {$apk} {$icon_hdpi} -d " . $temp);

        $apkinfo->icon = $icon_draw;

        $icon_draw_abs = $temp . $icon_draw;
        $icon_hdpi_abs = $temp . $icon_hdpi;

        $apkinfo->icon = @is_file($icon_hdpi_abs) ? $icon_hdpi_abs : $icon_draw_abs;
    }
    
    return $apkinfo;
}</code>
Copy after login

The Android apk file is actually a zip compressed package, you can directly read the xml file inside

xml文件里的信息 if it refers to AndroidManifest.xml

You need to use aapt to read it, just adjust the system command.

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template