2

I have two modules. App and module Test. Module app contains MainActivity with button. Module Test contains MainActivity two. I want to run Test/MainActivity from module app by click on button. But Android still cannot see MainActivity from module Test. See below.

Module app Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.main">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.example.test.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

Module Test manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Code in Module app MainActivity for click button and run second Activity:

 public void runActivity(View view) {

    Intent intent = null;
    try {
        intent = new Intent(this, Class.forName("com.example.test.MainActivity"));
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

In Console I get an error:

W/System.err: java.lang.ClassNotFoundException: com.example.test.MainActivity

EDIT: In app build.gradle file I have:

implementation project(path:':test', configuration:'default')

EDIT 2: Here is link with complete example that not working.

Musketyr
  • 745
  • 1
  • 16
  • 37

4 Answers4

0

You have multiple options for implementing this:

1- if Module test is a dependency for module App, then you can normally access Test/activity class from module App.

2- You can implicity start Test/Activity using intent filters & actions, See docs here and this is an example

3- If Module test have a broadcast receiver, you can send broadcast (either explicit or implicit) to it and open activity from there

Atef Hares
  • 4,715
  • 3
  • 29
  • 61
0

You can access the activity in another module by the same ways you're accessing your current module activity. So, this should works:

public void runActivity(View view) {
  Intent intent = new Intent(this, com.example.test.MainActivity.class);
  startActivity(intent);
}

But you should not do it because you make your App module is tightly coupled with Test module. Instead, you need to make a Navigator class inside your Test module. The Navigator should be the only way to access the activity from external module. The Navigator class is a singleton class.

You can create a Navigator class inside the Test module with something like this:

public class Navigator {
    private Navigator(){}

    public static Navigator getInstance() {
        return NavigatorHolder.INSTANCE;
    }

    private static class NavigatorHolder {
      private static final Navigator INSTANCE = new Navigator();
    }

    public void navigateToMainTest(Context context) {
      Intent intent = new Intent(context, MainActivity.class);
      context.startActivity(intent);
    }
}

Then you can launch the MainActivity of Test module with:

Navigator.getInstance().navigateToMainTest(yourAppActivity.this);

By using the Navigator, you can encapsulate what the inside of your Test module. You can change navigateToMainTest to launch an AlertDialog instead of Activity without affecting the App module.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

Finally, I found solutions, here: Unable to resolve dependency...

Now, Android Studio can see Module with MainActivity and I can run it.

Musketyr
  • 745
  • 1
  • 16
  • 37
0

If anyone still stuck with this, here is what worked for me ->

Step 1. Change this line in your build.gradle(Module) from -> apply plugin: 'com.android.application' to -> apply plugin: 'com.android.library'

Step 2. Delete this line from your build.gradle(Module) -> applicationId "com.test.something"

Step 3 add this line to your build.gradle(Module) in

dependencies{

...

    implementation project(path: ':<module_name>')

...

} 

Step 4.

Re-sync and Rebuild, Done

Step 5. (To Launch the activity)

var intent: Intent? = null
try {
     intent = Intent(this,
     Class.forName("com.example.fill.Activity"))
     startActivity(intent)
     } catch (e: ClassNotFoundException) {
             e.printStackTrace()
     }
xriminact
  • 55
  • 5