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

在我的活动中,我有:

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

10-06 03:40