Android to Unable to instantiate Application java.lang.ClassNotFoundException: - android

I am working on application which is host on android market. Sometimes (once a month ) I got a crash report:
Unable to instantiate application java.lang.ClassNotFoundException
App downloads are between 10,000-50,000. I don't know why this exceptions raise on some devices not all ( I tested it on 3 different devices & I couldn't re-produce it at my end).
I read articles/suggestions on different android forums regarding the issue but I didn't succeed in solving it. Does anyone face similar issue & suggest me what should I do?
Note:
I am extending application class like this
public class MyApplication extends Application {
}
I register it in the manifest.xml like this
<application android:icon="#drawable/app_icon"
android:label="#string/my_app_name" android:name="MyApplication">
Stack Trace :
java.lang.RuntimeException: Unable to instantiate application com.xyz.MyApplication java.lang.ClassNotFoundException: com.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-1/pkg.apk]
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:650)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4238)
at android.app.ActivityThread.access$3000(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2076)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
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.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-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.newApplication(Instrumentation.java:942)
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:645)
I don't know why application crash on some devices not all.

Some other similar questions indicate that this can be a user error. "/mnt/asec/..." indicates that the app is running from the sdcard. If the sdcard is removed it could cause this error. 3rd party apps or rooted devices can probably move an app to the sdcard even if its not allowed by the manifest.
Similar Question

I think the problem is with getApplication() which I have used in 10 different place. So I have used singleton pattern to solve this.
public class MyApplication extends Application {
private static MyApplication me;
#Override
public void onCreate() {
super.onCreate();
me = this ;
}
public static MyApplication getInstance() {
return me;
}
}
Now I have used getApplication() like this
MyApplication application = MyApplication.getInstance();
insted of
MyApplication application = (MyApplication) getApplication();
I have uploaded the fixed version on the market & now waiting if there is anymore this kind of crash. If everything goes perfect ( if no more crash in 2 weeks) then I will close the question. In meanwhile anyone has better idea or know the solution , please share it. Thanks,

In my case, I was compiling and signing with Eclipse ADT (with File > Export > Export Android Application...) but missing some classes if I decompile my .apk.
To solve it I use "Export an unsigned APK" and sign it using jarsigner and zipalign.

android:name=".MyApplication"
also consider adding full package

Related

ClassNotFoundException when downloaded from Google Play

I've got a problem involving ClassNotFoundException. When i "Run as android application" from eclipse it works fine, but when i download it from Google Play (same source code but "Exported as signed app") it crashes with the following stacktrace:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.larphoid.aeboing/com.larphoid.aeboing.BoingBoing}: java.lang.ClassNotFoundException: com.larphoid.aeboing.BoingBoing in loader dalvik.system.PathClassLoader[/data/app/com.larphoid.aeboing-1.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
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.larphoid.aeboing.BoingBoing in loader dalvik.system.PathClassLoader[/data/app/com.larphoid.aeboing-1.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:1033)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
... 11 more
I've never had this problem before, and i'm not experienced yet in "reading" Stacktraces, so i'm pretty much puzzled at this. If anyone has some pointers or solution i'd appreciate it.
Thanks.
Gonna copy this from another question as I had the exact same problem before, and this is how I fixed it:
If you're running adt rev 22, you might not be exporting your libraries. I don't know why
Google did this exactly, but in the newest revolution you have to
check to make sure you're exporting your libraries yourself.
Right click your project, click Properties, and check Order & Export
under Java Build Path. It should look like this if you want to export
everything:
Afterwards, rebuild the application and export it again. It should be working now. To test, simply install the .apk to your phone manually before putting it on the store to make sure it works.
ClassNotFoundException indicates that something is missing on the classpath. Maybe you are missing a library in the classpath.
As the name suggests 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.
Maybe you change your package name while signing your apk.
Check your package and class name

java.lang.ClassNotFoundException on startup

We have very weird java.lang.ClassNotFoundException on app startup that happens to very small amount of customers (< 1%). We never saw it in dev env and we cannot understand the source of this issue. Here is the stack trace from Google play crash reports.
java.lang.RuntimeException: Unable to instantiate application com.mycompany.myapplication.MyApplication: java.lang.ClassNotFoundException: com.mycompany.myapplication.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.mycompany.myapplication-1/pkg.apk]
at android.app.LoadedApk.makeApplication(LoadedApk.java:490)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3784)
at android.app.ActivityThread.access$2200(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1082)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4268)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.mycompany.myapplication.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.mycompany.myapplication-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at android.app.Instrumentation.newApplication(Instrumentation.java:972)
at android.app.LoadedApk.makeApplication(LoadedApk.java:481)
... 11 more
If the issue only occurs on with signed APKs and you are using Eclipse to Export the Signed APK ...
Before you 'Export and sign an APK' in Eclipse turn off the following setting in the menu [Project > Build Automatically] (then can switch it back on later when you continue developing).
I recently deployed an update to my app, and a user reported a crash the very next day via the Google Play reporting facility. The stack dump was for LoadApk() and the error was in the loading of my Application class. Here is that dump:
java.lang.RuntimeException: Unable to instantiate application com.goalstate.WordGames.FullBoard.library.FullBoardApplication: java.lang.ClassNotFoundException: Didn't find class "com.goalstate.WordGames.FullBoard.library.FullBoardApplication" on path: DexPathList[[],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4703)
at android.app.ActivityThread.access$1600(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.goalstate.WordGames.FullBoard.library.FullBoardApplication" on path: DexPathList[[],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.Instrumentation.newApplication(Instrumentation.java:981)
at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
... 11 more
I have a library and my Application class was defined in that library. The manifest for my app (which used the library) referred to the class in that library by its full path. It did not have an application class of its own.
All of my in-house testing had not reproduced this problem, and even when I tested (using the Samsung Remote Test Lab) on the same device (actually two different devices, one running 4.3 and one running 4.4.4) as reported the crash (a Galaxy Note II, running Android 4.4), there was no problem.
In searching for information on this I found mention of the fact that different devices may have slightly different approaches to resolving class references, and that is probably why most devices had no problem with my APK, but this particular device (which, unlike my test devices, was provided with its Android flavor by Sprint) did. And similarly, it may be why just one percent of your own customers had the problem, while most did not.
I decided that the best approach would be to make it as easy as possible for even an unsophisticated device to find the classes that were referenced from my manifest file. So, I defined a new application class within the package of the app itself (rather than the library) and I had that class inherit from the application class in my library. The new class was otherwise empty.
I then replaced the full path reference to the application class in the library with a relative reference to the new class I had created in the app itself. So, instead of having:
<application android:name="com.goalstate.WordGames.FullBoard.library.FullBoardApplication"
in my manifest, I had:
<application android:name=".FullBoardWordChumsApplication"
That (according to folklore on this topic) should make it easier for a less sophisticated resolution process to succeed.
I also took the full path that I had been using in the manifest to name my already-local activity class and made it relative (by simply lopping off everything preceding the final dot).
Additional folklore found online indicates that it may help to turn off "Build Automatically" for the project in Eclipse, then exit Eclipse, re-enter Eclipse, and then, after it rebuilds, go directly to Android Tools to export the signed APK (without ever turning on Build Automatically). So, throwing salt over my left shoulder, and saying a prayer to the gods of fragmentation paranoia, I accommodated this superstition in preparing my APK for release.
Does any of this really help? Time will tell, but so far my updated release has not generated any additional crashes.
This Error occurs when in your manifest file in application tag whatever class name you put it is not match with your coding java file .So make a current both in way.

Robotium test cannot find activity class

I have been using robotium to test my android application. I found it very useful tool so far. Recently we have done a refactoring that would use only one activity in the entire application, each page will be replaced by a fragment.
However, After we start using that activity to run the unit tests, the test complains NoClassDefound error -- it couldn't find the activity class. I don't see anywhere I have change the configuration whatsoever.
Can anybody give a clue what might be wrong , where to check and so on ?
[INFO] java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(T estSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
....
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.xxx.wallet.HaloActivity
at com.xxx.wallet.HaloActivityTest.<init>(HaloActivityTest.java:12)
... 18 more
The app apk is loaded, AndroidManifest.xml should be ok too.
Make sureafter refactoring:
The AndroidManifest.xml of the test project is still accurate:
<instrumentation android:targetPackage="package.of.the.app.under.test">
The Test class is still accurate:
public class YourTest extends ActivityInstrumentationTestCase2<SplashScreenActivity> {
protected static final String TARGET_PACKAGE_ID = "package.of.the.app.under.test";
protected Solo solo;
public Test() {
super(TARGET_PACKAGE_ID, StartingActivityOfYourAppUnderTest.class);
}
//..
}
All libraries of the app under test can only (!) be found in libs/yourlibrary.jar and are referenced in Project->Properties->Java Build Path->Libraries

FragmentActivity Junit Testing

Me used Fragment of Android Compatibility Package, using the android-support-v4.jar. But I can't do the JUnit test on this.
My main FragmentActivity class is declared as follows
public class MyActivityClass extends FragmentActivity{
...............
}
Then in my test project
public class MyActivityClassTest extends ActivityInstrumentationTestCase2<MyActivityClass> {
public MyActivityClassTest() {
super("com.android.myproject", MyActivityClass.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
...................
}
public void testPreconditions() {
.................
}
public void testNotNull(){
................
}
}
But when I run as Android JUnit Test produce FailedToCreateTests[Runner:Junit3]
Failure Trace
java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.reflect.InvocationTargetException
at com.android.myproject.test.MyActivityClassTest.<init>(MyActivityClassTest.java:28)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:336)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3982)
at android.app.ActivityThread.access$2900(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.android.myproject.MyActivityClass
... 19 more
When I changed MyActivityClass to extends Activity it worked fine (MyActivityClass extends Activity)
Me used the same android-support-v4.jar in my both test and main project
ActivityInstrumentationTestCase2 is compatible with Fragments. You should only follow the steps mentioned in Android Testing: External libraries, which has been updated to cover the case of the android-support-v4.jar too.
Then, you will be able to write tests like this one:
public void testFragmentManager() {
FragmentActivity activity = getActivity();
assertNotNull(activity.getSupportFragmentManager());
}
or whatever you need to test.
I found a solution
The problem was
Caused by: java.lang.NoClassDefFoundError: com.android.myproject.MyActivityClass
It cannot find the class path even though I refer the same jar in both projects ( I also tried by using separate jar for both the project )
Now, I created my testing environment in the same project, then it worked
In my AndroidManifest.xml
<manifest...>
<!-- For doing JUnit test, Instrumentation Start (remove later) -->
<instrumentation
android:targetPackage="com.pe.android.isccasinos"
android:name="android.test.InstrumentationTestRunner" />
<!-- For doing JUnit test, Instrumentation End (remove later) -->
<application ...>
................
<!-- For doing JUnit test, add library starting (remove later) -->
<uses-library
android:name="android.test.runner" />
<!-- For doing JUnit test, add library ending (remove later) -->
</application>
<manifest>
Then I added my Testing class in my special package for testing
extends ActivityInstrumentationTestCase2<fragmentActivity>
Now everything is working fine :)
See this question for a better answer:
FragmentActivity can not be tested via ActivityInstrumentationTestCase2
You need to export the reference to the compatibility library from your app.
This is late, but I solve the same issue by only adding the android-support-v4.jar dependency to the main project, and removing it from the test project.
Then, make sure the main project is on the build path of the test project.
Do the same for any other libraries the main, and test projects might share.
My guess is that ActivityInstrumentationTestCase2 is not compatible with Fragments.
You should try something like Robolectric.
I have tried almost a day to deal with this issue and found a solution from another post
Basically your android-support-v4.jar (in my case) is used in the Application should be same with what you have used in your Test application. The best way to do is, remove the same jar from the libs directory of your test project and export the same from your Application project. That way you can get rid of this issue for sure.

ClassNotFoundException for a ContentProvider

I have a ContentProvider class and is declared in AndroidMenifest.xml like this:
<provider android:name=".MediaSearchProvider"
android:authorities="org.iii.romulus.meridian.mediasearch">
<path-permission android:path="/search_suggest_query"
android:readPermission="android.permission.GLOBAL_SEARCH" />
</provider>
It works well on most devices, but the Market tells me some users are suffering error with it. The stack trace is:
java.lang.RuntimeException: Unable to get provider org.iii.romulus.meridian.MediaSearchProvider: java.lang.ClassNotFoundException: org.iii.romulus.meridian.MediaSearchProvider in loader dalvik.system.PathClassLoader[/mnt/asec/org.iii.romulus.meridian-1/pkg.apk]
at android.app.ActivityThread.installProvider(ActivityThread.java:4509)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4281)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4237)
at android.app.ActivityThread.access$3000(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
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:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: org.iii.romulus.meridian.MediaSearchProvider in loader dalvik.system.PathClassLoader[/mnt/asec/org.iii.romulus.meridian-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.ActivityThread.installProvider(ActivityThread.java:4494)
... 12 more
I have totally no idea about what's up and I can't reproduce it on any of my phones. I also tried clean and build, but the report still comes up.
Anyone can help? Thanks!
The answers regarding proguard are incorrect. This would cause an easily reproducible error on every phone, every time, because the ContentProvider class would be completely missing. The developer clearly states that they cannot reproduce the error, meaning that the ContentProvider class is present but for some reason is not being found on one of their user's phones.
I have the same crash reported in the market for my app. The stack traces look identical, and the error is occurring at installProvider. I have about 15 test phones in my office and none of them can reproduce this problem. Any other ideas would be appreciated.
Fully qualified names in the manifest are only necessary if your java package names are not the same as your android package name. If a fully qualified name is not specified, the OS will automatically prepend the android package name to the class name specified in the manifest.
Ensure twice that you have correct qualified class name specified in AndroidManifest.xml, it must read something like this:
<provider
android:authorities="org.iii.romulus.meridian.mediasearch"
android:name="org.iii.romulus.meridian.MediaSearchProvider">
</provider>
Notice that #name is fully qualified.
This sounds similar to an issue I had that was caused by an issue with the ClassLoader, see here: Bizarre behaviour when using Apache Commons lib in Android
This bug discusses an error relating to the class loader failing sometimes. The fix for me was to add this line:
Thread.currentThread().setContextClassLoader(this.getClassLoader());
in the constructor of the class that was calling the code that was failing.
Proguard excludes all inherited content providers by default with this line (make sure it's in your cfg):
-keep public class * extends android.content.ContentProvider
If you have any additional inheritance you should exclude it as well or exclude your specific Content Provider class, for example:
-keep public class org.iii.romulus.meridian.MediaSearchProvider
This is an old thread, and the OP didn't have the same ContentProvider declaration as me, but I had the same exact error, so I want to share my findings, in case it helps anyone.
For me, what caused the problem was that the ContentProvider declaration in the AndroidManifest.xml had an exported attribute set to true:
android:exported="true"
Removing it fixed the problem for me. (I didn't really need it)
If your app has grown large enough to require Multidexing and your app is not set up correctly as a Multidex app you will get this error. To set up your app as a multidex app, follow these directions:
Setup multidex app

Categories

Resources