85

I've just refactored an app into a framework library and an application, but now when I try and start the app in the emulator I get the following error stack trace:

06-02 18:22:35.529: E/AndroidRuntime(586): FATAL EXCEPTION: main
06-02 18:22:35.529: E/AndroidRuntime(586): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.matthewrathbone.eastersays/com.matthewrathbone.eastersays.EasterSimonSaysActivity}: java.lang.ClassNotFoundException: com.matthewrathbone.eastersays.EasterSimonSaysActivity in loader dalvik.system.PathClassLoader[/data/app/com.matthewrathbone.eastersays-1.apk]
06-02 18:22:35.529: E/AndroidRuntime(586):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
06-02 18:22:35.529: E/AndroidRuntime(586):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-02 18:22:35.529: E/AndroidRuntime(586):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-02 18:22:35.529: E/AndroidRuntime(586):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-02 18:22:35.529: E/AndroidRuntime(586):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 18:22:35.529: E/AndroidRuntime(586):  at android.os.Looper.loop(Looper.java:123)
06-02 18:22:35.529: E/AndroidRuntime(586):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-02 18:22:35.529: E/AndroidRuntime(586):  at java.lang.reflect.Method.invokeNative(Native Method)
06-02 18:22:35.529: E/AndroidRuntime(586):  at java.lang.reflect.Method.invoke(Method.java:521)
06-02 18:22:35.529: E/AndroidRuntime(586):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-02 18:22:35.529: E/AndroidRuntime(586):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-02 18:22:35.529: E/AndroidRuntime(586):  at dalvik.system.NativeStart.main(Native Method)
06-02 18:22:35.529: E/AndroidRuntime(586): Caused by: java.lang.ClassNotFoundException: com.matthewrathbone.eastersays.EasterSimonSaysActivity in loader dalvik.system.PathClassLoader[/data/app/com.matthewrathbone.eastersays-1.apk]
06-02 18:22:35.529: E/AndroidRuntime(586):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
06-02 18:22:35.529: E/AndroidRuntime(586):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
06-02 18:22:35.529: E/AndroidRuntime(586):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
06-02 18:22:35.529: E/AndroidRuntime(586):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-02 18:22:35.529: E/AndroidRuntime(586):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
06-02 18:22:35.529: E/AndroidRuntime(586):  ... 11 more

Usually this means that the manifest file is wrong in some way, but I've double checked everything I can think of.

Here is my activity class:

package com.matthewrathbone.eastersays;

import android.os.Bundle;

import com.rathboma.simonsays.Assets.Season;
import com.rathboma.simonsays.SeasonPicker;
import com.rathboma.simonsays.SimonSaysActivity;

    public class EasterSimonSaysActivity extends SimonSaysActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      }

      @Override
      protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
      }

      @Override
      public SeasonPicker getSeasonPicker() {
       return new SeasonPicker(){
        @Override
        public Season getSeason() {
          // TODO Auto-generated method stub
          return Season.EASTER;
        }
       };
      }
    }

As you can see, it's listed correctly in the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.matthewrathbone.eastersays"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".EasterSimonSaysActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I have no idea how to fix this, and would appreciate any help. I've scanned many similar questions on SO without seeing this particular behavior.

More info:

  • I've checked inside the generated APK and the class has an entry in the classes.dex file
  • I've tried cleaning/building the project in eclipse
  • I've tried using a totally new device image that doesn't have a copy of the APK on it already
  • I've changed the library project into a regular java, then changed back into an android project, no difference
  • Adding the abstract SimonSaysActivity to the manifest makes no difference.
  • I've tried making every dependency an android library project, and syncing the android version that they require, it did not help

Found the solution (see below). To everyone that posted an answer / comment: You all rock, thanks for helping me work through the problems!

Looks like this is introduced by an SDK tools upgrade. Thanks to @Nick below in the comments for this link: http://iqadd.com/item/noclassdeffounderror-adt-fix

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Matthew Rathbone
  • 8,144
  • 7
  • 49
  • 79
  • 1
    Is your Activity in the Library or the application? – Jeshurun Jun 02 '12 at 22:35
  • It's in the application. It extends a base activity in the library project. – Matthew Rathbone Jun 02 '12 at 22:36
  • Do you build with ant or with ADT? it seems that dex files do not contain library classes. – Tomasz Gawel Jun 02 '12 at 23:05
  • The error implies that your Activity location is com.matthewrathbone.eastersays.com.matthewrathbone.eastersays.EasterSimonSaysActivity - clearly not right. Try adding a package level to the project package that doesn't exist in the library e.g. ...eastersays.myapp.EasterSimonSaysActivity and specify com.matthewrathbone.eastersays.myapp as package in the manifest. But don't add the 'myapp' level to the library to keep the project path in the manifest distinct from the library framework path. – Gunnar Karlsson Jun 02 '12 at 23:11
  • what do you mean "you refactored your app into a framework library and an application"? – Alex Lockwood Jun 09 '12 at 18:29
  • Not to be an ass but this is asked roughly once a week, did you try the searchfunction? http://stackoverflow.com/questions/3642928/adding-a-library-jar-to-an-eclipse-android-project – richardwiden Jun 10 '12 at 00:16
  • Yep. I tried everything from every answer I found. That link you gave me does not have the solution to my problem. The answer accepted below is the correct solution to my specific issue. – Matthew Rathbone Jun 10 '12 at 00:22
  • 1
    http://iqadd.com/item/noclassdeffounderror-adt-fix : Link broken :/ – Nishant Srivastava Sep 21 '15 at 18:57

15 Answers15

148

I spent some time play with my own project, and I am able to replicate your problem and get exactly the same exception stack trace when trying to run my main project, so I think this could be the cause:

Just like what I thought, it is all about how you reference your Android library project in the Android main project, a simple Eclipse configuration settings.

The Wrong Way:
Right click main project, choose Properties -> Java Build Path -> Projects -> Add..., this add the Android library Project as a dependency project in Android main project's build path, this does not work. If all required Android-related resources are defined in main project, you will not get any error at compile time, but when run the application, you get the exception described in the question.

The Correct Way:
Right click main project, choose Properties -> Android, in the Library section, add your Android library project here. Check out official dev guide Referencing a library project. This should fix all your problem. Also note that you have to use relative path reference the actual Android library project, as stated in the Library Project - Development considerations.

starball
  • 20,030
  • 7
  • 43
  • 238
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • 1
    You my friend are a genius. What's interesting about this is that I was always including one library 'the wrong way', and it worked. So when I refactored into two libraries I thought it was something I did differently. You've just saved me hours and hours of work. – Matthew Rathbone Jun 10 '12 at 00:10
  • funny thing is, it was working before, I guess upgrading my sdk tools changed the behavior. It was very non-obvious. – Matthew Rathbone Jun 10 '12 at 00:23
  • @MatthewRathbone, AFAIK sdk upgrading should not change the behavior. You mentioned you have tried changing the library project into a regular java project. I would assume your problem is introduced by this action, as this is usually the case when we need add one project as a dependency into another one's build path. – yorkw Jun 10 '12 at 05:09
  • 3
    Yes, updating the SDK Tools to >ADT 17 introduces this error due to a change in the way the current ADT-version handles libraries. Check out: http://iqadd.com/item/noclassdeffounderror-adt-fix – Nick Jun 12 '12 at 12:57
  • 2
    Updating to ADT 22 gave a similar issue: http://stackoverflow.com/questions/16596969/libraries-do-not-get-added-to-apk-anymore-after-upgrade-to-adt-22 – Niels May 24 '13 at 07:43
  • I did this correct way, and ADT says "conversion to dalvik format failed with error 1" while exporting signed APK file...Everything is fine before...How come is it happen ? – RRTW Oct 31 '13 at 13:44
  • Any idea how I might fix a problem like this one on Netbeans, except with the native library? http://stackoverflow.com/questions/21732730/netbeans-activity-classnotfoundexception-nativelibrarydirectories – Rudi Kershaw Feb 12 '14 at 16:00
  • 8
    Would someone please let me know where can I find the `Properties`? I can't seem to find it anywhere. – jlively Apr 06 '17 at 08:56
8

i've tested the code that you've given , and it works fine. try changing the "extends SimonSaysActivity " to simply "extends Activity " and see for yourself that it works .

the reason that it doesn't work is either SimonSaysActivity doesn't extend Activity (which i don't think you've made this mistake) , or the order of the build path is wrong .

to go to the order of the build path , go to :

project->properties->Java build path->order and export .

my basic order is : project src , project gen , android 4.0.3 , android dependencies .

this problem usually occurs when you use libraries .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • So you're right, if I extend Activity directly it works. My build order matches yours, so I can't find a way to fix it that way. Any other ideas? – Matthew Rathbone Jun 03 '12 at 14:04
  • that's really odd . try to update the sdk&adt. also , please tell me if there are multiple library projects (or jar files, or both) that your app uses . if so , try to play a little with the order of the build path (i would try to put the libraries before) . in addition , i have another question : did you create a class that is called "Activity" and maybe you've extended it instead of the one of android? – android developer Jun 03 '12 at 14:08
  • There are 2 library projects, the second (engine) is depended on by both simonSays and the android project. I've tried every combination of build order, exporting the engine project from SimonSays. Answer to your question: I don't have a class called activity anywhere. – Matthew Rathbone Jun 03 '12 at 15:07
  • the engine is the core . other projects should use it and not the opposite . only the project that is an android project (meaning not a library project) should have the manifest that includes the decleration of all of the activities ,services, etc... the manifest on android library projects (currently) doesn't have much purposes (see this for more information : http://stackoverflow.com/a/10445630/878126 ) – android developer Jun 03 '12 at 15:18
  • None of the library projects have a manifest file. Only the android project does. – Matthew Rathbone Jun 03 '12 at 15:28
  • 1
    they must have . it's just that the manifest file can be very short and not include almost anything . also check the project.properties file (in each of the projects) that its target sdk is 15 as this is the latest version currently . if you wish, you can PM me and i will fix the projects . – android developer Jun 03 '12 at 16:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12084/discussion-between-matthew-rathbone-and-android-developer) – Matthew Rathbone Jun 04 '12 at 01:49
  • I tried making every project an android library project, all with the exact same android requirements. It did not work. – Matthew Rathbone Jun 09 '12 at 17:51
  • please send me your entire workspace (or the relevant projects) via PM or put a link on the first thread . i will see what i can do to fix it. – android developer Jun 09 '12 at 18:26
  • Yo this really helped me I had a project that always worked and than stoped working when I imported it and this was the only solution to fix it @androiddeveloper – B. TIger Jul 04 '13 at 20:23
  • @B.TIger i don't understand. do you need any help? – android developer Jul 04 '13 at 22:34
  • @androiddeveloper Thanks I am all good but this solution was gold to me – B. TIger Jul 08 '13 at 19:51
2

I've just refactored an app into a framework library and an application.

I'm not entirely sure what you are trying to say here, but the fact that you used the word "refactored" here leads me to believe that you are misunderstanding the concept of a library project.


What library projects are:

A library project is a development project that contains shared source code and resources. Other Android projects can reference the library project and include its compiled sources in their .apk files at compile time.

What library projects are not:

A library project differs from a standard Android project in that you cannot compile it directly to a single .apk file and run it on an Android device. You can't use an Android projects as a library project, and then have another Android project extend the library project. It doesn't work that way.


That said, I would investigate the structure of your library project and insure that you've set it up correctly. It's OK to use your library to store shared code/resources, but if your library is attempting to behave as if it were a separate .apk within the library project itself, then you've probably done something wrong. I believe a ClassNotFoundException would be thrown if this were the case. To fix the problem, I'd just build the library project from scratch, rather than attempting to convert the Android project to a library project. That'll prevent you from running into tiny, annoying bugs.

Feel free to post more code if you are still having trouble. You should also elaborate a bit more on the structure (and purpose) of your library project... why you decided to use one, how you created it, etc.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
2

For Eclipse Kepler(Mac): Control-click project -> Android Tools -> add support library

PSchuette
  • 4,463
  • 3
  • 19
  • 21
2

I had this problem with a project I was working on, though my case was different. If anyone out there still can't find the solution to their problem, try checking this:

In Eclipse:

Project properties -> Java Build Path -> Source tab

Inside you'll find a list with all the source folders to be compiled and put into the generated apk each time to build your project (and /gen). Tipically:

>MyProject/gen
>MyProject/src

What you'll want to be sure of is that inside each of your source folders (exclude /gen, so only /src in this case) the item Output folder is poiting to the default output folder, which is MyProject/bin/classes. Do this by selecting Output folder and clicking Remove from the right set of buttons (this will make it point to the default folder).

If you can't see the subitem Output folder, check "Allow output folders for source folders" right below the list

Gonzalo
  • 3,674
  • 2
  • 26
  • 28
1

If you followed all the hints (checked the private Libraries etc.) and still have the bug. Check your manifest for this Line of code in the application-tag

    android:hasCode="false" 

Remove it and be happy. (I was finally) :)

Corona
  • 685
  • 1
  • 10
  • 15
  • Thank you so much!! It was driving me crazy. This worked for me, I had a dummy MainActivity which was just loading an so. Took 2 days to find your comment! – AdrianTut Oct 04 '20 at 13:28
1

classNotFoundException in Java is a subclass of java.lang.Exception and Comes when Java Virtual Machine tries to load a particular class and doesn't found the requested class in classpath.

Read more: http://javarevisited.blogspot.com/2011/08/classnotfoundexception-in-java-example.html#ixzz3yX3WBeFA

Try going to Project -> Properties -> Java Build Path -> Order & Export And Confirm Android Private Libraries are checked for your project and for all other library projects you are using in your Application.

How to deal with the ClassNotFoundException

  1. Verify that the name of the requested class is correct and that the appropriate .jar file exists in your classpath. If not, you must explicitly add it to your application’s classpath.
  2. In case the specified .jar file exists in your classpath then, your application’s classpath is getting overriden and you must find the exact classpath used by your application.
  3. In case the exception is caused by a third party class, you must identify the class that throws the exception and then, add the missing .jar files in your classpath.
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

While this won't be applicable for everyone, I recently had this problem because my project was failing to link with a shared library present on the device. I had forgotten to specify in my manifest that my application <uses-library>.

lase
  • 2,508
  • 2
  • 24
  • 35
1

remove classes.dex from bin and rebuild project

Afzaal Iftikhar
  • 233
  • 2
  • 5
0

What interfaces or parent classes does SimonSaysActivity use/extend and are they available on the version of Android you're using? I've seen similar problems where my activity implemented an API interface that wasn't available on the version of Android I was using.

In my case my activity implemented the interface PopupMenu.OnDismissListener which is only available in API 14 but when I ran the app on my 2.3 device I received a java.lang.NoClassDefFoundError exception for the activity.

Charles Harley
  • 7,184
  • 3
  • 32
  • 38
0

I had the same problem and wasted most of a day trying all sorts of things. The app ran find when deployed from one pc but not on this one pc. In the end it ended up being a missing java nature! Strange how it showed me java build paths, order/export and all that but yet didn't have a java nature. :( Hope this saves someone else a lot of time

Luke
  • 331
  • 2
  • 5
  • 11
0

solution provided by @yorkw is absolutely correct. I had an additional issue, in one of the layout files, I had mentioned the absolute name incorrectly. It should have been

<android.support.v4.view.ViewPager xmlns:android="...

rather I had put it

<android.support.v4.app.view.ViewPager xmlns:android="...

after resolving these, it worked good.

Himadri Pant
  • 2,171
  • 21
  • 22
0

Believe it or not, the fix for me was to remove the old version (2.0) of google analytics library from my project, after trying everything for at least 4 hours. Of course, an update of this library could be the solution also. So it could be a dependency jar versioning problem.

Criss
  • 11
  • 1
  • 2
0

I was getting these type of error and i have tried everything but didn't get the solution. then i figured out if you are using library as aar file then you have to add all dependencies of your aar library to your app gradle.build file. if someone didn't figure out solution then try this!

Junaid Bashir
  • 152
  • 3
  • 15
-2

I was getting the same issue. But then i checked the current structure of the application.I found that older application structure(i don't know the time it is changed by android framework developers) has a folder name "lib" which is now "libs". I migrate my all jar files into "libs" and the error is gone. I hope this may help to someone :)

kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47