- Home /
Launching an external android app in C#/Unity
So i've been stuck on this problem for literally days. I have integrated Unity with Eclipse IDE and i can build and deploy projects perfectly. However, im trying to start an a basic Intent on the java side and trigger it on the Unity/C# side.
Here's my code for the Java side:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.unity3d.player.UnityPlayerNativeActivity;
public class AppLauncher extends UnityPlayerNativeActivity
{
public Intent myIntent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Assuming that we want to launch the browser to browse a website
Uri uri = Uri.parse("http://www.google.com");
myIntent= new Intent(Intent.ACTION_VIEW, uri);
}
public void Trigger()
{
startActivity(myIntent);
}
}
Here's the error im getting thrown by logcat when the C# trigger is hit:
And here's my code for the C# side of if it:
if(s[0].Equals("Spr"))
{
print("Launched");
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("Trigger");
}
Here's the error im getting thrown by logcat when the C# trigger is hit:
AndroidJavaException: java.lang.NoSuchMethodError: no method with name='Trigger' signature='()V' in class Lcom/unity3d/player/UnityPlayerNativeActivity;
I've tried screwing around by passong a custom signature along with the Trigger method name in the C# script, ive tried extending the standard UnityPlayerActivity, etc... I've tried hours worth of stuff and i Cannot seem to solve tis problem.
Any help is greatly accepted!
EDIT
This is the application launch section of the manifest:
<application android:icon="@drawable/app_icon" android:label="@string/app_name" >
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
Answer by liortal · Oct 19, 2014 at 09:09 AM
Your Java code looks OK.
Your C# code looks OK*
The only thing that's missing is you need to tell Unity that you have your own custom activity and it should use it.
In order to do that, you can follow the instructions in Building Plugins for Android, specifically the part under "Extending the UnityPlayerActivity Java Code".
Basically, you need to have an AndroidManifest.xml file that declares your own custom activity.
You copy the manifest to your project under Plugins/Android. Also, copy the compiled version of your Java code (.jar file) to Plugins/Android.
Follow the instructions in the link i provided to fully understand the process.
It is a safer practice to wrap usages of AndroidJavaClass and AndroidJavaObject in using { }
e.g:
// Makes sure the AndroidJavaObject JNI resources get disposed after usage
using (AndroidJavaObject ob = new .... )
{
// use android java object here
}
EDIT:
The modified AndroidManifest.xml should look something like this:
<application android:icon="@drawable/app_icon" android:label="@string/app_name" >
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<activity android:name=".AppLauncher" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="@string/app_name" android:screenOrientation="landscape" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I should point out that ive integerated unity with eclipse, so i import all my assets from unity into eclipse and build from there: refer to http://forum.unity3d.com/threads/integrating-unity-and-eclipse.71607/
I thoughtthat meant i didnt need to do all that unity plugin crap ?
Well, you need to tell the Android manifest which activity is the main activity of your app. If you don't modify the Android$$anonymous$$anifest.xml, it will launch UnityPlayerActivity by default, and that activity does not have any Trigger() method on it.
what do i need to replace to launch my main activity? I will edit my main post and post the manifest in the edit
updated my answer. I haven't tested it but you can see that i add your own AppLauncher activity, and removed the intent filter (action.$$anonymous$$AIN) from Unity's default activity
Your answer
![](https://koobas.hobune.stream/wayback/20220613165423im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
java android plugin worker thread ends up in Unity mainthread 1 Answer
Call a Android Studio method from Unity 0 Answers
Android app is not building in Unity, Error “Gradle initialization failed.” 1 Answer
c# or java for mobile version 2 Answers
How easy is Unity to use to create an android game if you know Java and some C#? 2 Answers