开发者

Android: Unable to instantiate application

I renamed my package and now I get this strange error:

Unable to instantiate application
app.MyApplication: java.lang.ClassNotFoundException: 
app.MyApplication in loaderdalvik.system.PathClassLoader

The MyA开发者_开发知识库pplication class is in Application/app. The Manifest says:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  package="Application">
<application 
    android:label="AGG"
    android:name="app.MyApplication"...

I tried restart, clean built. Is doesn't work on an emulator nor on a real device.

What on Earth is going on?


Let's assume, that your projects base package is really Application, as you've stated it in the manifest.

If your MyApplication class is inside this package (the package declaration of the class is package Application;), then the application element in your androidManifest.xml should look like

<application android:name=".MyApplication" [...]

If this MyApplication class is inside the Application.app package (package Application.app;), then in the manifest you should write:

<application android:name=".app.MyApplication" [...]

If you didn't extend the android.app.Application (you don't have a MyApplication extends android.app.Application class), just wanted to set a name to your application, remove it this attribute, since it says to the compiler that there is an Application extension that should be instantiated instead of the default android.app.Application.

And finally, if the first assumption is wrong, and you've changed for any reason the package declaration in your androidManifest's manifest element, undo it or update your classes to be in that package.


For me, the issue was with instant-run. Disabling it solved the issue.

Will update if I find a solution for re-enabling and making it work.


In my case it throws:

Caused by: java.lang.IllegalAccessException: java.lang.Class<foo.bar.MyApp> is not accessible from java.lang.Class<android.app.AppComponentFactory>

The reason is the Application class must be declared with the modifier public, e.g. do this:

public class MyApp extends Application {
}

instead of:

class MyApp extends Application {
}


this answer helped me, basically rename your lib to libs if you're using SDK 17+ https://stackoverflow.com/a/10176048/46459


In my case, I've change JAVA compiler compliance level from 1.7 to 1.6 and the problem was solved.


make sure all references in your manifest have been updated to reflect your new package name.


Should package="Application" be package="MyApplication"?


You need to correct the android:name attribute. Same problem mentioned at ClassNotFoundException after changing app's name in Android


Simple clean the project and rebuild.


Removing these lines worked for me:

compileOptions {
   sourceCompatibility JavaVersion.VERSION_1_8
   targetCompatibility JavaVersion.VERSION_1_8
}

Update:
The reason behind this issue is that i have not installed JAVA 8 on my PC


For me the issue was due to access level of the class, it has to be public

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜