I'm developing an Android Wear extension to an existing app. While working on Android Studio I'm getting the following every time I'm running the wear module AFTER I run the main app, and the wear app crashes on start:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.mobile.activities.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.mobile-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.mobile-2/lib/x86, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
The only solution I found for this is File > Invalidate Caches/Restart and then Build > Clean project.
After that I can run the wear module (until the next main run).
This makes my work very slow. Can someone help me here?
ClassNotFoundException was thrown when an application tries to load in a class through its string name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader .
The [loadClass](https://developer.android.com/reference/java/lang/ClassLoader.html#loadClass(java.lang.String, boolean)) method in class ClassLoader.
but no definition for the class with the specified name could be found.
As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The "optional exception that was raised while loading the class" that may be provided at construction time and accessed via the getException() method is now known as the cause, and may be accessed via the getCause() method, as well as the aforementioned "legacy method."
Found this SO related ticket, issue was resolved by doing steps below:
Right click on your project and select Properties.
Select Java Build Path from the menu on the left.
Select the Order and Export tab.
From the list make sure the libraries or external jars you added to your project are checked.
Finally, clean your project & run.
You may also check this SO ticket: Android Activity ClassNotFoundException - tried everything
Related
I'm trying to run a program on the phone from android studio. It tries to send an email using javax.mail. When running under the debugger, see these errors in the debug console.
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/sun/mail/util/MailLogger;
at javax.mail.Session.initLogger(Session.java:261)
at javax.mail.Session.<init>(Session.java:245)
at javax.mail.Session.getDefaultInstance(Session.java:356)
at com.example.sendemail.GMailSender.<init>(GMailSender.java:53)
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sun.mail.util.MailLogger" on path: DexPathList[...]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at javax.mail.Session.initLogger(Session.java:261)
...
The class com/sun/mail/util/MailLogger is present in the apk. Found this from 'Analyze APK' (image below). It's in classes.dex. Why isn't it found at run time?
Android Studio version 3.4.1
Android version on phone is 7.0
The application I am working on shows a strange symptom - if one cleans data and cache of the application, the application will crash on the first next run with exception, but on the second run it will run without a problem. Does anybody have any idea what could be going on and how to fix it.
What I already did:
deleted all the build dirs and rebuild
disabled instant run
check that the class is in the apk
tired it on multiple phones - all behave the same
Sadly nothing helped...
I am using the latest Android Studio 3.2.1. Compile and target SDK is 28, minimal 21. The app is written in Kotlin. Support lib is 28.0.0 and build tools 28.0.3.
The exception thrown on the first run (the name of the application was changed to protect the innocent):
2018-11-26 18:18:38.780 17135-17135/com.myapp.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.app, PID: 17135
java.lang.RuntimeException: Unable to instantiate service service.ServerNotificationService: java.lang.ClassNotFoundException: Didn't find class "service.ServerNotificationService" on path: DexPathList[[zip file "/data/app/com.myapp.app-YIEEjJIdfXs58PSDfpg4Ew==/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp.app-YIEEjJIdfXs58PSDfpg4Ew==/lib/arm64, /data/app/com.myapp.app-YIEEjJIdfXs58PSDfpg4Ew==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3459)
at android.app.ActivityThread.-wrap4(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: java.lang.ClassNotFoundException: Didn't find class "service.ServerNotificationService" on path: DexPathList[[zip file "/data/app/com.myapp.app-YIEEjJIdfXs58PSDfpg4Ew==/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp.app-YIEEjJIdfXs58PSDfpg4Ew==/lib/arm64, /data/app/com.myapp.app-YIEEjJIdfXs58PSDfpg4Ew==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3456)
at android.app.ActivityThread.-wrap4(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
So I managed to fix it. It was related to how Android tools merge AndroidManifest.xml files.
Let me explain (for posterity):
The application I am building is composed from a core - which is an android library and the actual application. The class service.ServerNotificationService is part of the library and was declared in AndroidManifest.xml of the said library as:
<service android:name="service.ServerNotificationService" />
Which is correct and the app will run - just not with the clean cache (I still do not understand this part, but I presume it has something to do with the black magic which is Android ART). To fix the problem I changed the line to:
<service android:name="com.myapp.app.core.service.ServerNotificationService" />
Which is the full name for the class. The lesson learned here is always use full class name in the libraries...
What is the proper way to import Guava into an Android project? Every time I try to use it I get a NoClassDefFoundError.
This is what I'm doing to generate the crash. I'm using Android Studio 3.0 Canary 7.
Create an new project File > New > New Project, target API 26.0, using the Empty Activity template.
Add to app/build.gradle in the dependencies section
implementation "com.google.guava:guava:20.0"
Add this to the onCreate method in MainActivity.java
ImmutableList<String> foo = ImmutableList.of("A", "B", "C");
Log.d("MainActivity", foo.get(0));
Run the App and open up Logcat to see this exception:
FATAL EXCEPTION: main
Process: com.letsdoit.guavaissue, PID: 14366
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/collect/ImmutableList;
at com.letsdoit.guavaissue.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.common.collect.ImmutableList" on path: DexPathList[[zip file "/data/app/com.letsdoit.guavaissue-1/base.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.letsdoit.guavaissue-1/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.letsdoit.guavaissue.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I'm almost certain it has to do with Gauva being large and not playing well with multidex, but am not sure what to do about it. These are some of the note worthy things I've tried to no avail:
Enabling multidex and specifying ImmutableList in the multiDexKeepFile.
Disabling instant run.
Pulled the APKs from the device and verified the Guava classes are in the APKs.
Following the recommendations in this stack overflow question.
TL;DR
Use guava version 22.0-android and up. Make sure to use the -android flavor, otherwise you'll run into the NoClassDefFoundError.
Explanation
I learned after posting the question how to manually clean the project and uninstall apks from the emulator. It turns out that version 20.0 actually does work, but I had tried version 21.0 right before then and failed to clean.
The non-android flavors of guava as of version 21.0 are using Java 8. The android flavors and versions before 21.0 use Java 7. This is described in these release notes for version 22.0.
I tested these flavors and versions:
20.0 (Java 7) - works
21.0 (Java 8) - doesn't work
22.0 (Java 8) - doesn't work
22.0-android (Java 7) - works
When using version 21.0 or 22.0 (no -android) the ImmutableList class is getting referenced but not compiled into the dex files (since it's in italics). This was causing the NoClassDefFoundError.
APK with dangling references to ImmutableList
As the android developer docs explain
In the tree view, italicized nodes are references that do not have a
definition in the selected DEX file.
It further explains that
A DEX file can reference methods and fields that are defined in a
different a file. For example System.out.println() is a reference to
the println() method in the Android framework.
But in this case, there is no other file that these methods and class definition should end up in. It's just failing to add them.
Contrast that to using 20.0 or 22.0-android, where the ImmutableList class actually gets compiled in.
APK with ImmutableList defined
And the app starts up as expected.
I've been struggling with this for too long now. I hope anyone can help me. Here is my situation:
In our Android app, we route XMLHttpRequests from our WebView via Java/Android so we can add an OAuth2 token and track the requests. This worked fine until Android 5 got released.
Starting Android 5, the following exception is being raised whenever the WebView tries to interact with the exposed object in JavaScript:
java.lang.ClassNotFoundException: Didn't find class "com/company/android/AndroidXMLHttpRequest$AjaxRequest" on path: DexPathList[[zip file "/system/app/webview/webview.apk"],nativeLibraryDirectories=[/system/app/webview/lib/x86_64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:53)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
Suppressed: java.lang.ClassNotFoundException: Invalid name: com/company/android/AndroidXMLHttpRequest$AjaxRequest
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 6 more
Results on different configurations:
This code works fine on physical and virtual devices running Android < 5.
This code breaks on physical devices running Android 5 and 5.1
This code works fine on a virtual device running Android 5.0
This code breaks on virtual devices running Android 5.1
There is no difference between running a debug or production build
Some info about the code:
* WebViewFragment gets initiated in MainActivity.onCreate.
* WebViewFragment.onCreateView creates object to be exposed to WebView
* JavaScript interface gets added in WebViewClient.onPageFinished
* The class that is exposed is Public
* All methods of the class that are exposed are public and have the #JavascriptInterface annotation
Starting Lollipop, WebView has been decoupled from Android so it can be upgraded via the Play Store. This is also something that shows from the error:
DexPathList[[zip file "/system/app/webview/webview.apk"],nativeLibraryDirectories=[/system/app/webview/lib/x86_64, /vendor/lib64, /system/lib64]]
I looked at the release notes to discover if we have to do something different regarding WebView and addJavaScriptInterface but I can't seem to find anything.
Anyone has any ideas what could be the reason that it fails?
Sorry for that, there was a bug in WebView: http://crbug.com/491800
The bug has been introduced in Android 5.1, so the original 5.0 works fine (the emulator). But on physical Lollipop devices with Play Store, WebView gets updated to a later version (43), which was still broken. The fix came out with WebView version 44, which I think is rolling out just now.
However, there is a workaround. In the injected method that accepts an instance of AjaxRequest, you need to replace the argument type to Object, and then convert to AjaxRequest later in the function (see this comment for an example). This way, WebView will stop trying to look up the class which it can't access at that point.
We have very weird java.lang.ClassNotFoundException on app startup that happens to very small amount of customers (< 1%). We never saw it in dev env and we cannot understand the source of this issue. Here is the stack trace from Google play crash reports.
java.lang.RuntimeException: Unable to instantiate application com.mycompany.myapplication.MyApplication: java.lang.ClassNotFoundException: com.mycompany.myapplication.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.mycompany.myapplication-1/pkg.apk]
at android.app.LoadedApk.makeApplication(LoadedApk.java:490)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3784)
at android.app.ActivityThread.access$2200(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1082)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4268)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.mycompany.myapplication.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.mycompany.myapplication-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at android.app.Instrumentation.newApplication(Instrumentation.java:972)
at android.app.LoadedApk.makeApplication(LoadedApk.java:481)
... 11 more
If the issue only occurs on with signed APKs and you are using Eclipse to Export the Signed APK ...
Before you 'Export and sign an APK' in Eclipse turn off the following setting in the menu [Project > Build Automatically] (then can switch it back on later when you continue developing).
I recently deployed an update to my app, and a user reported a crash the very next day via the Google Play reporting facility. The stack dump was for LoadApk() and the error was in the loading of my Application class. Here is that dump:
java.lang.RuntimeException: Unable to instantiate application com.goalstate.WordGames.FullBoard.library.FullBoardApplication: java.lang.ClassNotFoundException: Didn't find class "com.goalstate.WordGames.FullBoard.library.FullBoardApplication" on path: DexPathList[[],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4703)
at android.app.ActivityThread.access$1600(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.goalstate.WordGames.FullBoard.library.FullBoardApplication" on path: DexPathList[[],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.Instrumentation.newApplication(Instrumentation.java:981)
at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
... 11 more
I have a library and my Application class was defined in that library. The manifest for my app (which used the library) referred to the class in that library by its full path. It did not have an application class of its own.
All of my in-house testing had not reproduced this problem, and even when I tested (using the Samsung Remote Test Lab) on the same device (actually two different devices, one running 4.3 and one running 4.4.4) as reported the crash (a Galaxy Note II, running Android 4.4), there was no problem.
In searching for information on this I found mention of the fact that different devices may have slightly different approaches to resolving class references, and that is probably why most devices had no problem with my APK, but this particular device (which, unlike my test devices, was provided with its Android flavor by Sprint) did. And similarly, it may be why just one percent of your own customers had the problem, while most did not.
I decided that the best approach would be to make it as easy as possible for even an unsophisticated device to find the classes that were referenced from my manifest file. So, I defined a new application class within the package of the app itself (rather than the library) and I had that class inherit from the application class in my library. The new class was otherwise empty.
I then replaced the full path reference to the application class in the library with a relative reference to the new class I had created in the app itself. So, instead of having:
<application android:name="com.goalstate.WordGames.FullBoard.library.FullBoardApplication"
in my manifest, I had:
<application android:name=".FullBoardWordChumsApplication"
That (according to folklore on this topic) should make it easier for a less sophisticated resolution process to succeed.
I also took the full path that I had been using in the manifest to name my already-local activity class and made it relative (by simply lopping off everything preceding the final dot).
Additional folklore found online indicates that it may help to turn off "Build Automatically" for the project in Eclipse, then exit Eclipse, re-enter Eclipse, and then, after it rebuilds, go directly to Android Tools to export the signed APK (without ever turning on Build Automatically). So, throwing salt over my left shoulder, and saying a prayer to the gods of fragmentation paranoia, I accommodated this superstition in preparing my APK for release.
Does any of this really help? Time will tell, but so far my updated release has not generated any additional crashes.
This Error occurs when in your manifest file in application tag whatever class name you put it is not match with your coding java file .So make a current both in way.