Package rename and error “Activity class does not

2019-01-13 21:56发布

问题:

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?!

回答1:

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

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


回答2:

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



回答3:

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>



回答4:

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


回答5:

I've had the same error message in Eclipse/ADT (usually when two people are working on the same project using different IDEs) and fixed it by adding the package name to the beginning of the Activity name (and all the other Activities, Receivers, Services, etc.) in AndroidManifest.xml.

In this example, that would mean changing this:

<activity android:name=".SplashActivity"

To this:

<activity android:name="com.path1.path2.SplashActivity"


回答6:

I had this problem and it was because of a problem in manifest file. Make sure your manifest file doesn't have any problem.



回答7:

Had the same problem. After trying everything in this thread it turned out to be Android Studio's instant run.

Disabling this fixed the issue straight away as does deleting everything under .android/build-cache (of the specific build if you can find it). The reason being is that instant run appears to use a cached version of the app which isn't even removed during a regular clean/rebuild.



回答8:

I also had this problem and specifically when switching flavours. So what solved it for me is disabling Instant Run in android. I will like to include some important points regarding this-

  • Make sure your manifest is ok.
  • Maximum times cleaning the project and rebuilding it will solve the problem.
  • Disable instant run in the android studio above 2.1.
  • Uninstall the app and reinstall it.


回答9:

Make sure your package name is same in Gradle , Manifest. Go to Module Settings -> Flavors -> Application ID -> (your package Name)



回答10:

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