1 前言
Android Studio 对模块化开发提供的一个很有用的功能就是可以在主项目下新建库项目 (Module), 但是在使用库项目时却有一个问题就是资源 ID 冲突, 因为编译时 SDK 会自动帮我们处理这个问题, 所以一般我们不会察觉到, 但是在某些情况下, 我们需要意识到这个问题的存在
比如, 在新建的库项目中使用如下代码:
- public void onButtonClick(View view) {
- switch (view.getId()) {
- case R.id.button_1:
- break;
- case R.id.button_2;
- break;
- }
- }
IDE 会提示:
- Resource IDs cannot be used in a switch statement in Android library modules less.
- Validates using resource IDs in a switch statement in Android library module. Resource IDs are non final in the library projects since SDK tools r14, means that the library code cannot treat these IDs as constants.
再比如, 我们在库项目中以如下方式使用 ButterKnife, 编译时就会报错
- @OnClick(R.id.button_1)
- public void onButtonClick(View view) {
- }
2 分析
无论是 switch 语句还是注解, 都有一个要求就是使用的值必须是常量在主项目中, R 类中的成员变量都被 static final 修饰, 而在库项目中仅被 static 修饰
- // 库项目中生成的 R 类:
- public final class R {
- public static final class id {
- public static int button_1 = 0x7f0c0001;
- }
- }
- // 主项目中生成的 R 类:
- public final class R {
- public static final class id {
- public static final int text_1 = 2131165184;
- }
- }
为什么库项目中生成的资源 ID 没有被 final 修饰呢? 官方解释如下:
Non-constant Fields in Case Labels
当多个库项目进行合并时, 不同项目中的资源 ID 可能会重复在 ADT 14 之前, 无论是主项目还是库项目, 资源 ID 统一被定义为 final 类型的静态变量这样照成的结果就是主项目进行编译时一旦发现资源 ID 冲突, 库项目中对应的资源文件以及引用资源文件的代码都需要重新编译
如果代码中使用了被 static final 修饰的变量, 那这个变量实际上就是一个常量, 编译时会直接使用它的值进行替换在编译时, 如果库项目与主项目的资源 ID 发生了重复, 资源被分配了新的 ID 后库项目之前编译过的代码也就失效了
那么当库项目 R 类中的变量仅被 static 修饰后会起到什么作用呢, 我们可以看一下编译后的字节码再反编译后的样子
- // 主项目中的 Activity:
- public class MainActivity extends AppCompatActivity {
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 源代码: setContentView(R.layout.activity_main);
- this.setContentView(2131296283);
- }
- }
- // 库项目中的 Activity:
- public LibActivity extends AppCompatActivity {
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.activity_lib);
- }
- }
主项目 R 类中的资源 ID 被 static final 修饰, 编译时直接被替换成了对应的常量库项目 R 类中的资源 ID 仅被 static 修饰, 所以保留了变量这样当资源 ID 发送冲突时, 主项目 R 类不变, 修改库项目 R 类中的变量, 库项目已经编译过的代码仍有效
3ButterKnife 中的 R2 类
既然库项目中的资源 ID 不可以定义为常量, 那如何在库文项目使用 ButterKnife 呢, 作者提供了 R2 类供我使用
- @OnClick({R2.id.button_1, R2.id.button_2})
- public void onButtonClick(View view) {
- int id = view.getId();
- if (id == R.id.button_1) {
- // ...
- } else if (id == R.id.button_2) {
- // ...
- }
- }
没错在注解中使用 R2 类, 但是在代码里还是需要使用 R 类, 因为 R 类中的 ID 不是常量, 所以只能使用 if 语句进行判断
先来看一下 ButterKnife 为我们生成的 R2 类与 R 类有什么不同:
- // 库项目中的 R 类:
- public final class R {
- public static final class id {
- public static int button_1 = 0x7f0c0001;
- }
- }
- // 库项目中 ButterKnife 为我们生成的 R2 类:
- public final class R2 {
- public static final class id {
- public static final int button_1 = 0x7f0c0001;
- }
- }
ButterKnife 做的工作很简单, 仅仅是把 R 类中的变量搬到了 R2 类里, 然后给所有的变量都加上了 final 根据前面所说, 当项目整体编译时, 库项目的资源 ID 一旦与主项目的资源 ID 发送冲突, 库项目的资源会被重新分配 ID 导致其 R 类被修改显然这个过程并不涉及 R2 类, R2 类中保留的仍然是过时的 ID 但是 ButterKnife 提供的注解的作用是什么, 它们并不是为了提供运行时信息, 而是为了在编译时生成代码
- public class LibActivity_ViewBinding implements Unbinder {
- private LibActivity target;
- private View view_button_1;
- private View view_button_2;
- @UiThread
- public LibActivity_ViewBinding(final LibActivity target, View source) {
- this.target = target;
- View view = Utils.findRequiredView(source, R.id.button_1, "method'onButtonClick'");
- this.view_button_1 = view;
- //view.setOnClickListener....
- view = Utils.findRequiredView(source, R.id.button_2, "method'onButtonClick'");
- this.view_button_2 = view;
- //view.setOnClickListener....
- }
- }
在 ButterKnife 生成的代码中, 使用的仍然是 R 类 R2 起到的作用仅仅是提供一个符号名, 只要让程序知道在生成代码时对应哪一个变量即可这个方法可以说是很 tricky 了
来源: https://www.cnblogs.com/mmmmar/p/8628794.html