Starting AppCompatActivity from Unity C# Script

Hello,
I have a hello world project. I have class called StartActivity class which extends from AppCompatActivity. I have created an .aar lib and placed it under unity plugins folder. The idea is to launch the activity from unity android app so I am starting the activity from unity. When I try to start my activity from unity it throws an error saying the class StartActivity could not be found. When I extend the same class from Activity or UnityPlayerActivity class it works. But I need AppCompatActivity class features so cannot move to other classes.
This my manifest file

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

    <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="com.example.sitagy.helloworld.StartUpActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name = "com.example.sitagy.helloworld.StartActivity"></activity>
        <activity android:name = "com.example.sitagy.helloworld.MainActivity"></activity>
        <activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
          android:label="@string/app_name">
        <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
        <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" /> 
        </activity>

    </application>

</manifest>

The following is my java class

public class StartActivity extends AppCompatActivity {

    private String TAG = "Plug.StartActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("SY : on create start neew activity");

    }
    public static void Call(Activity activity)
    {
        // Creating an intent with the current activity and the activity we wish to start
        Intent myIntent = new Intent(activity, StartActivity.class);
        activity.startActivity(myIntent);
    }
    public static void CallStatic()
    {
        System.out.println("CallStatic")
    }

The following is my c# code

    void Start()
    {
   
        var androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        var jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
        var jc = new AndroidJavaClass("com.example.sitagy.helloworld.StartActivity");
        jc.CallStatic("Call", jo);

}

If I extend the StartActivity to just regular activity it works. Not sure if I am doing something wrong. I have the AppCompat libs under plugins folder

This is the error for adb log
7-29 19:34:35.305 657 3527 D audio_hw_primary: disable_audio_route: usecase(1) reset and update mixer path: low-latency-playback speaker
07-29 19:34:35.316 19519 19542 E Unity : AndroidJavaException: java.lang.ClassNotFoundException: com.example.sitagy.helloworld.StartActivity
07-29 19:34:35.316 19519 19542 E Unity : java.lang.ClassNotFoundException: com.example.sitagy.helloworld.StartActivity
07-29 19:34:35.316 19519 19542 E Unity : at java.lang.Class.classForName(Native Method)
07-29 19:34:35.316 19519 19542 E Unity : at java.lang.Class.forName(Class.java:454)
07-29 19:34:35.316 19519 19542 E Unity : at java.lang.Class.forName(Class.java:379)
07-29 19:34:35.316 19519 19542 E Unity : at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
07-29 19:34:35.316 19519 19542 E Unity : at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
07-29 19:34:35.316 19519 19542 E Unity : at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source:72)
07-29 19:34:35.316 19519 19542 E Unity : at android.os.MessageQueue.next(MessageQueue.java:405)
07-29 19:34:35.316 19519 19542 E Unity : at android.os.Looper.loop(Looper.java:174)
07-29 19:34:35.316 19519 19542 E Unity : at com.unity3d.player.UnityPlayer$e.run(Unknown Source:32)
07-29 19:34:35.316 19519 19542 E Unity : Caused by: java.lang.ClassNotFoundException: com.example.sitagy.helloworld.StartActivity
07-29 19:34:35.316 19519 19542 E Unity : … 9 more
07-29 19:34:35.316 19519 19542 E Unity : at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in :0
07-29 19:34:35.316 19519 19542 E Unity : at UnityEngine.AndroidJNISafe.CallStaticObjectMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[ ] args) [0x00000] in :0
07-29 19:34:35.316 19519 19542 E Unity : at UnityEngine.AndroidJava
07-29 19:34:35.375 644 19586 E ResolverController: No valid NAT64 prefix (116, /0)
07-29 19:34:35.851 644 19590 E ResolverController: No valid NAT64 prefix (116, /0)
07-29 19:34:37.064 19519 19542 E Unity : AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
07-29 19:34:37.064 19519 19542 E Unity : java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
07-29 19:34:37.064 19519 19542 E Unity : at java.lang.Class.classForName(Native Method)
07-29 19:34:37.064 19519 19542 E Unity : at java.lang.Class.forName(Class.java:454)
07-29 19:34:37.064 19519 19542 E Unity : at java.lang.Class.forName(Class.java:379)
07-29 19:34:37.064 19519 19542 E Unity : at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
07-29 19:34:37.064 19519 19542 E Unity : at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
07-29 19:34:37.064 19519 19542 E Unity : at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source:72)
07-29 19:34:37.064 19519 19542 E Unity : at android.os.MessageQueue.next(MessageQueue.java:405)
07-29 19:34:37.064 19519 19542 E Unity : at android.os.Looper.loop(Looper.java:174)
07-29 19:34:37.064 19519 19542 E Unity : at com.unity3d.player.UnityPlayer$e.run(Unknown Source:32)
07-29 19:34:37.064 19519 19542 E Unity : Caused by: java.lang.ClassNotFoundException: android.support.v7.app.AppCompatActivity
07-29 19:34:37.064 19519 19542 E Unity : … 9 more
07-29 19:34:37.064 19519 19542 E Unity : at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in :0
07-29 19:34:37.064 19519 19542 E Unity : at UnityEngine.AndroidJNISafe.CallStaticObjectMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[ ] args) [0x00000] in <filename unkno

Any help would be great!!
Thanks in advance everyone

I was able to resolve it. So we need to add every supported library mentioned under External Libraries in Android Studio to Unity Plugin/Android folder.

1 Like