I was making a simple android plugin for unity these days. It was tough for a noob like me but I finally successfully exported a .jar file and complied and ran it on an android device without errors. However, i got stuck when i use the unity native call to android class, and it keeps saying :
“I/Unity﹕ AndroidJavaException: java.lang.NoSuchMethodError: no static method with name=‘StartActivity’ signature=‘(Ljava/lang/String;Lcom.unity3d.player.UnityPlayerNativeActivity;)V’ in class Lcom/unity3d/player/UnityPlayerNativeActivity;”.
Here are my code pieces:
MainActivity.java
package com.example.eclipsetestplugin;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity{
private static Context mContext = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
Log.d("MainActivity", "Main Activity Created");
}
public static void StartActivity(String name, Context c)
{
Intent intent = new Intent(c,TestActivity1.class);
intent.putExtra("name",name);
c.startActivity(intent);
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eclipsetestplugin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.eclipsetestplugin.MainActivity"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
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="com.example.eclipsetestplugin.TestActivity1"
android:label="TestActivity1">
</activity>
</application>
</manifest>
TestAndroidModule.cs
public class TestAndroidModule : MonoBehaviour {
void Awake()
{
AndroidJNI.AttachCurrentThread();
}
void OnGUI()
{
if(GUILayout.Button("Goes To Activity One", GUILayout.Height(200)))
{
using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
{
jo.CallStatic("StartActivity", "This is Activity Page One", jo);
}
}
}
}
}
UPDATE
I have to switch to using Eclipse ADT since there is not much threads talking about using Android Studio to make Unity Plugins : (
After another day of digging, i progressed to find something tricky. When I run the apk on my Android device, there are two lines of log:
I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.eclipsetestplugin/com.unity3d.player.UnityPlayerNativeActivity} from pid 634
I/ActivityManager﹕ Start proc com.example.eclipsetestplugin for activity com.example.eclipsetestplugin/com.unity3d.player.UnityPlayerNativeActivity: pid=30085 uid=10133 gids={50133, 1028}
it seems that the UnityPlayerNativeActivity class is somehow nested inside the com.example.eclipsetestplugin pkg. I am not sure if this is the case, but i suppose it should not have this nested structure. And I think this also explains why unity java call cannot find the method ‘StartActivity’ in UnityPlayerNativeActivity, since the path is com.example.eclipsetestplugin/.UnityPlayerNativeActivity.
I am sure i somehow imported the class.jar in a wrong way, here i posted my project structure from eclipse:
Further, I tested the plugin by creating another MainActivity java class, and calling StartActivity from this class:
if(GUILayout.Button("Goes To Activity One", GUILayout.Height(200)))
{
using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
{
AndroidJavaClass main = new AndroidJavaClass("com.example.eclipsetestplugin.MainActivity");
main.CallStatic("StartActivity", "This is Activity Page One", jo);
}
}
}
Now the log changed to this!:
I/ActivityManager﹕ START u0 {cmp=com.example.eclipsetestplugin/.TestActivity1 (has extras)} from pid 31231
I/Unity﹕ AndroidJavaException: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.eclipsetestplugin/com.example.eclipsetestplugin.TestActivity1}; have you declared this activity in your AndroidManifest.xml?
Obviously, the method is found and been called from the newly created java class, however it crashes because it cannot find TestActivity1. It’s location nested inside the pkg again!!!
I think I am almost there!
Any one could advise? BIG THANKS!