在4.1之前,在系統framewor中添加資源文件
經過Eclipse的通常應,咱們能夠聯想到是否就是簡單的把字符串放在res的各個文件夾裏面。先來試試看,編譯,系統當即報錯。
它提示你利用make update-api這個命令來更新public.xml文件或者把這個聲明稱hide類型。這怎麼辦呢?
方法有二:
方法1:正常添加完資源後,執行make update-api函數。系統更新res/values/public.xml文件。
方法2:正常添加完資源後,手動更改/res/values/public.xml文件。打開public.xml文件。發現結構以下:
1. <resources>
2. <!-- We don't want to publish private symbols in Android.R as part of the
3. SDK. Instead, put them here. -->
4. <private-symbols package="com.android.internal" />
5. <!-- AndroidManifest.xml attributes. -->
6. <eat-comment />
7. <!-- ===============================================================
8. Resources for version 1 of the platform.
9. =============================================================== -->
10. <eat-comment />
11. <public type="string" name="cancel" id="0x01040000" />
12. <public type="string" name="copy" id="0x01040001" />
13. <public type="string" name="copyUrl" id="0x01040002" />
14. <public type="style" name="TextAppearance.Widget.TextView.SpinnerItem" id="0x01030052" />
15. <public type="style" name="TextAppearance.WindowTitle" id="0x01030053" />
16. <public type="attr" name="theme" id="0x01010000" />
17. <public type="attr" name="label" id="0x01010001" />
18. <public type="attr" name="icon" id="0x01010002" />
19. <public type="attr" name="name" id="0x01010003" />
20. <public type="attr" name="manageSpaceActivity" id="0x01010004" />
21. <public type="attr" name="allowClearUserData" id="0x01010005" />
22. <public type="attr" name="permission" id="0x01010006" />
23. <public type="attr" name="readPermission" id="0x01010007" />
24. <public type="attr" name="writePermission" id="0x01010008" />
25. <public type="attr" name="protectionLevel" id="0x01010009" />
26. <!-- ===============================================================
27. Resources added in version 7 of the platform (Eclair MR1).
28. =============================================================== -->
29. <eat-comment />
30. <public type="attr" name="author" id="0x010102b4" />
31. <public type="attr" name="autoStart" id="0x010102b5" />
32.
33. </resources>
能夠本身動手添加,但比較麻煩,容易引發id衝突,很差檢查,不推薦。java
但這並無結束,google在4.1以後對這部分從新作了整理修改。android
來看下作法吧ubuntu
4.1及之後作法 api
在Androird JellyBean 4.1.0的framework裏面添加一個res,把xml寫好之後編譯時候報錯以下的錯誤
int ticker = com.android.internal.R.string.xxxxxxxxxx;
^
frameworks/base/services/java/com/android/server/StatusBarManagerService.java:143: cannot find symbol
symbol : variable xxxxxxxxxx
location: class com.android.internal.R.drawable安全
1,在public.xml 裏添加一項<public ***/>ide
eg. <public type="string" name="sound_mute"/>函數
而後make update-api,這時在api/current.txt中生成this
field public static final int sound_mute = 17039385; // 0x1040019google
而後將<public type="string" name="sound_mute"/> 修改成<public type="string" name="sound_mute" id="0x1040019"/>code
這就完成了。也就添加了一個public的屬性,但出於安全考慮,建議用下面方式。
2,解決辦法很簡單,在MakeJavaSymbols.sed裏面有:
# Run this on the errors output by javac of missing resource symbols,
# to generate the set of <java-symbol> commands to have aapt generate
# the symbol for them.
#
# For example: make framework 2>&1 | sed -n -f MakeJavaSymbols.sed | sort -u
編譯方法 在 jb下, make framework 2>&1 | sed -n -f frameworks/base/core/res/MakeJavaSymbols.sed | sort -u
eg.
hanhy@ubuntu11:~/ice2-901/trunk/jb$ make framework 2>&1 | sed -n -f frameworks/base/core/res/MakeJavaSymbols.sed | sort -u
編譯會產生
<java-symbol type="string" name="xxxxxxxxxx" />
把這個copy到publlic.xml。再從新編譯一次就搞定了。
public.xml也提到
<!-- Private symbols that we need to reference from framework code. See
frameworks/base/core/res/MakeJavaSymbols.sed for how to easily generate
this.
-->
4.2裏把private部分的單獨分離出來了,在symbols.xml裏 看起來全部private的internal res都必須在這裏聲明一下。JellyBean之前貌似沒有這麼麻煩。好在他提供了一個sed,省得所有手寫。