Butterknife @generate not working{Error:(23, 6) er

2019-07-27 01:34发布

问题:

build.grable(root)app

apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion '25.0.0'
defaultConfig {
    minSdkVersion 18
    targetSdkVersion 23
    multiDexEnabled true
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner    "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),          'proguard-rules.pro'
    }
}
productFlavors {
}
dexOptions {
    incremental true
    javaMaxHeapSize '2g'
    jumboMode = true
    preDexLibraries =true
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support:design:25.3.0'
compile 'com.jakewharton:butterknife:8.5.1'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:support-v4:25.3.0'
compile 'com.android.support:support-annotations:25.3.0'
androidTestCompile 'com.android.support:support-annotations:25.3.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

build.gradle

package com.pocketfeeds.app1;

import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.Bind;

public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private static final int REQUEST_SIGNUP = 0;

@Bind(R.id.input_email) EditText _emailText;
@Bind(R.id.input_password) EditText _passwordText;
@Bind(R.id.btn_login) Button _loginButton;
@Bind(R.id.link_signup) TextView _signupLink;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    ButterKnife.bind(this);

    _loginButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            login();
        }
    });

   _signupLink.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Start the Signup activity
            Intent intent = new Intent(getApplicationContext(),         SignupActivity.class);
            startActivityForResult(intent, REQUEST_SIGNUP);
            finish();
            overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        }
    });
}

public void login() {
    Log.d(TAG, "Login");

    if (!validate()) {
        onLoginFailed();
        return;
    }

    _loginButton.setEnabled(false);

    final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this,
            R.style.AppTheme_Dark_Dialog);
    progressDialog.setIndeterminate(true);
    progressDialog.setMessage("Authenticating...");
    progressDialog.show();

    String email = _emailText.getText().toString();
    String password = _passwordText.getText().toString();

    // TODO: Implement your own authentication logic here.

    new android.os.Handler().postDelayed(
            new Runnable() {
                public void run() {
                    // On complete call either onLoginSuccess or onLoginFailed
                    onLoginSuccess();
                    // onLoginFailed();
                    progressDialog.dismiss();
                }
            }, 3000);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SIGNUP) {
        if (resultCode == RESULT_OK) {

            // TODO: Implement successful signup logic here
            // By default we just finish the Activity and log them in automatically
            this.finish();
        }
    }
}

@Override
public void onBackPressed() {
    // Disable going back to the MainActivity
    moveTaskToBack(true);
}

public void onLoginSuccess() {
    _loginButton.setEnabled(true);
    finish();
}

public void onLoginFailed() {
    Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();

    _loginButton.setEnabled(true);
}

public boolean validate() {
    boolean valid = true;

    String email = _emailText.getText().toString();
    String password = _passwordText.getText().toString();

    if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
        _emailText.setError("enter a valid email address");
        valid = false;
    } else {
        _emailText.setError(null);
    }

    if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
        _passwordText.setError("between 4 and 10 alphanumeric characters");
        valid = false;
    } else {
        _passwordText.setError(null);
    }

    return valid;
}
}
  • Make sure you have latest Butterknife lib on your classpath
  • Right click on usage of desired layout reference (e.g. R.layout.main in your Activity or Fragment), then Generate and Generate ButterKnife Injections
  • Pick injections you want, you also have an option to create ViewHolder for adapters.
  • Click Confirm and enjoy injections in your code with no work!
  • "confirm" it is not working cant injection any code

Error:Execution failed for task':app1:compileReleaseJavaWithJavac'.Compilation failed;see the compiler error output for details.'

回答1:

Add below entries in gradle file

compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

@BindView is the key which is used to bind views.



回答2:

To confirm what Tim mentioned in the comments above, you need to add the following to the dependencies section of your build.gradle:

annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

While you have the ButterKnife libraries enabled, the Java compiler is unable to process the @Bind annotations because the ButterKnife Annotation Processor has not been enabled. This annotation processor expands ButterKnife-specific statements into a format that Android can natively understand.

Also make sure that you have the jcenter and mavenCentral repositories enabled in your build.gradle:

repositories {
    mavenCentral()
    jcenter()
}

EDIT I'm an idiot. Thanks to Sourav Ganguly for pointing out that @BindView needs to be used, not @Bind. I've spent way too much time in Dagger recently...

EDIT 2: Regarding your Constant expression error, you also need to include your R file:

import com.pocketfeeds.app1.R


回答3:

Go to Android Studio-> File ->Setting -> Plugins -> Browse Respositories -> Search butterknife -> Install.

then restart your android studio.

It works for me