Search code examples
androidandroid-studiorefactoringpackageclassnotfoundexception

Refactoring package name breaks app with ClassNotFoundException due to not finding the Application class even though it is there


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) 

Solution

  • 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.