Butter Knife - Inject on Android lib
Asked Answered
S

4

8

I work on Android Studio with Gradle.

My issue is Non-constant Fields in Case Labels.

When I use Butter Knife in Android lib, I get the following error:

tutuFragment.java:31: error: attribute value must be constant
    @InjectView(R.id.noContactTV)

Has anyone experienced the same issue, and if so, have a solution for it?

Sirreverence answered 27/5, 2014 at 11:47 Comment(0)
B
18

According to https://github.com/JakeWharton/butterknife

Library projects

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

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

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;
...
}

So it's now to possible to use Butterknife injects in Android libs.

Hope it will help

Bhayani answered 27/8, 2016 at 20:6 Comment(0)
N
11

Butterknife does not support library projects at this time, please refer to https://github.com/JakeWharton/butterknife/issues/100 for more informataion.

Nyhagen answered 2/12, 2014 at 2:45 Comment(0)
S
0

Official github page has the solution: https://github.com/JakeWharton/butterknife

But when I obeyed the steps to config my library project, I went to some problems, such as NullPointerException, functions marked by @onClick annotition weren't invoked when the views were clicked, etc.

Then I changed somethings, and finally made it. Refer to this: http://blog.csdn.net/ytzys/article/details/53243438

Springspringboard answered 20/11, 2016 at 11:47 Comment(0)
V
0

I have experienced the same issue. I was getting this error whenever I wrote.

@BindView(R.id.pager) ViewPager pager;

or any other similar syntax for a view.

The reason for getting this error was that the R file that was imported into my java file was from the different package.

Now the question is why the R file imported was from a different package?

It was because I was using my project as a library into the other project. And while making the library I gave a different package name.

Ventriloquist answered 14/12, 2016 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.