Skip to content

Injection does not work in library projects #100

@jrgonzalezg

Description

@jrgonzalezg

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?

Activity

JakeWharton

JakeWharton commented on Feb 12, 2014

@JakeWharton
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

Limitin commented on Aug 27, 2015

@Limitin

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

ponsuyambu commented on Oct 7, 2015

@ponsuyambu

Jake, Shall we expect any improvements on this?

JakeWharton

JakeWharton commented on Oct 7, 2015

@JakeWharton
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

Limitin commented on Oct 7, 2015

@Limitin

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

steffandroid commented on Oct 7, 2015

@steffandroid

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

kongsonida

kongsonida commented on Dec 2, 2015

@kongsonida

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

What should we do ?

peacepassion

peacepassion commented on Jun 3, 2016

@peacepassion

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

JakeWharton

JakeWharton commented on Jul 13, 2016

@JakeWharton
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

rogerhu commented on Jul 18, 2016

@rogerhu

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

6 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @JakeWharton@rogerhu@Limitin@davidandreoletti@kongsonida

        Issue actions

          Injection does not work in library projects · Issue #100 · JakeWharton/butterknife