Skip to content

NoClassDefFoundError when calling Method.getAnnotation in SubscriberMethodFinder #292

@freekering

Description

@freekering

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions