Butter Knife - 在Android lib上注入

use*_*598 7 android gradle android-studio android-gradle-plugin butterknife

我使用Gradle在Android Studio上工作.

我的问题是案例标签中的非常量字段.

当我在Android lib中使用Butter Knife时,我收到以下错误:

tutuFragment.java:31: error: attribute value must be constant
    @InjectView(R.id.noContactTV)
Run Code Online (Sandbox Code Playgroud)

有没有人遇到过同样的问题,如果有的话,有解决方案吗?

pio*_*543 16

根据https://github.com/JakeWharton/butterknife

图书馆项目

要在库中使用Butter Knife,请将插件添加到您的buildscript中:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
  }
}
Run Code Online (Sandbox Code Playgroud)

然后将其应用于您的模块:

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
Run Code Online (Sandbox Code Playgroud)

现在确保在所有Butter Knife注释中使用R2而不是R.

class ExampleActivity extends Activity {
  @BindView(R2.id.user) EditText username;
  @BindView(R2.id.pass) EditText password;
...
}
Run Code Online (Sandbox Code Playgroud)

所以现在可以Butterknife在Android库中使用注入.

希望它会有所帮助


小智 11

Butterknife目前不支持图书馆项目,请参阅https://github.com/JakeWharton/butterknife/issues/100获取更多信息.