Android之组件化开发library中R类的id非常量问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010144805/article/details/81912187

元素值必须为常量表达式

在Tools Android的网站上有详细的说明,主要是避免多个库之间出现资源冲突。
https://blog.csdn.net/yy1300326388/article/details/46564507

butterknife的处理方法

butterknife在8.0之后的解决办法

GitHub README.md

To use Butter Knife in a library, add the plugin to your buildscript:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
  }
}

and then apply it in your module:

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'

Now make sure you use R2 instead of R inside all Butter Knife annotations.

class ExampleActivity extends Activity {
  @BindView(R2.id.user) EditText username;
  @BindView(R2.id.pass) EditText password;
...
}

butterknife组件化开发library中R类问题的批量解决方案

错误: 需要常量表达式

switch case的场景下,R.id.beijian处会提示该错误:

private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionevent) {
        int action = motionevent.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                switch (view.getId()) {
                    case R.id.beijian:
                        if (playingResource != null) {
                            new AsyncPTZCtrol().start(playingResource.getDeviceSerial(), playingResource.getChannelNo(), PTZCommand.PTZCommandShrink, PTZAction.PTZActionStart, 3);
                        }
                        break;
                    ...
                    default:
                        break;
                }
                break;
...
}

在R文件中可以看到:没有final修饰

D:\SVN\studioCloudAPP\videopatrol\build\generated\source\r\debug\com\chaos\videopatrol\R.java
public static int beijian=0x7f0e0173;
D:\SVN\studioCloudAPP\videopatrol\build\generated\source\r\debug\com\chaos\videopatrol\R2.java
@IdRes
public static final int beijian = 0x7f0e0173;

解决办法: 除了改写为if else,可以直接使用butterknife生成的R2

猜你喜欢

转载自blog.csdn.net/u010144805/article/details/81912187
今日推荐