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

Fatal Exception: java.lang.NoClassDefFoundError android/telephony/CellInfoGsm #149

Closed
pankajchandiitm opened this issue Jan 22, 2015 · 22 comments

Comments

@pankajchandiitm
Copy link

Hi,
On specific mobiles eventbus crashes with following error.

Fatal Exception: java.lang.NoClassDefFoundError
android/telephony/CellInfoGsm
de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods
Caused by java.lang.ClassNotFoundException

dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:61)

java.lang.Class.getDeclaredMethods (Class.java:703)

de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods (SubscriberMethodFinder.java:75)

de.greenrobot.event.EventBus.register (EventBus.java:163)

de.greenrobot.event.EventBus.registerSticky (EventBus.java:151)

Looks like findSubscriberMethods is trying to access methods which are marked as @TargetApi(17) while the phone is using api 16. Hence eventbus is not able to find those classes which are not available in api 16. Looks like a bug. Is there any workaround?

@premnirmal
Copy link

Same here, I get the exception:

Fatal Exception: java.lang.NoClassDefFoundError
android/os/PersistableBundle

on specific mobile devices

@greenrobot
Copy link
Owner

This looks like some kind of Android bug. If you have time and know how to do it, you could test if that occurs with the annotations branch. This code is not present anymore here.

@pankajchandiitm
Copy link
Author

It is an issue with reflection in Event Bus, because of proguard Event Bus
is not able to find some classes which it is trying to get via reflection.

Solution that worked for me -

Only required if you use AsyncExecutor

-keepclassmembers class * extends
de.greenrobot.event.util.ThrowableFailureEvent {
*;
}

add this to proguard-project.txt

Pankaj
Co-Founder, EnglishDost
B.Tech, Comp Sci & Engg,
IIT Madras, Class of 2008
9886259533

On Wed, Feb 25, 2015 at 1:32 PM, greenrobot notifications@github.com
wrote:

This looks like some kind of Android bug. If you have time and know how to
do it, you could test if that occurs with the annotations branch. This code
is not present anymore here.


Reply to this email directly or view it on GitHub
#149 (comment).

@greenrobot
Copy link
Owner

So, this can be closed?

@yuzeh
Copy link

yuzeh commented Feb 27, 2015

I'm seeing this issue too. I did a little digging into the code and found the issue.

It does seem to be an Android quirk (specifically in java.lang.Class), but I think it was introduced into EventBus at e47442b.

SubscriberMethodFinder#findSubscriberMethods used to use Class#getMethods(), which doesn't check method signatures (for things such as argument types that don't exist). This was changed to use Class#getDeclaredMethods(), which does check, and throws an exception if something goes wrong.

See lines 763, 770, and 822 in https://android.googlesource.com/platform/libcore/+/a8a489492749b845cc2a85d67c664449d7b0b019/libart/src/main/java/java/lang/Class.java.

The current workaround is to use version 2.3.0, which is what I'm doing for my project. I lied, this change happened before 2.3.0.

@pankajchandiitm
Copy link
Author

Yes that is correct.

Pankaj
Co-Founder, EnglishDost
B.Tech, Comp Sci & Engg,
IIT Madras, Class of 2008
9886259533

On Fri, Feb 27, 2015 at 7:21 AM, Dan Huang notifications@github.com wrote:

I'm seeing this issue too. I did a little digging into the code and found
the issue.

It does seem to be an Android quirk (specifically in java.lang.Class), but
I think it was introduced into EventBus at e47442b
e47442b
.

SubscriberMethodFinder#findSubscriberMethods used to use
Class#getMethods(), which doesn't check method signatures (for things
such as argument types that don't exist). This was changed to use
Class#getDeclaredMethods(), which does check, and throws an exception if
nothing goes wrong.

See lines 763, 770, and 822 in
https://android.googlesource.com/platform/libcore/+/a8a489492749b845cc2a85d67c664449d7b0b019/libart/src/main/java/java/lang/Class.java
.


Reply to this email directly or view it on GitHub
#149 (comment).

@greenrobot
Copy link
Owner

Thanks for investigating. getMethods is much slower than getDeclaredMethods. Maybe in combination with try/catch...

@yuzeh
Copy link

yuzeh commented Feb 27, 2015

You only need to call getMethods once, not for each superclass, so it might
be faster if we add that optimization.

On Fri, Feb 27, 2015 at 12:09 AM, greenrobot notifications@github.com
wrote:

Thanks for investigating. getMethods is much slower than
getDeclaredMethods. Maybe in combination with try/catch...


Reply to this email directly or view it on GitHub
#149 (comment).

nikclayton added a commit to nikclayton/android-squeezer that referenced this issue Jun 10, 2015
- Refactor ImageWorker to provide additional loadImage(...) methods that
  can:
  - load an image, and then call a callback
  - load an image in to a RemoteViews, and post a notification

- Use these new methods in updateOngoingNotification() to load the
  images in to the notification's view and post the new notification.

This causes a problem with EventBus on pre-Lollipop devices, as it uses
Class#getDeclaredMethods() finds methods that do not exist or have
non-existent return types on earlier devices. Guarding the code with
@TargetApi or checks against Build.VERSION.SDK_INT does not solve
this problem.

This is discussed in issues on EventBus, such as
greenrobot/EventBus#149 (comment).

The fork of EventBus at https://github.com/yuzeh/EventBus fixes this,
per the pull request at greenrobot/EventBus#158
and yuzeh/EventBus@dc326b0
so for the moment pull that code in as a submodule and use it in
preference to the official EventBus code.
@greenrobot
Copy link
Owner

Could someone confirm that be04a2d fixed the issue? Will do a 2.4.1 release once confirmed.

@greenrobot
Copy link
Owner

@yuzeh Thanks so much for investigating! Decided to do it as plan B only, because it's not so common and getDeclaredMethods is much faster last time I checked.

@MobileMon
Copy link

removed onRestoreInstanceState from my activity and problem solved

@skyler-b
Copy link

I am still seeing this in EventBus 3.0.0 when a class I'm using EventBus in has some method which references a higher API than the device I am running on.

Please see this tiny sample project:
https://gist.github.com/skyler-b/2bfea0b0bbb4ebdb5080

Which crashes on launch:

java.lang.NoClassDefFoundError: android/webkit/PermissionRequest
at java.lang.Class.getDeclaredMethods(Native Method)
at java.lang.Class.getPublicMethodsRecursive(Class.java:894)
at java.lang.Class.getMethods(Class.java:877)
at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingReflectionInSingleClass(SubscriberMethodFinder.java:157)
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)

This crash is seen when running on a 4.4.2 emulator.

@skyler-b
Copy link

@greenrobot can we re-open this?

@greenrobot
Copy link
Owner

@skyler-b are you using an index for eventbus 3? it might help to work around this because it avoids reflection.

@greenrobot greenrobot reopened this Mar 17, 2016
@skyler-b
Copy link

I hadn't heard of the subscriber index, but yes, after adding it to my project the crash does go away. Great work around, thanks for the tip!

@greenrobot
Copy link
Owner

@skyler-b great.

If it's reproducible for you without the index, would you be available to pinpoint the issue, so we can add a FAQ entry? In #182 it was suggested to remove Activity#onSaveInstanceState(Bundle, PersistableBundle) in favor of Activity#onSaveInstanceState(Bundle).

Which is the subscriber class that is triggering the issue for you? Did you override any methods having a PermissionRequest param? PermissionRequest is API level 21 - what does the affected device have?

@skyler-b
Copy link

@greenrobot if you refer to the gist I posted (https://gist.github.com/skyler-b/2bfea0b0bbb4ebdb5080), it pinpoints the issue pretty concisely. The crash posted occurs on a 4.4.2 emulator.

@greenrobot
Copy link
Owner

@skyler-b Does it help to make the "offending" method private?

@skyler-b
Copy link

@greenrobot Yes - a private method with an advanced API does not cause a crash. For example, in my sample project, making someMethod private does prevent the crash.

@greenrobot
Copy link
Owner

Awesome! I added a rather long FAQ entry for this: http://greenrobot.org/eventbus/documentation/faq/
Let me know if the FAQ can be improved.

Thanks for your help!

@skyler-b
Copy link

Document looks great, thanks for the explanation!

@zhangmingxingAtGit
Copy link

getMethods also occur NoClassDefFoundError on some devices,maybe add catch around getMethods

Method[] methods; 
try { 
	// This is faster than getMethods, especially when subscribers are fat classes like Activities 
	methods = findState.clazz.getDeclaredMethods(); 
} catch (Throwable th) { 
	try { 
		methods = findState.clazz.getMethods();
	}catch (Throwable th2){
		return;
	}finally { 
		findState.skipSuperClasses = true;
	}
}

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.

7 participants