3

I had this package format:

com
  example
    name
      oldAppName

Now, I refactored and renamed by Shift+F6 to this:

com
  myCompanyName
    name
      newAppName

However,now when I run my app, I get this crash:

java.lang.ClassNotFoundException: Didn't find class "com.example.name.oldAppName.Application" on path: DexPathList[[zip file "/data/app/com.myCompanyName.name.newAppName1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

I have my Application.java under newAppName, and it is defined in the Manifest correctly What I tried and did were:

  • Cleaning and Rebuilding
  • Changing applicationId in build.gradle
  • The package name in manifest is correct and so are all the names of the App and Activities.

In fact, here's my application manifest tag:

<application
  android:name=".Application"
  ...
  >

Even changing .Application to the full package name (com.blah.blah.Application) DIDN'T WORK.

Am I missing something? Is there a missing step?

Here's the full log:

    java.lang.RuntimeException: Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication: java.lang.IllegalStateException: java.lang.ClassNotFoundException: com.example.name.oldAppName.Application
      at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4345)
      at android.app.ActivityThread.access$1500(ActivityThread.java:135)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:136)
      at android.app.ActivityThread.main(ActivityThread.java:5045)
      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:779)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
      at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.IllegalStateException: java.lang.ClassNotFoundException: com.example.name.oldAppName.Application
      at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:220)
      at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239)
      at android.app.Application.attach(Application.java:181)
      at android.app.Instrumentation.newApplication(Instrumentation.java:1008)
      at android.app.Instrumentation.newApplication(Instrumentation.java:992)
      at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4345) 
      at android.app.ActivityThread.access$1500(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5045) 
      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:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method) 
   Caused by: java.lang.ClassNotFoundException: com.example.name.oldAppName.Application
      at java.lang.Class.classForName(Native Method)
      at java.lang.Class.forName(Class.java:251)
      at java.lang.Class.forName(Class.java:216)
      at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:209)
      at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239) 
      at android.app.Application.attach(Application.java:181) 
      at android.app.Instrumentation.newApplication(Instrumentation.java:1008) 
      at android.app.Instrumentation.newApplication(Instrumentation.java:992) 
      at android.app.LoadedApk.makeApplication(LoadedApk.java:511) 
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4345) 
      at android.app.ActivityThread.access$1500(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5045) 
      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:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method) 
   Caused by: java.lang.NoClassDefFoundError: com.example.name.oldAppName/Application
      at java.lang.Class.classForName(Native Method) 
      at java.lang.Class.forName(Class.java:251) 
      at java.lang.Class.forName(Class.java:216) 
      at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:209) 
      at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239) 
      at android.app.Application.attach(Application.java:181) 
      at android.app.Instrumentation.newApplication(Instrumentation.java:1008) 
      at android.app.Instrumentation.newApplication(Instrumentation.java:992) 
      at android.app.LoadedApk.makeApplication(LoadedApk.java:511) 
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4345) 
      at android.app.ActivityThread.access$1500(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5045) 
      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:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method) 
   Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.name.oldAppName.Application" on path: DexPathList[[zip file "/data/app/com.newCompanyName.name.newAppName-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.myCompanyName.name.newAppName-1, /system/lib]]
      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
      at java.lang.Class.classForName(Native Method) 
      at java.lang.Class.forName(Class.java:251) 
      at java.lang.Class.forName(Class.java:216) 
      at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:209) 
      at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239) 
      at android.app.Application.attach(Application.java:181) 
      at android.app.Instrumentation.newApplication(Instrumentation.java:1008) 
      at android.app.Instrumentation.newApplication(Instrumentation.java:992) 
      at android.app.LoadedApk.makeApplication(LoadedApk.java:511) 
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4345) 
      at android.app.ActivityThread.access$1500(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5045) 
      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:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method) 
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
  • Have you checked http://stackoverflow.com/questions/10866431/android-activity-classnotfoundexception-tried-everything?rq=1 or http://stackoverflow.com/questions/22399572/java-lang-classnotfoundexception-didnt-find-class-on-path-dexpathlist – Stefan Nov 10 '16 at 19:37
  • @Stefan those are both using Eclipse and are outdated. – Ali Bdeir Nov 10 '16 at 19:38
  • Those still have some interesting answers below. Have you checked your imported libraries versions? Clean app cache and uninstall it completely then try to build again? – Stefan Nov 10 '16 at 19:48
  • @Stefan nothing you mentioned worked – Ali Bdeir Nov 10 '16 at 19:49
  • In application tag android:name is not mandatory so did u tried to remove the same and rebuild your application. – abbas piplodawala Nov 11 '16 at 08:54
  • @abbaspiplodawala but I need the Application class as I am setting some things in it. – Ali Bdeir Nov 11 '16 at 09:17

5 Answers5

6

Sometimes, disabling Instant Run will solve the issue.

Or, if disabling Instant Run didn't work, the problem could be with Android Studio not recognizing the changes yet. BACKUP YOUR PROJECT FIRST and note that you'll have to re-configure your VCS after this (but no commits or branches will be lost, just the registration of your VCS.)

Delete the .idea and [yourProjectName].iml of your app.


For future users:


The right way to rename packages in Android Studio

  1. In your Project pane, choose the gear icon
  2. Uncheck "Compact Empty Middle Packages" if checked.

Gear icon example

  1. Now go on with refactor->rename for every broken down package name (essentially com, example, name, oldAppName)
  2. Rename might open a waring dialog in which you might have to choose Rename Package

Warning dialog example

  1. Once renaming package is done, Replace applicationId in gradle with the correct one

  2. Sync gradle

  3. clean and rebuild

You could find a better version of how to properly refactor your package here

P.S: you must revert to the original package name at the point where you did not get "ClassNotFound" exception and try the above solution.

Community
  • 1
  • 1
Vijai
  • 1,067
  • 2
  • 11
  • 25
  • Probably a wild suggestion. Could you disable instant run, remove app and re-run the project? Instant run has failed in few instances for me – Vijai Nov 11 '16 at 09:09
  • did you run a find usage check on your old package name? and double check your package name for typos in both manifest and build.gradle – Vijai Nov 11 '16 at 09:23
  • Also check your Application class hierarchy. Probably its in `com.mycompanyname.name` rather than in `com.mycompanyname.name.newAppName` or somewhere else. Also, assuming the class name in the actual question is exactly same as you have in code, you cannot have `Application` as class name – Vijai Nov 11 '16 at 09:23
  • I did a Find-Replace after the refactoring and replaced the old package with the new one. Still nothing – Ali Bdeir Nov 11 '16 at 10:41
  • Wait. I renamed my `Application` class to `MyActualAppName`. Now it crashes, **but with the SAME error**. Nothing changes, it still says `java.lang.ClassNotFoundException: com.example.name.oldAppName.Application` instead of `java.lang.ClassNotFoundException: com.example.name.oldAppName.MyActualAppName`. This is weird. – Ali Bdeir Nov 11 '16 at 10:55
  • And as a plus, when I remove `android:name` from my `` tag, the error still occurs. – Ali Bdeir Nov 11 '16 at 10:58
  • That makes me believe that android studio didnt pick up package changes correctly. Can you close the project, delete `.idea`folder and the .iml file in project root and reimport project in android studio? Warning: Make a backup before and any vcs config will be lost and has to be configured again – Vijai Nov 11 '16 at 11:12
  • That makes sense. I'll check it out – Ali Bdeir Nov 11 '16 at 11:17
  • Ok, did that. Now, thanks to Android's marvelous logic, it's forcing me to update everything. :)\ – Ali Bdeir Nov 11 '16 at 11:24
  • I can award my bounty in 6 hours. It will be awarded to you. :) – Ali Bdeir Nov 11 '16 at 12:43
  • That's fine @AbAppletic :) – Vijai Nov 12 '16 at 03:24
  • The Android Gradle plugin is aggressively caching an artifact used by instant run to retrieve your application id and application class. You can workaround the problem by doing `rm -rf ~/.android/build-cache` (there is a way to limit deletion by doing `find ~/.android/build-cache -type f | xargs grep -l AppInfo` and deleting only matching cache folders) and then doing a clean rebuild then launch of your app. This will fix the issue until the next application Id/package rename. See details here https://code.google.com/p/android/issues/detail?id=229128#c8 – Matt Jan 06 '17 at 13:58
3

For those coming here, in at least Android Gradle plugin version 2.2.3, there is a bug aggressively caching an artifact used by instant run to retrieve your application id and application class. You can workaround the problem by doing rm -rf ~/.android/build-cache and then doing a clean rebuild then launch your app.

There is a way to limit deletion by doing find ~/.android/build-cache -type f | xargs grep -l AppInfo and deleting only matching cache folders:

$ find ~/.android/build-cache -type f | xargs grep -l AppInfo
/Users/joe/.android/build-cache/dde3e201178401491cae141d479a0b4113bf3a0e/output/classes.dex
/Users/joe/.android/build-cache/f7d636c8af71d68edfb468297d20157f3eb6659c/output/classes.dex

$ rm -rf /Users/joe/.android/build-cache/dde3e201178401491cae141d479a0b4113bf3a0e
$ rm -rf /Users/joe/.android/build-cache/f7d636c8af71d68edfb468297d20157f3eb6659c

This will fix the issue until the next application id/package rename.

See details here code.google.com/p/android/issues/detail?id=229128#c8

Vijai
  • 1,067
  • 2
  • 11
  • 25
Matt
  • 10,633
  • 3
  • 46
  • 49
0

I encountered similar problem after refactoring one of my java class, and I resolved it by cleaning the project structure.

  1. Go to Build
  2. Click on the clean project option

After this, everything got resolved.

Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51
Krishna
  • 39
  • 9
0

Before doing a clean build, do check the following below to ensure the following all have been updated with the new class name.

  1. class name on Directory: MainActivity
  2. class: public class MainActivity extends ....
  3. Manifest:
  4. XML: tools:context=".MainActivity"

After which, you do a clean build/rebuild.

lonelearner
  • 1,637
  • 4
  • 14
  • 22
0

Refactoring the name of a Qt based Android app

For those coming here after seeing the exact same error messages when trying to rename a Qt based Android application: to rename it simply adapt the following XML attributes in your AndroidManifest.xml:

<!-- application package name as seen in Google Play URLs, 
     in the output of `adb shell pm list packages` etc. -->
<manifest package="com.example.appname" …>

<!-- application name as shown below the app icon -->
<application … android:label="App Name" …>

<!-- application name "sometimes" shown below the app icon -->
<activity … android:label="appname" …>

Do not touch the following attributes, because that would lead to the symptoms described here:

<application … android:name="org.qtproject.qt5.android.bindings.QtApplication" …>

<activity … android:name="org.qtproject.qt5.android.bindings.QtActivity" …>

The reason is that in a Qt application for Android, you do not define the Android activity to launch in your main.cpp or something. Instead, the program anroiddeployqt generates bootstrap Java code that eventually calls your C++ code from main.cpp. That bootstrap code needs the application and activity names as seen above. But you can change their labels (display names) – see above.

tanius
  • 14,003
  • 3
  • 51
  • 63