Android app ClassNotFoundException for main activity
Asked Answered
H

13

21

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?

Hagioscope answered 27/5, 2011 at 2:30 Comment(5)
Have you defined your Activity in Menifest file?Energumen
The SD card is removed or mounted to a PC #4821054 #5484447Cawley
The SD card is removed or mounted to a PC #4821054 #5484447Cawley
Did you find the correct answer eventually?Touchandgo
In my case the problem was caused by updating SDK to version 22. I was able to solve this problem using this question: #16586464Triode
W
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.

Wyly answered 27/1, 2013 at 22:1 Comment(2)
Sadly, I am getting this exception even with that line on top of the manifest. It's kind of random, though ...Castello
This answer helped me out. I also had to run gradle clean, since the old debug APK was still not correct.Smelter
S
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.

Stepchild answered 27/5, 2011 at 4:45 Comment(5)
<activity android:name=".MyMainActivity" ...>, it is correct way to defineEnergumen
good point. Although in this example if the project is com.company.project, I think it would have to be ".main.MyMainActivity"?Stepchild
yes, before Activity, it takes package name, Package name is already defined in menifestEnergumen
I think fredw is trying to solve my problem but I cannot verify it so soon, as the problem only occurs at customer side.Hagioscope
This fixed for me. Thanks.Tsarevitch
C
6

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

Choose answered 27/5, 2011 at 4:29 Comment(2)
the problem only occurs at customer side. :/Hagioscope
Not sure why this was downvoted. Restarting Eclipse fixed it for me.Hexateuch
O
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.

Olsson answered 6/12, 2013 at 10:11 Comment(0)
F
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.

Funicular answered 18/7, 2014 at 8:16 Comment(1)
+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!!!Webster
B
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.

Blitzkrieg answered 11/10, 2013 at 5:1 Comment(0)
H
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.

Holna answered 24/6, 2014 at 17:22 Comment(0)
H
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.

Hypothermal answered 16/7, 2014 at 0:39 Comment(0)
H
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.

Hammad answered 22/8, 2015 at 14:59 Comment(0)
C
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.

Chamfer answered 5/1, 2014 at 4:6 Comment(0)
L
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.

Lachrymatory answered 13/5, 2018 at 8:53 Comment(0)
C
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

Celaeno answered 20/12, 2018 at 7:36 Comment(0)
B
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
Boschbok answered 7/2, 2023 at 6:19 Comment(1)
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.Sherborne

© 2022 - 2024 — McMap. All rights reserved.