Package rename and error "Activity class does not exist"

29,002

Solution 1

The error was in IntelliJ IDEA after all. When you create a project, the Configuration checks Launch feature automatically and prints the name of default class. When you change the name of package, refactoring does not change the configuration string which still points to the old class name. That is why there was not compile-time error, but runtime error.

It would be great if they could fix this issue in this awesome IDE as these kind of errors are very hard to track down (this one took 4 months to realize where the error was).

Solution 2

It is a bug in Android Studio. To fix it:

  1. Close Studio.
  2. Remove .idea/workspace.xml
  3. Launch Studio.

Solution 3

File -> Invalidate caches/Restart may fix this type of errors

Solution 4

Did you notice this difference (For package name in manifest),

The Launching Activity is com.path1.pathOLD/com.path1.path2.SplashActivity.

Change package in manifest file .. use this modified manifest and let me know what happen..

EDIT: Modified Manifest file,

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.path1.path2"
      android:versionCode="2"
      android:versionName="1.0">
<uses-sdk android:minSdkVersion="4"/>

<!--permissions-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application android:label="@string/app_name" android:icon="@drawable/icon">
    <activity android:name=".SplashActivity"
              android:label="@string/app_name"
            >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".SomeActivity"
              android:label="@string/app_name"
            >
        <intent-filter>
           <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    <activity android:name=".OtherActivity" android:label="@string/app_name"
            />

    <!--services-->
    <service android:name=".AlarmService"/>

</application>

Solution 5

In my case I followed all the answers from here, but also I had to clean the cache. I followed this steps:

1. Go to ~/.gradle/ and delete caches folder
   rm -r caches
2. "Sync project with Gradle files" icon on Android Studio
3.  Run project
Share:
29,002

Related videos on Youtube

sandalone
Author by

sandalone

A developer since 1999, started with Java. A freelancer since 2009, and a client hiring freelancers since 2011. Have been using Android Studio and Gradle since 0.1.

Updated on May 24, 2020

Comments

  • sandalone
    sandalone about 4 years

    I have a splash activity which starts another activity like this

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
    
            final Thread splashThread = new Thread() {
                @Override
                public void run() {
                    try {
                        int wait = 0;
                        while (_isActive && (_splashTime > wait)) { 
                            sleep(100);
    
                            if (_isActive) {
                                wait += 100;
                            }
                        }
                    } catch (InterruptedException e) {
                        Log.d(TAG, e.getMessage());
    
                    } finally {
                        startActivity(new Intent("com.path1.path2.SomeActivity"));
                        finish();
                    }
                }
            };
            splashThread.start();
        }
    

    To start another activity I use string parameter for the Intent constructor. The corresponding class is paired with the splash string like this

       <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.path1.path2"
              android:versionCode="2"
              android:versionName="1.0">
        <uses-sdk android:minSdkVersion="4"/>
    
        <!--permissions-->
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    
        <application android:label="@string/app_name" android:icon="@drawable/icon">
            <activity android:name=".SplashActivity"
                      android:label="@string/app_name"
                    >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
            <activity android:name=".SomeActivity"
                      android:label="@string/app_name"
                    >
                <intent-filter>
                    <action android:name="com.path1.path2.SomeActivity"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
            </activity>
            <activity android:name=".OtherActivity" android:label="@string/app_name"
                    />
    
            <!--services-->
            <service android:name=".AlarmService"/>
    
        </application>
    </manifest>
    

    This works flawlessly UNTIL I rename the package name. I rename the package name in the Manifest by using refactoring and the IDE renames all other classes accordingly. But when I want to start newly renamed project, I face an error

    Launching application: com.path1.pathOLD/com.path1.path2.SplashActivity.
    DEVICE SHELL COMMAND: am start -n "com.path1.pathOLD/com.path1.path2.SplashActivity"
    Starting: Intent { cmp=com.path1.pathOLD/com.path1.path2.SplashActivity }
    Error type 3
    Error: Activity class {com.path1.pathOLD/com.path1.path2.SplashActivity} does not exist.
    

    It seems that the app tries to start the Splash activity using OLDpath/NEWpath.Splash path and the error is there, but I can't find why it is using such path.

    I use IntelliJ IDE. Any ideas? Could it be in the Filter in the 2nd activity in the Manifest?!

    • user370305
      user370305 over 12 years
      So, your problem is solved or not?
    • Lalit Poptani
      Lalit Poptani over 12 years
      please come to casual chat room so that we can discuss your issue there.
    • user370305
      user370305 over 12 years
      ok, just change package name in manifest file write this "com.path1.pathOLD" and run your application again and let me know what happen...
  • Lalit Poptani
    Lalit Poptani over 12 years
    Ah!!! we both have the answer but I took it a while to describe the whole answer, never mind +1. I think this is the only problem.
  • user370305
    user370305 over 12 years
    @Lalit Poptani - I think, In manifest file have problem, look at this line too, <action android:name="com.path1.path12.SomeActivity"/> this in <intent - filter>.
  • sandalone
    sandalone over 12 years
    @user370305 I also think that the problem is in the filter. But why it does not work if I renamed both string paths accordingly. It works usually, but it does not work ONLY after renaming the package name.
  • sandalone
    sandalone over 12 years
    @user370305 As I told Lalit, the error is not in the Manifest in the activity name, it was my typo. Sorry!
  • sandalone
    sandalone over 12 years
    Please correct your answer so that we do not confuse others. Thanks
  • Lalit Poptani
    Lalit Poptani over 12 years
    I think now the problem should be resolved it was the package name error.
  • sandalone
    sandalone over 12 years
    It is not, please look at the rewritten question. I confused names while writing the question, while the manifest holds the write names.
  • user370305
    user370305 over 12 years
    I think, Eclipse has inbuilt updating task for package name at the time of launching application.
  • Phobos
    Phobos over 11 years
    This is a horrible bug in a great IDE. I went from Eclipse to IntelliJ and have not had any problems until this bug hit me. Thank you for answering your own question for the rest of us. You are a good community member.
  • Alexander Romanov
    Alexander Romanov about 11 years
    Thanks so much for answering your own question. I added new activity which I wanted to start first, but IntelliJ IDEA kept saying that I must add <action android:name="android.intent.action.MAIN"/> to an old one. Solution was to set radio button to Launch Default Activity
  • Admin
    Admin about 11 years
    Great job finding the solution. I found the screen you show here at the top menu at run>edit configurations, and after changing to "launch default activity" the project ran.
  • Marian Klühspies
    Marian Klühspies about 10 years
    Thank you. The option "start default activity" was active and seemed to find the wrong activity internally, so now I´ve choosen the start activity manually
  • shawkinaw
    shawkinaw over 9 years
    Despite trying everything else in this thread, still had no luck until I did this as well.
  • marbdq
    marbdq over 9 years
    This fixed the problem with Android Studio 1.0. Thanks!
  • The Original Android
    The Original Android over 9 years
    In my view this answer is better than the one posted as Best. In my case, I deleted the 2 directories of .idea as well. I had to close the Studio and import the Gradle project, detecting the many changes I made.
  • Falco Winkler
    Falco Winkler about 9 years
    This fixed my problem after renaming package in android studio 1.1.0
  • jayeffkay
    jayeffkay about 9 years
    Nothing helped until I tried delete cache as described here stackoverflow.com/a/25602161/630833
  • Bruce
    Bruce about 8 years
    Tried this and it doesn't work, I guess this fixes a different problem.
  • DarkoM
    DarkoM almost 8 years
    Removing .idea/workspace.xml fixed this persisting problem. Thank you!
  • geisterfurz007
    geisterfurz007 over 7 years
    This is still existing in 2016.3.4! I think this would have taken ages for me and I would have just renamed it back... This should really(!) be fixxed...
  • Santiago
    Santiago almost 6 years
    Works like a charm in Android Studio 3.1.2