2010-06-10 5 views
18

Android APK 파일에서 앱 버전을 가져 오기 위해 PHP 스크립트를 만들려고합니다. APK (zip) 파일에서 XML 파일을 추출한 다음 XML을 구문 분석하는 것도 한 가지 방법이지만 더 간단해야합니다. PHP Manual, 예 # 3과 같은 것입니다. 스크립트를 만드는 방법에 대한 아이디어가 있으십니까?PHP : 안드로이드 .apk 파일에서 버전을 얻는 방법?

+0

는 당신이 안드로이드 SDK를 설치 할 수있는 가능성이 있는가 서버? –

+0

정보를 얻고 자하는 우편 번호는 어디에 있습니까? – 2ndkauboy

+0

@Francesco : Android SDK가없는 경우 @ Kau-Boy : zip (apk) 파일은 PHP 스크립트가있는 서버에 있습니다. 게시 한 링크 (예 # 3)에서와 같은 작업을 수행 할 수 있습니까? – Solata

답변

47

서버에 Android SDK가 설치된 경우 aapt 도구 ($ANDROID_HOME/platforms/android-X/tools)를 실행하기 위해 PHP의 exec (또는 이와 비슷한 도구)를 사용할 수 있습니다.

$ aapt dump badging myapp.apk

그리고 출력이 포함되어야

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

당신이 가 안드로이드 SDK를 설치할 수없는 경우 어떤 이유로, 당신은 안드로이드의 바이너리 XML 형식을 구문 분석해야합니다. APK zip 구조 내의 AndroidManifest.xml 파일은 이 아니고 일반 텍스트입니다.

utility like AXMLParser을 Java에서 PHP로 이식해야합니다. CLI를에서

+0

PHP 스크립트가있는 서버에 Android SDK가 설치되어 있지 않습니다.내가 게시 한 링크 (예 # 3)와 같은 해결책을 찾고있다 – Solata

+0

내 대답을 업데이트했다. –

+0

@ChristopherOrr 첫 번째 방법에 대한 예제를 제공 할 수 있습니까? 내 Windows Server를 통해 실행할 수 없습니다! – iSun

1

사용이 :

apktool if 1.apk 
aapt dump badging 1.apk 

당신은 exec 또는 shell_exec를 사용하여 PHP에서 이러한 명령을 사용할 수 있습니다.

6

나는 APK의 버전 코드 만 찾을 수있는 일련의 PHP 함수를 만들었습니다. 이것은 당신은 사람이 읽을 수를 얻을 것이다 described here

<?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++; 
    } 
} 
    ?> 
+0

안녕하세요 @ CHeil402 versionname을 얻는 방법 –

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

로 axml (바이너리 안드로이드 XML 형식)에 AndroidMainfest.xml 파일이 첫 번째 태그로 버전 코드가 포함되어 있다는 사실에 근거하고, 기반 형태.

sdkVersion:'14' 
targetSdkVersion:'14' 

Android SDK가 설치되어있는 경우 시스템에서 aapt를 찾으십시오.

<SDKPATH>/build-tools/19.0.3/aapt 
1

덤프 형식은 작업하기 가장 쉬운 조금 이상하다하지 :에 광산이다. 다른 답변을 확장하기 만하면 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 } }' 

버전을 원한다면 훨씬 간단 할 수 있습니다. 여기

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

는 둔한과 mawk의 모두에 적합 변화입니다 (덤프 형식 변경의 경우에 좀 덜 튼튼하지만 잘해야한다) :

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 }' 
관련 문제