Hot questions for Using Butter Knife in dagger

Question:

I learn how to use dagger2 and Butterknife in my android project, everything is work fine until I inject the view with Butterknife BindView annotation, it display error like this ;

Error:(5, 57) error: cannot find symbol class DaggerNetComponent

this is my DaggerNetComponent code :

public class App extends Application {

private NetComponent mNetComponent;

@Override
public void onCreate() {
    super.onCreate();

    mNetComponent = DaggerNetComponent.builder().appModule(new AppModule(this)).netModule(new NetModule("http://example.com/"))
            .build();
}

public NetComponent getNetComponent()
{
    return mNetComponent;
}

}

and this is how I inject my view with butterknife :

public class MainActivity extends AppCompatActivity implements MainScreenContact.View {

@BindView(R.id.listContact)
ListView listView;

ArrayList<CharSequence> list;
ArrayAdapter<CharSequence> adapter;

@Inject
MainScreenPresenter mainPresenter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(this);

if I remove the BindView annotation and ButterKnife bind on this class, then it works fine, but if I use it, the error will appear.

this is my Gradle.app

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'android-apt'


android {
compileSdkVersion 25
buildToolsVersion "24.0.3"
defaultConfig {
    applicationId "com.project.echo.contactmanagement"
    minSdkVersion 14
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner      "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

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

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'

//Retrofit
compile 'com.squareup.retrofit2:retrofit:2.0.2'
//OkHttp
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okio:okio:1.7.0'

//Gson
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'

//RxJava
compile 'io.reactivex:rxjava:1.1.2'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'

testCompile 'junit:junit:4.12'

//Dagger
apt 'com.google.dagger:dagger-compiler:2.2'
compile 'com.google.dagger:dagger:2.2'
provided 'javax.annotation:jsr250-api:1.0'

//butterknife
compile 'com.jakewharton:butterknife:8.1.0'
apt 'com.jakewharton:butterknife-compiler:8.1.0'
}

Please help me, any answer will be appreciate...

Thank you..


Answer:

I was able to fix this by using the same annotation plugin for both dependencies:

// Butter Knife
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
// Dagger 2
apt 'com.google.dagger:dagger-compiler:2.8'
compile 'com.google.dagger:dagger:2.8'
provided 'javax.annotation:jsr250-api:1.0'

Came up with this fix after reading the following issue:

https://github.com/JakeWharton/butterknife/issues/803

Question:

Hello my fellow developers,

I'm trying to switch from Gradle to Buck and have problems setting it up with Dagger and Butterknife since they need annotation processing.

I found a few links and tutorials, but they are already a few (~4) years old and I thought there might be a little bit newer approach on doing things.

This is what I looked at already: https://github.com/ryan-endacott/android-buck-dagger-starter

Thanks in advance, Patrick


Answer:

Buck supports annotation processing for Java-based rules (including android_library). Take a look at how Immutables are used in buck repository (link to source code):

  1. First of all, you need to declare library jars:
    java_library(
        name = "immutables",
        exported_deps = [
            ":builder",
            ":value",
        ],
        visibility = [
            "PUBLIC",
        ],
    )

    prebuilt_jar(
        name = "value",
        binary_jar = "value-2.5.6.jar",
        source_jar = "value-2.5.6-sources.jar",
        licenses = [
            "COPYING",
        ],
    )

    prebuilt_jar(
        name = "builder",
        binary_jar = "builder-2.5.6.jar",
        source_jar = "builder-2.5.6-sources.jar",
        licenses = [
            "COPYING",
        ],
    )
  1. Define annotation processor:
    java_annotation_processor(
        name = "processor",
        isolate_class_loader = True,
        processor_class = "org.immutables.value.internal.$processor$.$Processor",
        visibility = [
            "PUBLIC",
        ],
        deps = [
            ":immutables",
        ],
    )
  1. You can then add this annotation processor to the list of plugins of the targets:
    java_library(
        name = "target",
        plugins = [
            "//third-party/java/immutables:processor",
        ],
        deps = [
            ...
        ],
        ...
    )

Buck repository contains a custom rule (java_immutables_library) that adds this annotation processor so that developers don't need to specify plugins on every target. You can probably use similar approach.