Butterknife: AndroidX support in butterknife gradle plugin

Created on 27 Jun 2018  路  10Comments  路  Source: JakeWharton/butterknife

I've tried migrating our project to AndroidX and even using 9.0.0-SNAPSHOT we get compile errors. Looks like the errors are coming from a library module that uses the butterknife gradle plugin.

Here's an example error:

butterknifepluginandroidxissue/lib/build/generated/not_namespaced_r_class_sources/debug/generateDebugRFile/out/kozinski/marcin/lib/R2.java:4: error: package android.support.annotation does not exist
import android.support.annotation.AttrRes;
                                 ^

Looks like the plugin doesn't check if the project is using AndroidX and always generates an R2 class with references to android.support annotations.

I've uploaded a sample project that has this issue: https://github.com/marcin-kozinski/butterknifepluginandroidxissue
You can go back to the initial commit to check that the project is compiling successfully before the migration to AndroidX.

bug

Most helpful comment

Just increase butter knife versions to 10.0.0
And then in build.gradle, add this
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

All 10 comments

That project is using version 8.5.1 which is expected to fail.

Right, sorry about that. Pushed a commit that updates to 9.0.0-SNAPSHOT. The error remains.

The Gradle plugin wasn't updated to know about AndroidX and there's no great way to know on way or the other. For now we can use the property, but the property is only required when using jetifier which will eventually go away.

PR welcome for the Gradle plugin support.

I am also facing the same issue in library project. R2.java shows following error -

error: package android.support.annotation does not exist
error: cannot find symbol class AnimRes
error: cannot find symbol class AttrRes

@marcin-kozinski Did you get any solution for this?

I got the same issue.

For now we can use the property

@JakeWharton , can you be a bit more specific? Many thanks.

@sharmag222cool Unfortunately no, there is no solution so far. If you have the time, feel free to send a PR that fixes it.

@khatv911 I believe it was a note about how a fix for this could be implemented in the library. So not something you could use as a user of the library. Something that could help you implement a PR to fix it in the library.

Have the same problem. Butterknife depends on com.android.support:.....25.3.0
list of dependencies

(./gradlew :app:dependencies)

image

I'm having the same issue.
Generated classes are referencing android support libraries.

Anyone with a workaround for this yet?

@Eipeks You can try my fixed build until my PR is merged to master.

buildscript {
    repositories {
        maven { url 'https://jitpack.io' }
    }

    dependencies {
        // XXX classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT'
        classpath 'com.github.brudaswen.butterknife:butterknife-gradle-plugin:issue_1300-SNAPSHOT'
    }
}

Just increase butter knife versions to 10.0.0
And then in build.gradle, add this
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

Was this page helpful?
0 / 5 - 0 ratings