EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

NoClassDefFoundError when calling Method.getAnnotation in SubscriberMethodFinder

Open freekering opened this issue 9 years ago • 2 comments

In Android, android.webkit.JavascriptInterface annotation exists on API Level 17 or later and its retention policy is runtime.

package android.webkit;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Annotation that allows exposing methods to JavaScript. Starting from API level
 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} and above, only methods explicitly
 * marked with this annotation are available to the Javascript code. See
 * {@link android.webkit.WebView#addJavascriptInterface} for more information about it.
 *
 */
@SuppressWarnings("javadoc")
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface JavascriptInterface {
}

So, calling Method.getAnnotation to method having this annotation in SubscriberMethodFinder occurs NoClsssDefFoundError if it runs under low API level(<17) device.

java.lang.NoClassDefFoundError: android/webkit/JavascriptInterface
               at java.lang.reflect.Method.getAnnotation(Native Method)
               at java.lang.reflect.Method.getAnnotation(Method.java:275)
               at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingReflectionInSingleClass(SubscriberMethodFinder.java:165)
               at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingInfo(SubscriberMethodFinder.java:88)
               at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:64)
               at org.greenrobot.eventbus.EventBus.register(EventBus.java:136)
               ...

For example, if I make subscriber like following code and try registering it, EventBus will throws NoClassDefFoundError under low API level(<17) device.

import android.webkit.JavascriptInterface;
...

public class Subscriber {
    ...
    @JavascriptInterface
    public void jsCallback() {
        ....
    }

    @Subscribe
    public void onEvent(Event e) {
        ....
    }
    ....
}

Thank you.

freekering avatar Apr 07 '16 07:04 freekering

In the meanwhile you might be able to work around this by using a subscriber index which avoids using reflection at runtime (and is faster). -ut

greenrobot-team avatar Jun 28 '16 12:06 greenrobot-team

@greenrobot-team I meet the same question with it. I try to use subscriber index by android-apt method.And generate MyEventBusIndex.class success.But it is not useful! The same error is still exist.That is mean it also use reflection? In addition,Should I use @Subcriber instead of @Subscribe?But I not found @Subscriber in the project.

UltramanTIGA avatar Jan 04 '18 08:01 UltramanTIGA