2015-04-16 96 views
18

我想將純文本AndroidManifest.xml文件轉換爲Android用於將其打包到最終APK中的二進制格式。Android:將標準XML轉換爲Android二進制XML格式(AXML)

我想這樣做在Java中,因爲我需要做的是在Android設備本身(這就是爲什麼這個問題是不是的副本:How to convert XML to Android Binary XML我知道你可以使用AAPT,但我需要一個java方法)

有很多工具可以將二進制xml解碼爲可讀文件,但沒有關於如何做相反的事情,這正是我想要的。

有關如何實現這一目標的任何信息或提示,

+1

有這太問題的資源,以XML https://stackoverflow.com/a/4761689/4307644他很不錯的格式發表評論解碼AXML。我們所需要的是對未知數據的「黑客」部分進行逆向工程,然後扭轉這個人的過程。這裏遲到了,所以我會在早上看看它。但希望有更多時間的人可以在此之前使用此資源並提供幫助。 – MeetTitan

+1

爲了讓賞金更清晰一些,@MeetTitan建議符合接收賞金的標準。像aapt,apktool等工具在他們「工作」的時候不會產生預期的結果。 –

+0

@Gavin所以基本上你正在尋找一個不依賴於sys調用外部工具的代碼答案,對吧? –

回答

6

您可能會發現許多命令行工具可用於編譯和反編譯Android的xml文件。這些工具結合了幾種構建工具,包括aapt (Android Asset Packaging Tools)查看,創建和更新Zip兼容檔案(zip,jar,apk)。由於此工具是Android SDK的一部分,因此在Java中沒有本地實現。

幸運的是,self-compile-Android存儲庫中有所有必需的文件Java Native Interface (JNI)。他們已準備好從Android應用程序內部使用,並且能夠自我編譯,突變和病毒式傳播。

這裏可用的本機模塊的應用程序的列表:

aapt -> Platform_Framework_Base\tools\aapt aidl -> Platform_Framework_Base\tools\aidl androidfw -> Platform_Framework_Base\include\androidfw 

zipalign -> Platform_Build\tools\zipalign host -> Platform_Build\lib\host 

libpng -> Platform_External_Libpng expat -> Platform_External_Expat zlib -> Platform_External_Zlib 

libcutils -> Platform_System_Core\libcutils cutils -> Platform_System_Core\include\cutils 

liblog -> Platform_System_Core\liblog log -> Platform_System_Core\include\log 

libutils -> Platform_System_Core\libutils utils -> Platform_System_Core\include\utils 

log.h -> Platform_System_Core\include\android 

asset_manager.h -> Platform_Framework_Native\include\android looper.h -> Platform_Framework_Native\include\android 

zopfli -> zopfli\src 

ld -> Tool_Chain_Utils\binutils-2.25\ld 

如果您在源仔細觀察,你會發現執行使用原生JNI文件AAPT命令的應用:

private void runAapt() throws Exception { 
    Util.deleteRecursive(new File(S.dirRes, "drawable-xxhdpi")); 

    Aapt aapt = new Aapt(); 
    int exitCode = aapt.fnExecute("aapt p -f -v -M " + S.xmlMan.getPath() + " -F " + S.ap_Resources.getPath() 
      + " -I " + S.jarAndroid.getPath() + " -A " + S.dirAssets.getPath() + " -S " + S.dirRes.getPath() 
      + " -J " + S.dirGen.getPath()); 

    if (exitCode != 0) { 
     throw new Exception("AAPT exit(" + exitCode + ")"); 
    } 
} 

現在,通過示例代碼來了解功能是如何實現的。例如,要更改清單文件中的值,

private void modifyManifest() throws Exception { 
     Document dom = Util.readXml(S.xmlMan); 

     dom.getDocumentElement().getAttributes().getNamedItem("package").setNodeValue(userInput.appPackage); 

     Transformer t = tf.newTransformer(); 
     t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); 
     t.setOutputProperty(OutputKeys.INDENT, "yes"); 
     t.setOutputProperty(OutputKeys.METHOD, "xml"); 
     t.setOutputProperty(OutputKeys.VERSION, "1.0"); 
     t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
     t.transform(new DOMSource(dom), new StreamResult(new FileOutputStream(xmlFile))); 
    } 
+0

謝謝,這是一個非常古老的問題,我不再在這個項目上工作,但我會接受它,因爲它看起來很好研究,我沒有理由相信它不會工作。 –

+0

非常感謝。 @Master_T –

0

您的問題歸結爲如何在Android設備上運行aapt。

由於aapt是開源的,最好的解決方案就是自己構建它!

很多人已經這樣做 - 事實上Play商店中也有例子。請參閱http://talc1.loria.fr/users/cerisara/posts/buildandroid/瞭解如何使用從AIDE應用獲取的aapt的說明。在Play商店中搜索出現了大量其他Android IDE,這些IDE也提供了apt功能。