2012-02-24 32 views
0

我想從AndroidManifest.xml中獲取versionName,versionCode和包名。 這是我到目前爲止有:php從androidmanifest.xml獲取manifest屬性

// Load AndroidManifest.xml 
$xml = simplexml_load_string($manifest); 

//print_r($xml); 

list(,$versionCode) = each($xml->xpath('/manifest/@android:versionCode')); 
list(,$versionName) = each($xml->xpath('/manifest/@android:versionName')); 
list(,$package) = each($xml->xpath('(/manifest/@package)')); 

echo "versionCode: ".$versionCode.", versionName: ".$versionName.", package:".$package."\n"; 

我不知道是否有更好的方式來獲得這些元素的值?

樣品的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest android:versionCode="1" android:versionName="1.0" package="com.example.bluetoothchat" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="11" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <application android:label="@string/app_name"> 
     <activity android:label="@string/app_name" android:name=".BluetoothChat" android:configChanges="keyboardHidden|orientation"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     </activity> 
     <activity android:label="@string/select_device" android:name=".DeviceListActivity" android:configChanges="keyboardHidden|orientation" /> 
    </application> 
</manifest> 
+0

您是如何打開apk文件以閱讀清單的? – iSun 2012-09-17 16:29:57

+0

如何獲取app_name,因爲它是來自「Strings.xml」的引用文件 – twlkyao 2014-01-07 13:23:04

+0

@iSun AndroidManifest.xml使用[AXMLPrinter2.jar](https://code.google.com/p/android4me/downloads/詳細信息?name = AXMLPrinter2.jar&can = 2&q =),首先解壓縮apk文件,然後進入解壓目錄並運行java -jar AXMLPrinter2.jar AndroidManifest.xml。 @twlkyao我沒有必要獲得應用程序名稱,所以沒有答案抱歉! – t0x13 2014-02-06 05:23:15

回答

0

檢查,這是預期的結果

<?php 
error_reporting(E_ERROR | E_PARSE); 
$dom = new DOMDocument(); 
$dom->load('AndroidManifest.xml'); 
$xml = simplexml_import_dom($dom); 
$versionName = $xml->xpath('/manifest/@android:versionName'); 
$versionCode =$xml->xpath('/manifest/@android:versionCode'); 
$package = $xml->xpath('/manifest/@package'); 
echo "VERSION NAME :".$versionName[0]->versionName."<br/>"; 
echo "VERSION CODE :".$versionCode[0]->versionCode."<br/>"; 
echo "VERSION PAC :".$package[0]->package."<br/>"; 
?> 
+0

感謝張貼..不幸的是,它失敗了:** PHP警告:DOMDocument :: load():I/O警告:未能加載外部實體** – t0x13 2012-02-24 19:21:15

+0

@ t0x13檢查出來,現在它不會通過Warning錯誤,,,在此之前,你需要有你的清單文件在相同的目錄.. – 2012-02-25 04:11:54

+0

它不會拋出一個錯誤了..但是,xpath似乎沒有在不同的機器上正常工作。我的代碼現在不工作。它可以獲取包,但versionCode和versionName會返回emtpy。它可能是由於不同版本的PHP? – t0x13 2012-03-02 17:27:39