Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injection does not work in library projects #100

Closed
jrgonzalezg opened this issue Feb 11, 2014 · 14 comments
Closed

Injection does not work in library projects #100

jrgonzalezg opened this issue Feb 11, 2014 · 14 comments

Comments

@jrgonzalezg
Copy link

I am having problem with ButterKnife injections on library projects. Whenever i try any view injection such as:

@InjectView(R.id.content_frame)
protected FrameLayout contentFrame;

I get a compile time error: "error: attribute value must be constant" on the "R.id.content_frame". This occurs both using Android Studio and latest gradle plugin builds.

Any idea why?

@JakeWharton
Copy link
Owner

Yes this is not supported. This is a tradeoff between ease-of-use and what I tolerate as a sane API.

The only way to accomplish something like this is to allow string values like this:

@InjectView(name = "content_frame")
protected FrameLayout contentFrame;

The downside's to this are:

  • It's not rename-safe or compile-safe (kind of). If you open the layout XML for this and do an IDE-backed rename operation it will not catch this string and update it. It also means you can (technically) compile the above code if there is no content_frame ID that exists. Now the compilation will eventually fail because the generated code will become invalid.
  • It's hard to know what R class to reference in the generated code, especially when you take into consideration the fact that you can use references to IDs that exist in libraries that you are referencing.
  • It forces us to have defaults for both the value and name properties on each annotation which means you can write @InjectView Foo foo and Butter Knife will have to fail the builder rather than javac.

Because of these facts, I have chosen not to support library projects.

One way that this could potentially be solved is to use a Gradle plugin rather than an annotation processor. I don't have the time to explore something like that for a few months though.

@Limitin
Copy link

Limitin commented Aug 27, 2015

Jake, is there any way to support library projects in the future? I love Butterknife in my other projects, but I am currently working on one that uses a shared "library" codebase to make three apps that mostly do style and resource overrides. Having Butterknife supporting the library project would be amazing.

@ponsuyambu
Copy link

Jake, Shall we expect any improvements on this?

@JakeWharton
Copy link
Owner

No. It's unlikely to ever be supported.

On Wed, Oct 7, 2015 at 2:37 PM Ponsuyambu Velladurai <
notifications@github.com> wrote:

Jake, Shall we expect any improvements on this?


Reply to this email directly or view it on GitHub
#100 (comment)
.

@Limitin
Copy link

Limitin commented Oct 7, 2015

Aw. I would love to be using this for a library module I am working on for my company. The library serves as the base application project with all of the shared code between most of the apps my company makes.

@steffandroid
Copy link

As Butterknife won't support this, the next best option is probably Butterfork.

@kongsonida
Copy link

The lib version 7.0.1 is not support @InjectView(name = "content_frame")

What should we do ?

@peacepassion
Copy link

Butterfork is a solution. But I found another solution which used more easier. Ref ButterCookie.

@JakeWharton
Copy link
Owner

That isn't enough information to be an actionable bug report.

On Tue, Jul 12, 2016 at 11:19 PM liuli notifications@github.com wrote:

I want to use it in libary with R2,but when i user the binded view ,report
nullpointException,why?


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
#100 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAEEEXUr4l4srMtocssWP53WzJG2IsHiks5qVFkrgaJpZM4Bg7MN
.

@rogerhu
Copy link

rogerhu commented Jul 18, 2016

The README instructions are somewhat misleading, the Gradle plugin for library projects only create a seperate R2 class. I believe you still need to include the Dagger compiler too as well right?

https://github.com/JakeWharton/butterknife/blob/master/sample/library/build.gradle

@mohamed-elwy
Copy link

mohamed-elwy commented Jul 22, 2016

i followed the README, but i get an error (cannot resolve symbol 'R2'), did i miss something?!

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.2.1'
        classpath 'com.oguzbabaoglu:butterfork-plugin:2.0.0'
    }
}

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.oguzbabaoglu.butterfork-plugin'
apply plugin: 'android-apt'

dependencies {
    compile 'com.jakewharton:butterknife:8.2.1'
    apt 'com.jakewharton:butterknife-compiler:8.2.1'
    // Dagger 2 and Compiler
    compile 'com.google.dagger:dagger:2.0.1'
    apt "com.google.dagger:dagger-compiler:2.0.1"
}

@davidandreoletti
Copy link

davidandreoletti commented Jul 24, 2016

@mohamed-elwy See #690

maimoonak pushed a commit to opensrp/opensrp-client that referenced this issue Jul 25, 2016
…uired for vaccinator app. Site should add it to module lib if needed. (JakeWharton/butterknife#100)
@elye
Copy link

elye commented Feb 24, 2017

Great to have now Butterknife supported in Library using R2.

I face a new issue, where if I have library A viewX, inheriting from library B viewY... I notice my butterknife injected in library B viewY is not working. (If I move viewY to library A, everything works).

Is this expected issue? or it shouldn't be an issue. i.e. my code issue somewhere.

@nethergrim
Copy link

nethergrim commented May 23, 2017

In order to use Butterknife in an Instant App, you also have to use Butterknife gradle plugin to generate class R2.java .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests