Home » Android » Butter Knife – Inject on Android lib

Butter Knife – Inject on Android lib

Posted by: admin June 15, 2020 Leave a comment


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

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

How to&Answers:

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

Library projects

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

buildscript {
  repositories {
  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

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


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


If your Fragment is in an Android Library Project, please read this:



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


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.