23

Most of the devices can run my app but I got this error report :

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.company.app/com.company.app.MainActivity}: java.lang.ClassNotFoundException: com.company.app.MainActivity in loader dalvik.system.PathClassLoader[/mnt/asec/com.company.app-1/pkg.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2755)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
at android.app.ActivityThread.access$2300(ActivityThread.java:136)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5068)
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:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.company.app.MainActivity in loader dalvik.system.PathClassLoader[/mnt/asec/com.company.app-1/pkg.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.newActivity(Instrumentation.java:1034)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
... 11 more

I found there is a related answer: ClassNotFoundException Android

but the question is, my exception occurs before any line of code i write, so how can I ensure the class loader is loaded?

5

13 Answers 13

21

I had exactly the same problem. Suddenly my app stopped working. I didint make any change to manifest, but with some accident there was missing first row:

<?xml version="1.0" encoding="utf-8"?>

This caused ClassNotFoundException for launcher activity. I was struggling with that for few hours and I overlooked this every time I was checking manifest for possible mistake.

Strange thing happened after I have deleted this row again. Suprise, suprise app was still working. (and I didnt forget to rebuild app)

Hope this help someone to save few hours.

Sign up to request clarification or add additional context in comments.

2 Comments

Sadly, I am getting this exception even with that line on top of the manifest. It's kind of random, though ...
This answer helped me out. I also had to run gradle clean, since the old debug APK was still not correct.
11

I get this error when my class isn't correctly defined in the manifest, typically because I haven't put in the correct name space as in:

    <activity android:name="com.company.project.main.MyMainActivity" ...>

as opposed to

    <activity android:name="MyMainActivity" ...>

Not sure why it would work for you on some devices and not others though.

5 Comments

<activity android:name=".MyMainActivity" ...>, it is correct way to define
good point. Although in this example if the project is com.company.project, I think it would have to be ".main.MyMainActivity"?
yes, before Activity, it takes package name, Package name is already defined in menifest
I think fredw is trying to solve my problem but I cannot verify it so soon, as the problem only occurs at customer side.
This fixed for me. Thanks.
6

Please close your eclipse and open. Clean your project and do a refresh. Then run and see.

2 Comments

the problem only occurs at customer side. :/
Not sure why this was downvoted. Restarting Eclipse fixed it for me.
3

I had the same issue and the problem was a third party library that is using Android Support Library.

myproject/libs/android-support-v4.jar 
3rdpartylib/libs/android-support-v4.jar

When I updated the library with newer version, the support library was also updated - but not in my project. After that I could not launch the application with this error. After making both libraries to be the same (copy), it was ok.

Comments

2

In eclipse, check Properties>Java Build Path>Source and see if the output folder is always the default (/bin/classes).

For me, this was an issue because of the maven plugin: whenever I right click>Maven>Update Project...>Ok, it changes the output folder of the src folder to target/classes, causing the ClassNotFoundException.

1 Comment

+1. I changed my project to Maven Project so it changed my output folder to /target/classes. Hence the class not found error - because its in the wrong path!!!
1

I just ran into this problem. Turns out it was because I had moved my MainActivity into a new package.

What I did was move it from

com.example.MainActivity

into

Activity

so in my manifest it looked like this:

 android:name="Activity.MainActivity"`

Solved it by moving it back into the original package. It can also be solved by renaming your packages correctly:

Eg: com.example.Activity.MainActivity

Hope this helps someone.

Comments

1

I had this problem for the last couple days. I could run my app on every test environment, I could export a release apk, and install and run it on every test environment, but when I uploaded the apk to the play store and installed from the play store the app would fource close throwing ClassNotFoundException. It turned out that in my project I was sending multipart form data to a web api, and to do this requires some external libraries. At first I just included all the mim4j, httpclient, and httpcore .jars (turns out to be a lot of jars).

After deleting almost all of them one by one and seeing if my project would still build I was left with only one jar THAT WAS ACTUALLY REQUIRED. The take away is delete any jars that are not required to build. Then I tested the app and turns out I needed to add one of the jars back because it's resources were referenced by the one that was required to build the project. Make sure to test every function of your app after deleting jars.

Then I went through each library project I was including and went to right-click on project -> properties -> java build path -> order and export and checked all the boxes FOR EVERY LIBRARY and MY OWN PACKAGE.

Then I did a project -> clean on every project that is included and the main project.

Then I exported the apk and put the package up for beta testing and it finally worked on users devices.

Comments

1

I ran into this problem and it was fixed using this.

I had not checked one external library under "Order and export" and i was getting the error.

Comments

1

None of the answers above worked for me, contributing my solution if it's going to spare some hours to anyone in the future.

What apparently worked for me was removing an extra support lib v4 jar in my project.

After I updated the support lib via android tools, I had to fix jar mismatches in many libraries I use and point them all to the same jar in the android-sdks folder.

I accidently added it to my project as well, and for some reason it made my MainActivity class not found in runtime, because the jar was also in the android-support-v7-appcompat library I was using.

No errors on eclipse at all.

I removed the jar from my project, and checked the jar in the v7 appcompat library in the export tab so it would be available to my project as well, and voila, app is working.

Comments

0

Thanks to version control, I noticed that I didn't have these lines in my .classpath xml file:

<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>

These caused Class-not-found for main activity.

Comments

0

I've got the same when run Robolectric tests. Actually, test classpath includes jar with android.com.Activity, which has not been founded. A solution in my case is downgrade JAVA_HOME from 10th JDK version, which is used in Maven project.

Comments

0

I made a copy/paste of the following folders to a new project, keeping the same package name, and solved it:

  • ../src/debug
  • ../src/main
  • ../src/release

And finally I added the file dependencies build.gradle.

GL

Comments

0

If you changed your package name in flutter using some dev dependency package you have to cross check the both area

  1. In androidmanifest.xml => "<activity android:name="your new package name.MainActivity" 2.In mainActivity.java

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.