Question

Je suis en train de créer un script PHP pour obtenir la version de l'application Android de fichier APK. XML extractible à partir du fichier APK (zip), puis analyse XML est une façon, mais je suppose qu'il devrait être plus simple. Quelque chose comme Manuel PHP , par exemple # 3. Toute idée comment créer le script?

Était-ce utile?

La solution

Si vous avez le SDK Android installé sur le serveur, vous pouvez utiliser exec de PHP (ou similaire) pour exécuter l'outil aapt (en $ANDROID_HOME/platforms/android-X/tools).

$ aapt dump badging myapp.apk

Et la sortie devrait inclure:

package: name='com.example.myapp' versionCode='1530' versionName='1.5.3'

Si vous ne peut pas installer le SDK Android, pour une raison quelconque, vous devez analyser le format XML binaire Android. Le fichier AndroidManifest.xml intérieur de la structure zip APK est un texte pas simple.

vous devez le port d'un de Java à PHP.

Autres conseils

J'ai créé un ensemble de fonctions PHP qui trouveront juste le Code de la version d'un APK. Ceci est basé sur le fait que le fichier AndroidMainfest.xml contient le code de version comme la première balise, et basée sur le AXML (format XML Android binaire) comme décrit ici

    <?php
$APKLocation = "PATH TO APK GOES HERE";

$versionCode = getVersionCodeFromAPK($APKLocation);
echo $versionCode;

//Based on the fact that the Version Code is the first tag in the AndroidManifest.xml file, this will return its value
//PHP implementation based on the AXML format described here: https://stackoverflow.com/questions/2097813/how-to-parse-the-androidmanifest-xml-file-inside-an-apk-package/14814245#14814245
function getVersionCodeFromAPK($APKLocation) {

    $versionCode = "N/A";

    //AXML LEW 32-bit word (hex) for a start tag
    $XMLStartTag = "00100102";

    //APK is esentially a zip file, so open it
    $zip = zip_open($APKLocation);
    if ($zip) {
        while ($zip_entry = zip_read($zip)) {
            //Look for the AndroidManifest.xml file in the APK root directory
            if (zip_entry_name($zip_entry) == "AndroidManifest.xml") {
                //Get the contents of the file in hex format
                $axml = getHex($zip, $zip_entry);
                //Convert AXML hex file into an array of 32-bit words
                $axmlArr = convert2wordArray($axml);
                //Convert AXML 32-bit word array into Little Endian format 32-bit word array
                $axmlArr = convert2LEWwordArray($axmlArr);
                //Get first AXML open tag word index
                $firstStartTagword = findWord($axmlArr, $XMLStartTag);
                //The version code is 13 words after the first open tag word
                $versionCode = intval($axmlArr[$firstStartTagword + 13], 16);

                break;
            }
        }
    }
    zip_close($zip);

    return $versionCode;
}

//Get the contents of the file in hex format
function getHex($zip, $zip_entry) {
    if (zip_entry_open($zip, $zip_entry, 'r')) {
        $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
        $hex = unpack("H*", $buf);
        return current($hex);
    }
}

    //Given a hex byte stream, return an array of words
function convert2wordArray($hex) {
    $wordArr = array();
    $numwords = strlen($hex)/8;

    for ($i = 0; $i < $numwords; $i++)
        $wordArr[] = substr($hex, $i * 8, 8);

    return $wordArr;
}

//Given an array of words, convert them to Little Endian format (LSB first)
function convert2LEWwordArray($wordArr) {
    $LEWArr = array();

    foreach($wordArr as $word) {
        $LEWword = "";
        for ($i = 0; $i < strlen($word)/2; $i++)
            $LEWword .= substr($word, (strlen($word) - ($i*2) - 2), 2);
        $LEWArr[] = $LEWword;
    }

    return $LEWArr;
}

//Find a word in the word array and return its index value
function findWord($wordArr, $wordToFind) {
    $currentword = 0;
    foreach ($wordArr as $word) {
        if ($word == $wordToFind)
            return $currentword;
        else
            $currentword++;
    }
}
    ?>

Utilisez ce dans le CLI:

apktool if 1.apk
aapt dump badging 1.apk

Vous pouvez utiliser ces commandes en PHP en utilisant exec ou shell_exec.

aapt dump badging ./apkfile.apk | grep sdkVersion -i

Vous obtiendrez une forme lisible par l'homme.

sdkVersion:'14'
targetSdkVersion:'14'

Il suffit de chercher AAPT dans votre système si vous avez Android SDK installé. Le mien est dans:

<SDKPATH>/build-tools/19.0.3/aapt

Le format de vidage est un peu étrange et pas le plus facile à travailler. Juste pour développer quelques-unes des autres réponses, c'est un script shell que j'utilise pour analyser le nom et la version des fichiers APK.

aapt d badging PACKAGE | gawk $'match($0, /^application-label:\'([^\']*)\'/, a) { n = a[1] }
                               match($0, /versionName=\'([^\']*)\'/, b) { v=b[1] }
                               END { if ( length(n)>0 && length(v)>0 ) { print n, v } }'

Si vous voulez juste la version alors évidemment il peut être beaucoup plus simple.

aapt d badging PACKAGE | gawk $'match($0, /versionName=\'([^\']*)\'/, v) { print v[1] }'

Voici les variations appropriées pour les deux gawk et mawk (un peu moins durable au cas où les changements de format de vidage, mais doit être fine):

aapt d badging PACKAGE | mawk -F\' '$1 ~ /^application-label:$/ { n=$2 }
                                    $5 ~ /^ versionName=$/ { v=$6 }
                                    END{ if ( length(n)>0 && length(v)>0 ) { print n, v } }'

aapt d badging PACKAGE | mawk -F\' '$5 ~ /^ versionName=$/ { print $6 }'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top