java.lang.ClassNotFoundException on working app
Asked Answered
F

26

54

I have created and published my first Android app. It's very simple. It works fine on simulator and some phones, but I am getting this error:

java.lang.RuntimeException: Unable to instantiate application cz.teamnovak.droid.Novak ESC Track guide: java.lang.ClassNotFoundException: cz.teamnovak.droid.Novak ESC Track guide in loader dalvik.system.PathClassLoader[/data/app/cz.teamnovak.droid-1.apk]
    at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:649)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4232)
    at android.app.ActivityThread.access$3000(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: cz.teamnovak.droid.Novak ESC Track guide in loader dalvik.system.PathClassLoader[/data/app/cz.teamnovak.droid-1.apk]
    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    at android.app.Instrumentation.newApplication(Instrumentation.java:942)
    at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:644)
    ... 11 more

Any idea what can cause this?

Forby answered 23/9, 2010 at 17:49 Comment(4)
Hello well, I have exported by eclipse and I dont see any classes or jars inside. och, it does not work this export.... I have to find out whyForby
Well, there is file classes.dex. It should contain compiled code. Right? The file is there. So why do I get ClassNotFoundException? Skoky.Forby
Hello, one more cosideration. The error says "java.lang.ClassNotFoundException: cz.teamnovak.droid.Novak ESC Track guide" -> the problem is I dont have such a class. Its somehow mixed up. The "Novak ESC Track guide" is name of the application. Pleas advice whats going on. Thank you, Skoky.Forby
For me this topic resolved my problem: https://mcmap.net/q/169155/-unable-to-resolve-superclass-error-when-referencing-jar-libraryMuncy
J
40

Yep, I had this exact same problem. It was because I specified the android:name attribute in the application node in the manifest file.

Your Android Manifest file probably looks something like this:

   <application 
       android:name="Novak ESC Track guide" 
       android:icon="@drawable/icon" 
       android:label="@string/app_name" 
       android:description="@string/help_text" >

Do not use the android:name attribute! unless you've implemented a custom Application object.

The application:name attribute has nothing to do with the name of your app. This is the name of a specific class to load as your Application instance. That's why you would get the ClassNotFoundException if that class wouldn't exist.

For the name of the app use the android:label attribute on this same application node instead.

Remove it and it should work:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:description="@string/help_text" >
Jurisconsult answered 22/2, 2011 at 19:34 Comment(5)
android:name is required if you have your own application class that extends Application. It is the name of your application, so android:name seems like the right terminology to me. I agree it is misleading, though, if you don't need your own application class.Shoeshine
that worked thanks. @RickNotFred, I am confused though. I normally have done this in the past and have not had any problems. I created a class library and made a base application class that I will use on different projects, so I am extending this class and I got this error. How should I go about this?Nidus
If I remove the android:name the app does not start, and no errorsShoebill
I previously had the name attribute in my app and it was working fine. I then updated to Android Studio 2.2.2 and it was no longer working. I removed the name attribute and now it is working. What caused this?Stablish
i dont have name attribute inside application TAG, but the same error.Idolism
G
14

Something like this happened when I changed the build target to 3.2. After digging around I found that a had named the jar lib folder "lib" instead of "libs". I just renamed it to libs and updated the references on the Java build path and everything was working again. Maybe this will help someone...

Goosestep answered 3/4, 2012 at 20:53 Comment(3)
Thanks.. I have same problem and resolve the problem after reading your post. keep it up...Pursuance
Thanks, it solved the problem! I had to edit the libraries in Rightclick Project->Build Path and set them to the new "libs" folder also.Ludvig
I'm totally stuck trying to update the Java Build Path. Where is this in Android Studio?Hogg
C
10

We have a couple of projects where this issue was logged from time to time on the Android Market. I found the following issues in the manifests:

  1. If the package name is com.test then activities names should be .ActivityName (with a leading dot), not just ActivityName.

  2. For some classes, those that appeared in the logs most often, the class name was specified as com.test.Name while it should have been .Name.

I guess many implementations of Android handle these minor issues successfully (this is why the exception never happened in testing), while others few are throwing the exception.

Consonant answered 11/10, 2011 at 15:1 Comment(2)
Good catch. I also had problems with the name attribute of application, where most devices worked just fine with "MyApplicationSubclass", but some would crash on launch. Changing to "my.package.MyApplicationSubclass" fixed the issue. Some versions/vendors are more tolerant to others on these issues.Rutherfordium
Wow you are spot on. I can't believe this. Thanks for sorting this out, but I think this is something Google should fixQuanta
E
5

I have this problem sometimes with eclipse. What has corrected it for me is to go to Project Properties / Android and change the build target API to a different version and republish. I'll find that corrected it, then I can change it back to the desired build target.

or

You may need to check your proguard.cfg.

Assuming you have linked your libraries properly and that your library projects have the code you need marked for export, the next step you might want to do is to check your proguard settings and make sure you are not stripping out the classes you need.

I was struggling with this quite a bit after I had my app working going directly to the emulator or device from eclipse. The problem I was having was after the app was published (i.e. gone through proguard) and run on the device it was missing classes that were contained in the project. They were being stripped out somehow.

My problem may have been caused when I had tried to use IntelliJ and have switched back to eclipse.

Here is the proguard file that worked for me:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembers class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
Exfoliate answered 6/4, 2012 at 16:27 Comment(0)
M
5

Had the same error: java.lang.RuntimeException: Unable to instantiate activity (classnotfound) FIRST try to change the build platform (2.3.3 -> 2.2 -> 2.3.3) worked for me.

Methodius answered 2/8, 2012 at 6:40 Comment(1)
Worked for me too, I had to remove some "@Override" annotations.Ludvig
W
5

This is my observation with respect to the Error. I recently Updated the ADT to 22.0.1. I am getting following Error when i imported my previous Projects "E/AndroidRuntime(24807): Caused by: java.lang.ClassNotFoundException: com.sherl.sherlockfragmentsapp.StartActivity in loader dalvik.system.PathClassLoader[/data/app/com.sherl.sherlockfragmentsapp-1.apk]"

Then I changed "Properties->Java Build Path-> Order and Export" in the following manner [Unable to add the Image because of the Forum rules]

  1. Android Private Libraries - checked
  2. Android 4.2.2 - unchecked
  3. Android Dependencies - checked
  4. /src - selected
  5. /gen - selected

It resolved the issue. Hope this is Help you guys.

Whitebook answered 22/7, 2013 at 10:15 Comment(3)
If the above code is working fine, please accept this as Answer!Whitebook
Where is "Properties"?Hogg
@MartinErlic lol it's from Eclipse IDE, a relic from the 1800'sTrey
A
4

I know this question has been answered, and it likely wasn't the case. But I was getting this error, and figured I'd post why in case it can be helpful to anyone else.

So I was getting this error, and after several hours sheepishly realized that I had unchecked 'Project > Build Automatically'. So although I had no compilation errors, this is why I was getting this error. Everything started working as soon as I realized that I wasn't actually building the project before deploying :-/

Well, that's my story :-)

Agincourt answered 2/9, 2011 at 17:56 Comment(0)
A
4

Had this sort of problem today after upgrading to latest ADT/SDK. Took me quite a while. Checked that i used google-apis (for maps), uses-library, cleaned the project etc.

Deleting the .project and adding a fresh one (create new android project) finally solved it.

Antecedents answered 21/10, 2011 at 22:15 Comment(2)
Where is the .project?Hogg
Only applies if you're using Eclipse. Theres not .project if you're using Android Studio / IntelliJ.Antecedents
R
4

I got this error when I ran my app on earlier versions of android. I thought SearchView was backwards compatible to Android 1.5, but it was created in 3.0. I removed its reference from the code and it worked.

Rapeseed answered 16/7, 2012 at 1:11 Comment(0)
R
3

In my case I had to add android:name=".activity.SkeletonAppActivity" instead of android:name=".SkeletonAppActivity" in the manifest file for the main activity. SkeletonAppActivity was in a different package from the application class. Good luck!

Rubber answered 14/9, 2011 at 16:3 Comment(0)
U
3

Make sure that android:hasCode is not set to false in your manifest file. This is what solved the problem for me!

Uniseptate answered 16/11, 2011 at 19:24 Comment(0)
A
2

Well you have a java.lang.ClassNotFoundException. That means a class is missing in the application runtime. You should check wheather you have added all your libs to the build path.

Right click on your project -> properties -> java build path -> libraries, add your libs or create one containing your classes and enable order export for your libs.

Allwein answered 23/9, 2010 at 18:11 Comment(3)
@bluemunch in the project inspectorAllwein
I see. I'm running Android Studio. Are these instructions for Eclipse? If so, how do I do this in Android Studio?Hogg
@bluemunch it depends. Try #17725142 or create a question on SO.Allwein
B
2

In my case, the icon of the app was causing the error:

<application
    android:name="com.test.MyApp"
    android:icon="@drawable/myicon"

Why? Because I put the icon only in the folder "drawable", and I'm using a high resolution testing device, so it looks in the folder "drawable-hdpi" for the icon. The default behaviour for everything else is use the icons from "drawable" if they aren't in "drawable-hdpi". But for the launching icon this doesn't seem to be valid.

So the solution is to put a copy of the icon (with the same name, of course) in "drawable-hdpi" (or whichever supported resolutions the devices have).

Badminton answered 11/6, 2012 at 14:41 Comment(2)
Was this for the specific question asked related to the java.lang.ClassNotFoundException or was your error another error? I've been trying to figure out why this is happening for our application when we clearly have it defined properly but haven't found anything useful until looking again today to find this... I know we don't have an LDPI resolution icon but we support LDPI devices (expecting to have the device scale down).Orlena
It is about this specific question, only happened testing on hdpi and xhdpi devices, and I haven't added icon for ldpi device. I'm not even sure this fixes it, still got the exception, sometimes, after it, and usually clean the project and build again fixes it...Badminton
S
2

I used a supertype method that was declared 'final' in one of my Activities (specifically the 'isResumed()' method). The actual error showed in LogCat only after restarting my development device.

Scalpel answered 30/7, 2012 at 13:51 Comment(1)
I implemented my own isResumed() - method wich caused the ClassDefNotFoundException, since it is a public final hidden method in the Android SDK. Simply rename my method worked and the Exception is was never thrown again.Resplendent
I
2

The Class Not Found Error shows that your class files are missing. Please go to Properties > Java Build Path and add your package containing your java files to the Source tab if found missing. Then build your project. This will create the missing .class files.

Interlining answered 2/8, 2012 at 5:40 Comment(0)
P
2

I recently invested some time in a similar error report that reached me through the play store error reports.

An overview of some possible causes.

Missing Library

Since my app is tested before release and the issue only occurs approx. once a week I'm pretty sure mine isn’t a library issue. You should think in this direction if a build on your system fails and installing the app using ADB results in error on launch.

Cleaning and building your project could help if this is a local issue and make sure you have the latest version of both SDK and IDE.

In these cases it’s one class of your app or a library used in your app that triggers the error. This is an important difference with the next point where the entry point (Activity with MAIN intent or your custom service/Application object) of your app is not found.

If so look into the following questions & answers I’ve selected. They all have an accepted answer.

unable to instantiate application - ClassNotFoundException Android Activity ClassNotFoundException - tried everything Android ClassNotFoundException

Main Activity or Application not found

Misconfiguration of AndroidManifest.xml

More like a beginners mistake was quoted here before in one of the answers talking about the android manifest file.

Important here is that this error would always block your app from loading. It should build though but crash on the first use.

I’ve edited that answer, this is the important part:

Do not use the android:name attribute! unless you've implemented a custom Application object.

The application:name attribute has nothing to do with the name of your app. This is the name of a specific class to load as your Application instance. That's why you would get the ClassNotFoundException if that class wouldn't exist.

For the name of the app use the android:label attribute on this same application node instead.

Suppressed: java.io.IOException: unable to open DEX file

Now it’s getting interesting! This was part of the stack trace logged in my case through the playstore. So I can create a build and execute that on my device without errors (whereas all previous errors would prevent any run).

An important comment was made to the following SO question Android Application ClassNotFoundException, Unable to instantiate application

Looks like the system has trouble opening your application dex file java.io.IOException: unable to open DEX file, this is before any of your classes are loaded, so it doesn't have anything to do with how you define your application class in the Manifest. – user2046264 Feb 27 '14 at 2:25

The important part here is the suppressed IOException. This indicates that an IO operation failed. Examples of IO operations are network and storage calls.

Another error report that goes into that direction is listed at Android to Unable to instantiate Application java.lang.ClassNotFoundException: where the role of the external storage provided by an sdcard is mentioned.

Some other similar questions indicate that this can be a user error. "/mnt/asec/..." indicates that the app is running from the sdcard. If the sdcard is removed it could cause this error. 3rd party apps or rooted devices can probably move an app to the sdcard even if its not allowed by the manifest.

If you’re desperate read all the answers to that question since there are some other paths listed also. Think of full package specification, use of singletons and more.

The point here is that both network and storage are IO.

For my specific case I checked all the devices that reported this error and found that they all have external storage support. So a user could install the app and move it to external storage, have the SD card removed and get this error (just a theory, not tested).

For more info on the installLocation options see http://developer.android.com/guide/topics/data/install-location.html

I also considered the network. On top of the stack trace detail page at the play store reports you get an overview of the occurrences of the error over time.

It appeared to me that the error occurred more around the times an update was released. This could support the theory that the user gets an update of the app, downloads this update that somehow gets corrupted. The installation goes fine but the first execution of the app results in the error (again just a theory, not tested).

Other resources

Finally 2 non SO resources that might help you:

https://groups.google.com/forum/#!topic/android-developers/JC7_qqwBRjo https://code.google.com/p/android/issues/detail?id=72121

Pridemore answered 8/5, 2015 at 13:44 Comment(0)
A
0

Are you using your code in different enviroments, let's say, by using Dropbox or something like that? Do you use any library or similar? I had a lot of troubles after sharing the workspace files and I fixed it reconfiguring dependences. Hope it helps somebody!

Adolphus answered 6/9, 2011 at 21:36 Comment(2)
How do u reconfigure dependences?Exceptional
In the IDE. You must go to Project Configuration and see if paths are the same. i.e.: Maybe you have your dropbox/copy/gdrive path in c:\users\me\blahblahblah in one PC but in another one you have it on d:\blahblahblah. You can have everything 'cloned' in every machine so the paths match, or you have to modify the project each time in each machine.Adolphus
T
0

Check if the package name in the class matches the package name in the manifest file. This worked for me

Tommy answered 27/3, 2012 at 11:21 Comment(0)
B
0

In my case it happen when i moved my launcher activity to different package without updating manifest file.

Bethea answered 2/11, 2012 at 5:5 Comment(0)
L
0

I had a ClassNotFoundException pointing to my Application class.

I found that I missed Java builder in my .project

If something is missing in your buildSpec, close Eclipse, make sure everything is in place and start Eclipse again

        <buildSpec>
            <buildCommand>
                    <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
                    <arguments>
                    </arguments>
            </buildCommand>
            <buildCommand>
                    <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
                    <arguments>
                    </arguments>
            </buildCommand>
            <buildCommand>
                    <name>org.eclipse.jdt.core.javabuilder</name>
                    <arguments>
                    </arguments>
            </buildCommand>
            <buildCommand>
                    <name>com.android.ide.eclipse.adt.ApkBuilder</name>
                    <arguments>
                    </arguments>
            </buildCommand>
    </buildSpec>
Leech answered 15/1, 2013 at 13:26 Comment(0)
H
0

I have same problem in android os version 4.1.2

add below line to your AndroidManifest.xml below android:label="@string/app_name" in application tag

android:name="android.support.multidex.MultiDexApplication"

This may help some one with same problem.

Hendecagon answered 3/11, 2016 at 18:30 Comment(0)
C
0

Setting minifyEnabled to false in my build.gradle file fixed the issue for me.

release {
    minifyEnabled false
}
Chloral answered 30/7, 2018 at 20:9 Comment(0)
S
0

For me it was just to clean project.

I cleaned project, and run again. And all errors gone.

Socinus answered 11/9, 2018 at 11:31 Comment(0)
H
0

What helped me in case of Android Studio: The problem occurred after renamning package of large project. So I did almost everything AS offers to clean and refresh the project officially, and it works. I'm not saying this is solution for everyone just in case you're using Android Studio. Done in Android Studio 3.5.1, Windows 10.

  • Alex's answer
  • Build > Clean Project
  • Build > Rebuild Project
  • File > Sync with File System
  • File > Sync project with Gradle Files
  • File > Invalidate Caches / Restart
Holleyholli answered 5/11, 2019 at 8:24 Comment(0)
H
0

Make sure that you have package

com.aaraf.demowithdoc //Your Package Name 

com.aaraf.demowithdoc [YOUR PACKAGE NAME] is there in your class where this java.lang.classnotfoundexception is occuring

Haeckel answered 10/2, 2022 at 15:6 Comment(0)
C
-2

For me, 'closing' the application from Eclipse and 'reopening' of the project, resolved the issue.

Cleon answered 7/4, 2014 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.