3

我正在尝试使用 Greenrobot 的 Eventbus 在我的 Android 应用程序中设置一个简单的订阅者,但我遇到了一个 gradle 构建错误。我在下面显示了我的代码。

事件类

public final class OffersProcessedEvent {}

基础片段

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();
    }
}

活动帖子'

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

订阅代码

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

以下是我的错误

构建 Gradle 错误

错误:没有选项 eventBusIndex 传递给注释处理器

错误:任务“:app:compileDebugJavaWithJavac”执行失败。

编译失败;有关详细信息,请参阅编译器错误输出。

Gradle 依赖项

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'
}
4

2 回答 2

5
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')
    }
}
于 2018-10-11T11:25:02.637 回答
3

基于此链接http://greenrobot.org/eventbus/documentation/faq/

当您设置构建脚本并且您尚未向代码添加任何注释时会发生这种情况。如果没有任何 @Subscribe 注释,用于索引的注释处理器将不会被触发来使用 'eventBusIndex' 选项。因此,只需添加一些 @Subscribe 注释就可以了。

所以你必须添加

@Subscribe
fun onEvent(event: SomeEvent) {

}

但你是钢铁得到Error:No option eventBusIndex passed to annotation processor

将此添加到您的app.gradle

如果使用annotationProcessor

defaultConfig {
    //other code

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

如果使用kapt

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

如果使用android-apt

dependencies {
//other codes
}

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

这个文件将由 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;
        }
    }
}

希望有帮助

于 2018-11-24T12:05:48.103 回答