0

After updating ADT to 22.3, sometimes APKs can be signed, but throws ClassNotFoundException when testing, as exemplified below (it seems to always affect the class extending Application):

12-19 16:32:04.374: E/AndroidRuntime(25074): FATAL EXCEPTION: main
12-19 16:32:04.374: E/AndroidRuntime(25074): java.lang.RuntimeException: Unable to instantiate application com.innostreams.vieshow.ApplicationSettings: java.lang.ClassNotFoundException: com.innostreams.vieshow.ApplicationSettings
12-19 16:32:04.374: E/AndroidRuntime(25074):    at android.app.LoadedApk.makeApplication(LoadedApk.java:501)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4201)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at android.app.ActivityThread.access$1300(ActivityThread.java:140)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at android.os.Looper.loop(Looper.java:137)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at android.app.ActivityThread.main(ActivityThread.java:4898)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at java.lang.reflect.Method.invokeNative(Native Method)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at java.lang.reflect.Method.invoke(Method.java:511)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at dalvik.system.NativeStart.main(Native Method)
12-19 16:32:04.374: E/AndroidRuntime(25074): Caused by: java.lang.ClassNotFoundException: com.innostreams.vieshow.ApplicationSettings
12-19 16:32:04.374: E/AndroidRuntime(25074):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at android.app.Instrumentation.newApplication(Instrumentation.java:971)
12-19 16:32:04.374: E/AndroidRuntime(25074):    at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
12-19 16:32:04.374: E/AndroidRuntime(25074):    ... 11 more

When this is encountered, generating another signed APK would usually solve the problem and the APK can then be installed an run without issues. No code was changed between the two signed APK builds, and project need not be cleaned, all it took to create a "correct" signed APK was to export a signed APK (righ click project->Android Tools->Export signed application package...), so this couldn't be an issue with the code nor the project setup. What I wish to know is:

  1. Why is this happening
  2. How do I fix it once and for all

Edit: Please note that the application never encounters the above issue when "unsigned" (aka signed with debug certificate) version is installed and ran.

Kai
  • 15,284
  • 6
  • 51
  • 82
  • are you using any jar file? – Amit Gupta Dec 19 '13 at 08:47
  • There might be various reasons for this. Try solutions described in this thread: http://stackoverflow.com/questions/3781151/java-lang-classnotfoundexception-on-working-app – sergej shafarenka Dec 19 '13 at 09:01
  • Is your Activity name "ApplicationSettings"? – Piyush Dec 19 '13 at 09:13
  • @AmitGupta yes, it uses a few jar libs – Kai Dec 19 '13 at 09:24
  • Try to change your Activity Name and see what happen. – Piyush Dec 19 '13 at 09:25
  • @PiyushGupta as mentioned the app signed the 2nd time runs fine (but I forgot to mention that the app _always_ works when "unsigned" aka signed with default debug certificate), so it's not an issue of specifying the wrong Application/Activity etc – Kai Dec 19 '13 at 09:27
  • @beworker thanks for the help, but all of the answers are not pertinent to my question – Kai Dec 19 '13 at 09:28
  • Have you add ant library or jar file to your project? – Piyush Dec 19 '13 at 09:28
  • @PiyushGupta yes the project does have a few jars, but please note that the app runs fine in debug, and simply recreate another signed APK resolves the issue, so it's got nothing to do with unexported jars, etc. – Kai Dec 19 '13 at 09:31
  • @Kai OK. I had same issue and the only reliable solution I found was to use Android Ant build. It produces valid apk all the time. – sergej shafarenka Dec 19 '13 at 10:01
  • @beworker Thanks for the suggestion, I'll try it. Since it's an intermittent issue, probably have to wait a while before ascertain the fix to be working or not. – Kai Dec 19 '13 at 13:19

1 Answers1

0

If you are using library projects and jar files then try this :

Remove all the jar files and library projects from your project and then add them and build the project everytime you want to create a signed apk.

I had a similar issue and it worked for me .

Chirag_RB
  • 123
  • 2
  • 9
  • Doing that would take roughly the same amount of time as regenerate signed APK and test, I wish to know some fix that would permanently resolve the issue (if such a fix exists) – Kai Dec 19 '13 at 09:23