Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to instantiate application - ClassNotFoundException

Tags:

android

EDIT: This problem has not already been resolved in the other suggested SO question

I had a fully working app on the market for over a year, with very few crash reports. Then recently I changed my app into a library, so that it could be included within multiple different "wrapper" projects. This was so that I could easily make different version - free, paid, non-google markets, with/without in-app purchasing etc etc.

The new "library+wrapper" app appeared to work fine. I could run it multiple times, without error. But then a day later (when presumably the OS had closed some or all of the app's activities) I tried to run it and it reported

Unable to instantiate application com.mycompany.mygamelibrary.MyGameApplicationClass: java.lang.ClassNotFoundException: com.mycompany.mygamelibrary.MyGameApplicationClass

The class it failed to find is the first class that runs when the program starts up, MyGameApplicationClass - which extends Application. This class is part of the library.

I suspect something goofy in one of the two manifest files.

The manifest of the wrapper project contains the lines...

<application android:icon="@drawable/mygame_icon"
    android:screenOrientation="portrait" android:label="My Game Name"
    android:name="com.mycompany.mygamelibrary.MyGameApplicationClass">

Any ideas what could have gone wrong?

EDIT: The library was referenced "the correct way" as defined by yorkw's answer to this SO question.

EDIT: I can not repeat the crash at the moment :-( I don't know what it is the OS does when the app is not used for a day or two.

like image 706
Mick Avatar asked Jul 31 '13 08:07

Mick


5 Answers

There are two possibilities. Either you, like me, have a spelling error in your manifest file. Have a co-worker or friend read it to make sure the name is correct. Or you have not referenced the project correctly.

The official document describes how to properly link projects in its documentation.

Why it would first seem to work and later stop working is a bit of a mystery. However, I guess that the VM might still have had the necessary references ready to resolve the classes in the library just fine. A restart of the VM removed all those references and trying to resolve them was unsuccessful.

Update: Regarding the edits in the OP: As you confirm that you have correctly referenced the other project, you can check if the project is included in APK, just to be sure. You can rename and open an APK as any other archive (.rar works fine for me). Sometimes, it happened to me, the project is not correctly included in the APK. A cleaning of your workspace usually remedies the problem and so could a restart of your IDE depending on what you are using. To manually conduct a clean in Eclipse for example, use Project->Clean... or try Android Tools->Fix Project Properties by right-clicking on your project.

As you seem to also have fixed the problem by restarting your device, it could be that the libraries were linked incorrectly. A problem that I have never seen myself but as a very common quote says: "Have you tried turning it off and on again?".

like image 91
Eric Tobias Avatar answered Nov 19 '22 18:11

Eric Tobias


For Android Studio:

Build --> Clean Project

Fixed issue.

like image 17
AskQ Avatar answered Nov 19 '22 17:11

AskQ


Have you tried to make a new subclass of MyGameApplicationClass in your 'main' project and set it in the manifest as Application class?

like image 5
sashomasho Avatar answered Nov 19 '22 17:11

sashomasho


I had a slew of bugs with Android Studio 3.0 Canary 4 and the way I fixed them was by editing the AndroidManifest.xml by adding in some jibberish to the application name. Then, I clicked build. Obviously, a whole mess of new error messages appeared. I changed the name back to what it should be, built the app, and it just ran.

Sometimes, I just don't know...

EDIT: Just ran into this issue on Android Studio 3.0 Canary 4 on my laptop when switching over. I again went through the same process of changing AndroidManifest.xml file to contain a typo, building, and changing back. That didn't work.

I then noticed that instant run was still enabled. Going into settings (by clicking command + , (comma key)) and typing "instant run", I was able to disable instant run, built the app, and the error of class not found went away.

Summary of Steps to Fix [FOR ME]

  1. Invalidate cache / restart
  2. Clean the project
  3. Manually delete the build folder (need to be in project view for this one)
  4. Make an intentionally errant edit to your AndroidManifest.xml file, build the app, observe the errors, remove the errant edit and build again
  5. Disable instant run

Again, I don't mean to insinuate that this will fix everyone's error, but I have now used some combination of these steps on two different machines (MacOS Sierra) and it has been resolved for me. Hope it helps.

like image 4
stack_overflow_user Avatar answered Nov 19 '22 18:11

stack_overflow_user


In my case, application id and package were mismatched. This should be same as presented in following images...

AndroidManifest.xml

enter image description here

app/build.gradle

enter image description here

In this case you can see, applicationId and package both are same that is com.mycompany.mygamelibrary

like image 2
Khan Avatar answered Nov 19 '22 19:11

Khan