Greenrobot Android Eventbus - No option eventbusindex passed to annotation processor

2.1k views Asked by At

I'm trying to set up a simple subscriber in my Android application using Greenrobot's Eventbus, but I am getting a gradle build error. I have shown my code below.

Event class

public final class OffersProcessedEvent {}

Base Fragment

public class BaseFragment extends Fragment {
    private boolean registered;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        registered = true;
   }

    @Override
    public void onResume() {
        super.onResume();
        if (!registered) {
        EventBus.getDefault().register(this);
        registered = true;
    }
}

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
        registered = false;
    }

    public AppCompatActivity getAppCompatActivity() {
        return (AppCompatActivity) getActivity();
    }
}

Event post'

EventBus.getDefault().post(new OffersProcessedEvent());

Subscribe code

@Subscribe
    public void onMessageEvent(OffersProcessedEvent event){
        *do whatever*
    }

The following are my errors

Build Gradle Errors

Error:No option eventBusIndex passed to annotation processor

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

Gradle dependencies

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    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:26.+'
    compile 'com.android.support:design:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.facebook.android:facebook-android-sdk:4.5.0'
    compile 'com.jakewharton:butterknife:8.6.0'
    compile 'com.jakewharton:butterknife-compiler:8.6.0'
    compile 'com.jakewharton:butterknife:8.7.0'
    compile 'com.jakewharton:butterknife-compiler:8.7.0'
    compile 'com.github.bumptech.glide:glide:3.5.2'
    compile 'com.android.support:cardview-v7:26.0.0-alpha1'
    compile 'com.google.code.gson:gson:2.8.1'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}
2

There are 2 answers

0
Radesh On

Base on this link http://greenrobot.org/eventbus/documentation/faq/

This happens when you set up the build script, and you did not add any annotations to your code yet. Without any @Subscribe annotations, annotation processor for indexing will not be triggered to consume the ‘eventBusIndex’ option. So, just add some @Subscribe annotations and you should be fine.

So you must add

@Subscribe
fun onEvent(event: SomeEvent) {

}

But is you steel getting Error:No option eventBusIndex passed to annotation processor

Add this to your app.gradle

If using annotationProcessor

defaultConfig {
    //other code

   javaCompileOptions {
       annotationProcessorOptions {
           arguments = [eventBusIndex: "com.example.myapp.MyEventBusIndex"]
       }
   }
}

If using kapt

dependencies {
//other codes
}
kapt {
    arguments {
        arg('eventBusIndex', 'com.example.myapp.MyEventBusIndex')
    }
}

And if using android-apt

dependencies {
//other codes
}

apt {
    arguments {
        eventBusIndex "com.example.myapp.MyEventBusIndex"
    }
}

And this file will be generate by EventBus

/** This class is generated by EventBus, do not edit. */
public class MyEventBusIndex implements SubscriberInfoIndex {
    private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;

    static {
        SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();

        putIndex(new SimpleSubscriberInfo(MainActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onBleClick", com.example.myapp.Events.BleClickListener.class),
        }));

    }

    private static void putIndex(SubscriberInfo info) {
        SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);
    }

    @Override
    public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {
        SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);
        if (info != null) {
            return info;
        } else {
            return null;
        }
    }
}

Hope it's help

0
RamKr On
apply plugin: 'kotlin-kapt' // ensure kapt plugin is applied

dependencies {
    implementation 'org.greenrobot:eventbus:3.1.1'
    kapt 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

kapt {
    arguments {
        arg('eventBusIndex', 'com.example.myapp.MyEventBusIndex')
    }
}