Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Starting AppCompatActivity from Unity C# Script

Discussion in 'Editor & General Support' started by sita, Jul 31, 2020.

  1. sita

    sita

    Joined:
    Jul 30, 2015
    Posts:
    9
    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
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.     package="com.example.sitagy.helloworld">
    4.  
    5.     <application
    6.         android:allowBackup="true"
    7.         android:icon="@mipmap/ic_launcher"
    8.         android:label="@string/app_name"
    9.         android:roundIcon="@mipmap/ic_launcher_round"
    10.         android:supportsRtl="true"
    11.         android:theme="@style/AppTheme">
    12.  
    13.         <activity android:name="com.example.sitagy.helloworld.StartUpActivity">
    14.             <intent-filter>
    15.                 <action android:name="android.intent.action.MAIN" />
    16.                 <category android:name="android.intent.category.LAUNCHER" />
    17.             </intent-filter>
    18.         </activity>
    19.         <activity android:name = "com.example.sitagy.helloworld.StartActivity"></activity>
    20.         <activity android:name = "com.example.sitagy.helloworld.MainActivity"></activity>
    21.         <activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
    22.           android:label="@string/app_name">
    23.         <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
    24.         <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
    25.         </activity>
    26.  
    27.     </application>
    28.  
    29. </manifest>
    The following is my java class
    Code (CSharp):
    1. public class StartActivity extends AppCompatActivity {
    2.  
    3.     private String TAG = "Plug.StartActivity";
    4.     @Override
    5.     public void onCreate(Bundle savedInstanceState) {
    6.         super.onCreate(savedInstanceState);
    7.         System.out.println("SY : on create start neew activity");
    8.  
    9.     }
    10.     public static void Call(Activity activity)
    11.     {
    12.         // Creating an intent with the current activity and the activity we wish to start
    13.         Intent myIntent = new Intent(activity, StartActivity.class);
    14.         activity.startActivity(myIntent);
    15.     }
    16.     public static void CallStatic()
    17.     {
    18.         System.out.println("CallStatic")
    19.     }
    20.  
    The following is my c# code
    Code (CSharp):
    1.     void Start()
    2.     {
    3.    
    4.         var androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    5.         var jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
    6.         var jc = new AndroidJavaClass("com.example.sitagy.helloworld.StartActivity");
    7.         jc.CallStatic("Call", jo);
    8.  
    9. }
    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
    upload_2020-7-30_18-38-6.png

    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 <filename unknown>:0
    07-29 19:34:35.316 19519 19542 E Unity : at UnityEngine.AndroidJNISafe.CallStaticObjectMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>: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, <unspecified>/0)
    07-29 19:34:35.851 644 19590 E ResolverController: No valid NAT64 prefix (116, <unspecified>/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 <filename unknown>: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
     
  2. sita

    sita

    Joined:
    Jul 30, 2015
    Posts:
    9
    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.
     
    theodhoribeka likes this.