EventBus 注释处理器在 AndroidStudio 3.1 中不接受 ThreadMode.MAIN_ORDERED

EventBus annotation processor won't accept ThreadMode.MAIN_ORDERED in AndroidStudio 3.1

我目前正在使用 AndroidStudio 3.1 Canary 3 和 D8。我试过没有 D8,也试过 3.1.0。一致的问题是 MAIN_ORDERED 在无法找到 ENUM 的注解处理器中抛出错误。但是,当我检查 EventBus 代码时,ENUM 就在那里。 Android Studio 还会自动完成 MAIN_ORDERED 枚举。

在我的 Activity 我有:

@Subscribe(threadMode = ThreadMode.MAIN_ORDERED) public void fragmentDone(FragmentResult msg) {

由于注解处理器的错误,构建失败:

07:28:26.857 [ERROR] [system.err] error: Unexpected error in EventBusAnnotationProcessor: java.lang.EnumConstantNotPresentException: org.greenrobot.eventbus.ThreadMode.MAIN_ORDERED

如果我恢复使用 ThreadMode.MAIN,它就会消失。

我正在使用 org.greenrobot:eventbus:3.1.1org.greenrobot:eventbus-annotation-processor:3.1.0 构建,这似乎是正确的版本。

20:47:37.023 [ERROR] [system.err] java.lang.EnumConstantNotPresentException: org.greenrobot.eventbus.ThreadMode.MAIN_ORDERED
20:47:37.129 [ERROR] [system.err]   at sun.reflect.annotation.EnumConstantNotPresentExceptionProxy.generateException(EnumConstantNotPresentExceptionProxy.java:46)
20:47:37.130 [ERROR] [system.err]   at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:84)
20:47:37.130 [ERROR] [system.err]   at com.sun.proxy.$Proxy142.threadMode(Unknown Source)
20:47:37.130 [ERROR] [system.err]   at org.greenrobot.eventbus.annotationprocessor.EventBusAnnotationProcessor.writeCreateSubscriberMethods(EventBusAnnotationProcessor.java:287)
20:47:37.130 [ERROR] [system.err]   at org.greenrobot.eventbus.annotationprocessor.EventBusAnnotationProcessor.writeIndexLines(EventBusAnnotationProcessor.java:373)
20:47:37.130 [ERROR] [system.err]   at org.greenrobot.eventbus.annotationprocessor.EventBusAnnotationProcessor.createInfoIndexFile(EventBusAnnotationProcessor.java:333)
20:47:37.130 [ERROR] [system.err]   at org.greenrobot.eventbus.annotationprocessor.EventBusAnnotationProcessor.process(EventBusAnnotationProcessor.java:106)
20:47:37.130 [ERROR] [system.err]   at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
20:47:37.130 [ERROR] [system.err]   at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705)

这会导致生成损坏的 EventBusIndex.java 文件:-

package com.company.product;

import org.greenrobot.eventbus.meta.SimpleSubscriberInfo;
import org.greenrobot.eventbus.meta.SubscriberMethodInfo;
import org.greenrobot.eventbus.meta.SubscriberInfo;
import org.greenrobot.eventbus.meta.SubscriberInfoIndex;

import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

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

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

        putIndex(new SimpleSubscriberInfo(com.company.product.activities.MainActivity.class, true,
                new SubscriberMethodInfo[] {

再次查看 import org.greenrobot.eventbus.ThreadMode; 文件,枚举看起来包含 MAIN_ORDERED 声明。

我没主意了...

已发布 EventBusAnnotationProcessor 的更新。
请检查问题 thread.

Gradle依赖

annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'

成功生成索引

import org.greenrobot.eventbus.meta.SimpleSubscriberInfo;
import org.greenrobot.eventbus.meta.SubscriberMethodInfo;
import org.greenrobot.eventbus.meta.SubscriberInfo;
import org.greenrobot.eventbus.meta.SubscriberInfoIndex;

import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/** 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("onEvent", MainActivity.TestEvent.class, ThreadMode.MAIN_ORDERED),
        }));

    }

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