您的位置:首页 > 移动开发 > Android开发

android framework中添加资源文件

2013-09-05 17:58 357 查看
在4.1以前,在系统framework中添加资源文件

通过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冲突,不好检查,不推荐。

但这并没有结束,google在4.1之后对这部分重新做了整理修改。

来看下做法吧

4.1及以后做法

在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 ***/>

eg.  <public type="string" name="sound_mute"/>
然后make update-api,这时在framework/base/api/current.txt中生成
field public static final int sound_mute = 17039385; // 0x1040019
然后将<public type="string" name="sound_mute"/> 修改为<public type="string" name="sound_mute" id="0x1040019"/>
这就完成了。但出于安全考虑,建议用下面方式。

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

编译方法在源码根目录下执行

$ make framework 2>&1 | sed -n -f frameworks/base/core/res/MakeJavaSymbols.sed | sort -u

  <java-symbol type="string" name="sound_mute" />
重新编译会产生

  <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,免得全部手写。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息