JakeWharton / butterknife

Bind Android views and callbacks to fields and methods.
http://jakewharton.github.io/butterknife/
Apache License 2.0
25.56k stars 4.6k forks source link

Injection does not work in library projects #100

Closed jrgonzalezg closed 10 years ago

jrgonzalezg commented 10 years ago

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 commented 10 years ago

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:

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 commented 9 years ago

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 commented 9 years ago

Jake, Shall we expect any improvements on this?

JakeWharton commented 9 years ago

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 https://github.com/JakeWharton/butterknife/issues/100#issuecomment-146288280 .

Limitin commented 9 years ago

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 commented 9 years ago

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

kongsonida commented 8 years ago

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

What should we do ?

peacepassion commented 8 years ago

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

JakeWharton commented 8 years ago

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 https://github.com/JakeWharton/butterknife/issues/100#issuecomment-232245082, or mute the thread https://github.com/notifications/unsubscribe/AAEEEXUr4l4srMtocssWP53WzJG2IsHiks5qVFkrgaJpZM4Bg7MN .

rogerhu commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

@mohamed-elwy See #690

elye commented 7 years ago

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.

ghost commented 7 years ago

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