Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoClassDefFoundError when calling Method.getAnnotation in SubscriberMethodFinder #292

Open
freekering opened this issue Apr 7, 2016 · 2 comments · May be fixed by #293
Open

NoClassDefFoundError when calling Method.getAnnotation in SubscriberMethodFinder #292

freekering opened this issue Apr 7, 2016 · 2 comments · May be fixed by #293
Assignees

Comments

@freekering
Copy link

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.

@greenrobot-team
Copy link
Collaborator

greenrobot-team commented Jun 28, 2016

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

@UltramanTIGA
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants