天天看點

android aidl

aidl的使用幫助如下:

C:\Documents and Settings\Administrator>aidl

INPUT required

usage: aidl OPTIONS INPUT [OUTPUT]

       aidl --preprocess OUTPUT INPUT...

OPTIONS:

   -I<DIR>    search path for import statements.

   -d<FILE>   generate dependency file.

   -p<FILE>   file created by --preprocess to import.

   -o<FOLDER> base output folder for generated files.

   -b         fail when trying to compile a parcelable.

INPUT:

   An aidl interface file.

OUTPUT:

   The generated interface files.

   If omitted and the -o option is not used, the input filename is used, with th

e .aidl extension changed to a .java extension.

   If the -o option is used, the generated files will be placed in the base outp

ut folder, under their package folder

複制代碼

這些資訊太少了,還不能讓人明白如何使用。幫助資訊首句就是“INPUT required”,其實隻有"INPUT"仍是不夠的,OPTIONS中“-I”是必選而非可選,否則會提示:

D:\JavaTest\ApiDemos\src\com\example\android\apis\app\IRemoteService.aidl:19: couldn't find import for class com.example.android.apis.app.IRemoteServiceCallback

以SDK自帶的ApiDemos為實踐對象,該工程存儲路徑為:"D:\JavaTest\ApiDemos\",有三個aidl檔案分别為:

com\example\android\apis\app\IRemoteService.aidl

com\example\android\apis\app\IRemoteServiceCallback.aidl

com\example\android\apis\app\ISecondary.aidl

要生成對應的.java檔案指令行如下:

aidl -ID:\JavaTest\ApiDemos\src  D:\JavaTest\ApiDemos\src\com\example\android\apis\app\IRemoteService.aidl

對,你沒看錯,"-I"與"D:\JavaTest\***"之間是沒有空格的。XX,看來Google裡也有相當混蛋的程式員。

執行此條指令後,生成的.java會與.aidl檔案在同一目錄下。

如果想指定aidl的生成路徑,則可以按照aidl的提示資訊使用"-o"選項:

aidl -ID:\JavaTest\ApiDemos\src  -oD:\JavaTest D:\JavaTest\ApiDemos\src\com\example\android\apis\app\IRemoteService.aidl

對,你還是沒看錯,"-o"與"D:\JavaTest\**"中間還是沒有空格,再次咒罵設計了aidl工具的那個混蛋。

執行此指令後,則生成的aidl檔案存于"D:\JavaTest\"路徑下了。

在ApiDemos中,IRemoteService.aidl與IRemoteServiceCallback.aidl是互相依賴的,在編譯IRemoteService.aidl時,通過使用"-d"可以将其依賴的相關類輸出到自定義的檔案中。

aidl -ID:\JavaTest\ApiDemos\src  -oD:\JavaTest  -dD:\JavaTest\aidl_dependency.txt D:\JavaTest\ApiDemos\src\com\example\android\apis\app\IRemoteService.aidl

生成的aidl_dependenry.txt内容如下:

: \

  D:\JavaTest\ApiDemos\src\com\example\android\apis\app\IRemoteService.aidl \

  D:\JavaTest\ApiDemos\src\com\example\android\apis\app\IRemoteServiceCallback.aidl

可選項中"-b"的用法不詳,幫助資訊中的那句“fail when trying to compile a parcelable.”就感覺少說了些内容,"fail"然後幹嘛,XX,第三次咒罵設計了aidl工具的那個混蛋。

幫助資訊中還有“aidl --preprocess OUTPUT INPUT...”,作用是根據要編譯的.aidl生成預處理檔案,但具體預處理起到什麼,還請知道的兄弟姐妹們告訴我下啊,謝謝。

仍以ApiDemos為例,生成預處理檔案為aidl.preprocess:

aidl --preprocess D:\JavaTest\aidl.preprocess D:\JavaTest\ApiDemos\src\com\example\android\apis\app\IRemoteService.aidl D:\JavaTest\ApiDemos\src\com\example\android\apis\app\IRemoteServiceCallback.aidl D:\JavaTest\ApiDemos\src\com\example\android\apis\app\ISecondary.aidl

生成的aidl.preprocess内容如下:

interface com.example.android.apis.app.IRemoteService;

interface com.example.android.apis.app.IRemoteServiceCallback;

interface com.example.android.apis.app.ISecondary;

SDK自帶了預處理檔案為<sdk_path>\platforms\android-<level>\framework.aidl。

“-p”表示編譯aidl時以預處理檔案為參去生成.java,指令方法如下:

aidl -ID:\JavaTest\ApiDemos\src  -pE:\SoftSetup\AndroidSDK\android_sdk_r08_windows\platforms\android-3\framework.aidl D:\JavaTest\ApiDemos\src\com\example\android\apis\app\IRemoteService.aidl

而通過檢視adt的源碼可以發現,adt編譯aidl使用的正是系統自帶的預處理檔案framewrok.aidl。

adt源碼處理aidl的檔案為:<OpenSourceProjectPath>\sdk\eclipse\plugins\com.android.ide.eclipse.adt\src\com\android\ide\eclipse\adt\internal\build\PreCompilerBuilder.java,其中的handleAidl()即為編譯aidl的執行方法。

繼續閱讀