Butterknife: Importing Androidx annotation

Created on 12 Sep 2018  Â·  16Comments  Â·  Source: JakeWharton/butterknife

the generated ViewBinding class imports annotation from android package instead of androidx package hence the error

error: package android.support.annotation does not exist
error: cannot find symbol class UiThread
error: cannot find symbol class CallSuper

Gradle Properties

repositories { google() mavenCentral() maven { name 'Sonatype SNAPSHOTs' url 'https://oss.sonatype.org/content/repositories/snapshots/' } jcenter() }

dependencies { classpath 'com.android.tools.build:gradle:3.3.0-alpha09' classpath 'com.google.gms:google-services:4.0.1' classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }

implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

Needs Info

Most helpful comment

Increasing butter knife version has worked for us (:

All 16 comments

Please provide a full sample that demonstrates the issue. This was fixed in #1289.

here is an excerpt.
AddEmployeeActivity_ViewBinding.java

import android.support.annotation.CallSuper;
import android.support.annotation.UiThread;
import android.view.View;
import android.widget.Spinner;
import androidx.appcompat.widget.Toolbar;
import butterknife.Unbinder;
import butterknife.internal.Utils;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import java.lang.IllegalStateException;
import java.lang.Override;

public class AddEmployeeActivity_ViewBinding implements Unbinder {
  private AddEmployeeActivity target;

  @UiThread
  public AddEmployeeActivity_ViewBinding(AddEmployeeActivity target) {
    this(target, target.getWindow().getDecorView());
  }

}

We have a test that indicates this behaves correctly (https://github.com/JakeWharton/butterknife/blob/83216d86d22a2e3c0df5cc6d117877a75a1ddac3/butterknife-runtime/src/test/java/butterknife/BindViewTest.java#L62-L106) so I need more information than that.

The generated class is supposed to import

androidx.annotation.CallSuper NOT android.support.annotation.CallSuper

androidx.annotation.UiThread NOT android.support.annotation.UiThread

the above test imports from androidx package. please also refer to my first post for gradle setup

below is the complete generated class

// Generated code from Butter Knife. Do not modify!
package com.idealorb.payrollapp;

import android.support.annotation.CallSuper; 
import android.support.annotation.UiThread; 
import android.view.View;
import android.widget.Spinner;
import androidx.appcompat.widget.Toolbar;
import butterknife.Unbinder;
import butterknife.internal.Utils;
import com.google.android.material.button.MaterialButton;
import java.lang.IllegalStateException;
import java.lang.Override;

public class AddEmployeeActivity_ViewBinding implements Unbinder {
  private AddEmployeeActivity target;

  @UiThread
  public AddEmployeeActivity_ViewBinding(AddEmployeeActivity target) {
    this(target, target.getWindow().getDecorView());
  }

  @UiThread
  public AddEmployeeActivity_ViewBinding(AddEmployeeActivity target, View source) {
    this.target = target;


    target.saveButton = Utils.findRequiredViewAsType(source, R.id.save_button, "field 'saveButton'", MaterialButton.class);
    target.toolbar = Utils.findRequiredViewAsType(source, R.id.add_employee_toolbar, "field 'toolbar'", Toolbar.class);
  }

  @Override
  @CallSuper
  public void unbind() {
    AddEmployeeActivity target = this.target;
    if (target == null) throw new IllegalStateException("Bindings already cleared.");
    this.target = null;


    target.saveButton = null;
    target.toolbar = null;
  }
}

Gradule App module

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.jakewharton.butterknife'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.idealorb.payrollapp"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        android.defaultConfig.vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-rc02'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0-rc02'
    implementation 'com.google.android.gms:play-services-auth:16.0.0'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-rc02'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01'
    implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha05'
    implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha05'
    implementation 'androidx.preference:preference:1.0.0-rc02'
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

The test I linked does import from androidx.*

I'm facing the same issue. Could I be missing anything? Below are more details.

More Details:

  • Migrated to AndroidX using Android Studio Canary 10
  • Gradle:
...
compileSdkVersion 28

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
...
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
  • Android Studio Version:
    > Android Studio 3.3 Canary 10
    > Build #AI-182.4129.33.33.4996246, built on September 7, 2018
    > JRE: 1.8.0_152-release-1248-b01 amd64
    > JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    > Windows 10 10.0

image

image

It only works with 9.0.0-SNAPSHOT

@JakeWharton sorry, I tried again, but gradle sync fails:

ERROR: Could not find com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT.
Searched in the following locations:

Gradle Updates made based on code provided by @CodeIsmail :

...
dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0-alpha10'
        classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT'
...
...
apply plugin: 'com.jakewharton.butterknife'
...
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
...



md5-79cd7ac371405db36961e82fb5b10474



...
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation 'com.jakewharton:butterknife:9.0.0-SNAPSHOT'
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT'
...

@SyedNabil Check the question I asked on stack overflow with respect to the "ERROR: Could not find com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT." error https://stackoverflow.com/questions/52294319/why-is-butterknife-9-0-0-snapshot-not-resolving

@CodeIsmail Thank you! Is it working fine for you now? I see your comment on this answer says:

Yes. the generated ViewBinding class keeps importing annotation from android package instead of androidx package

I undid the migration to androidx at the moment as i had to get some things done. BTW, do you think it makes sense to use migrate to androidx for an app that needs to be release 3 months from now?

@SyedNabil sadly no. I was hoping to get help on this platform. Personally, yes, it makes sense even though there aren't enough resources to help when you encounter a blocker. You should consider the trade-offs before migrating!

@CodeIsmail Thank you, The solution here seems to have worked for someone. Have you tried it?

@CodeIsmail I really wanted to update the project to androidx.. Tried everything out there and a certain configuration finally worked for me. I have posted the answer to your stackoverflow question.
Hope it helps! Let me know if it worked.

@SyedNabil Thank you! I made some little tweaks to your answer and it worked. here

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

Increasing butter knife version has worked for us (:

Was this page helpful?
0 / 5 - 0 ratings