I have exactly the same problem. I have 2 android plugins, Vuforia and my plugin, but only one activity can be the “currentActivity” of Unity:
AndroidJavaClass jc = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
AndroidJavaObject jo = jc.GetStatic(“currentActivity”);
→ In the manifest, if the activity declared as “android.intent.action.MAIN” is the Vuforia one then I cannot launch my activities, and vice versa, if my root activity is the MAIN, then Vuforia cannot be launched.
I wonder how 2 main activities can co-exist or how to launch two activities from different packages.
Does anyone have found a solution?
Thanks for your help
I found a solution on my side : grab the current active activity (can be an other android plugin or your own activity) and start statically a new activity from it:
In the .cs file:
#if UNITY_ANDROID
//Grab the current activity (the one declared as MAIN in the manifest - can be an other plugin)
AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity");
//Give the activity instance to my own static method to launch an activity from there
var jc = new AndroidJavaClass("com.my.package.RootActivity");
jc.CallStatic("launchActivity", ajo, showUI, true, "string", 0);
#endif
In the com.my.package.RootActivity java file :
public static void launchActivity(Activity root, boolean param1, String param2, int param3) {
Intent intent = new Intent(root, CameraActivity.class);
intent.putExtra(RootActivity.EXTRA_BOOLEAN_PARAM1, param1);
intent.putExtra(RootActivity.EXTRA_STRING_PARAM2, param2);
intent.putExtra(RootActivity.EXTRA_INT_PARAM3, param3);
root.startActivity(intent);
}
I think for those who wants to use Vuforia and their plugins in the same custom activity this article is really useful. It explains how to extend Vuforia QCARPlayerNativeActivity.
Use Vuforia with AdMobe (by Google). Both require the declaration of a main activity in the manifest and the two do not fear coexist. I am a beginner programmer and do not know how to extend the plugin AdMobe with Vuforia. Can someone help me?
My problem seems to be the same as your (Except i use Metaio and a custom NFC plugin). So can you explain more in depth how your plugin works? I understand the C# code but I don’t understand the java part. Should I put this chunk of code in my own Eclipse Project and recompile it in a new .jar?
What is the difference between Call and CallStatic? i have an android plug in developed by me.
And works fine when i use alone, i have another project which runs Vuforia, Cardboard and leap motion plugins all working fine with no change. Why they work fine and mine is not?
I need to understand how this works?
is is possible to make the plugin with no activity? just services or classes? so we call them form Unity?
or is mandatory to have an activity? i just couldnt find a post that explains the plugins stuff.
Exactly, I was thinking about just the same, and you’re absolutely right. There is no need to subclass main activity, nor include a single AndroidManifest.xml at all. Android plugins can be wrapped in Fragment classes as well (kind of sub-activities). They spare the stuff mentioned above, while you can still start and receive results from native Activities. Also it provides a single instance for Unity, so C# code can invoke methods on the singleton directly.
I spend days on research / prototype this, gonna publish an exhaustive tutorial on the subject.
Wow, it has even more details since than, with project setup, single folder plugin (great for git submodules / to be able work on the plugin in client projects), and more. Now I have to apply it in a production production project, but I’m planning to do the tuts on the weekend, along with an example github repo.
eppz, thank you for making that tutorial. Do you have it ready? If so, could you please post the link here? I’m trying to follow the examples above. In particular, Psykna’s post on Oct 12, 2012.
I am now using Unity 5.3.3p3.
Psykna, thank you for providing that solution. For some reason, the Unity Android app cannot recognize the class I am including in this line in place of CameraActivity.class in the Java file:
04-13 18:52:52.218 18259 18294 I Unity : java.lang.NoClassDefFoundError: com.example.myapp.MyActivity
Also, for this line, I used a custom class not inherited from Activity instead of the custom RootActivity, although inheriting it from Activity did not fix it either:
I use Android Studio 2.0 for the Android development, and I see that the MyActivity.class is included in the jar file. It is also defined in my AndroidManifest.xml in Plugins/Android.
Hi,
I got a very annoying issue. All of my Android plugins in Unity just work if I dont use parameters in the calling functions. I am working on this issue since 2 days and I am about to give up. Even a minimal project doesnt work. I am sure I doing it right because the calling functions are working fine without parameter.
Unity:
AndroidJavaClass pluginClass = new AndroidJavaClass(“tnbrtrm.lications.de.myplugin.MyPlugin”);
String test = pluginClass.CallStatic(“getMessage”, new object[ ] { “alalalalala” });
Debug.Log("callShareAppAARPlugin: " + test);
Android:
public class MyPlugin extends UnityPlayerActivity {
The plugin prints “Hello world” when I delete the parameter “String text”, but with text I get this annoying error:
AndroidJavaException: java.lang.NoSuchMethodError: no static method “Ltnbrtrm/lications/de/myplugin/MyPlugin;.getMessage(Ljava/lang/String;)Ljava/lang/String;”
I see this error using Plugins as *.jar or *.aar. And I also had a look into this packages and my Classes and Functions are there. Otherwise it wouldnt work without parameters…
Can anyone please help me? I tried a lot of things… (older JDK versions, etc.)
Kind regards,
Tino