- Home /
Answer by tebandesade · Feb 25, 2015 at 10:02 PM
UNITY 1. Create a new Unity project. Switch to the Android platform. 2. Save the empty scene, add it to the build settings. Set the bundle identifier in the player settings. 3. Build and Run the application. You should see the Unity logo and a blue background.
ECLIPSE 1. Open Eclipse with the ADT plugin. If you don't have Eclipse, you can install it from here: http://developer.android.com/sdk/installing/bundle.html 2. Create a new Android Application Project. Name it "[NAME]" 3. Once the project is created, run it on the device to verify it works. 4. Open the Project properties in Eclipse and in the Android tab, tick the "Is Library" option. 5. Create a new class for your Project, named [NAMEPROJECT].
Eclipse will build a .Jar file. You need to copy that file into Unity. The file is found in your workspace. The easiest way to get the path is to select the file in Eclipse and open the Properties window. You can look at what's inside the .jar file if you add the .zip extension to it and open it with any application which can view zip files.
UNITY In the Project view, create a "Plugins" folder. Then create an "Android" folder inside the plugins folder. Create a new C# file in Unity, and name it "CallJavaFromUnity .cs". Then just start calling EXAMPLE
//The comments describe what you would need to do if you were using raw JNI
AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some_string");
// jni.FindClass("java.lang.String");
// jni.GetMethodID(classID, "<init>", "(Ljava/lang/String;)V");
// jni.NewStringUTF("some_string");
// jni.NewObject(classID, methodID, javaString);
int hash = jo.Call<int>("hashCode");
// jni.GetMethodID(classID, "hashCode", "()I");
// jni.CallIntMethod(objectID, methodID);
or
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
// jni.FindClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
// jni.GetStaticFieldID(classID, "Ljava/lang/Object;");
// jni.GetStaticObjectField(classID, fieldID);
// jni.FindClass("java.lang.Object");
Debug.Log(jo.Call<AndroidJavaObject>("getCacheDir").Call<string>("getCanonicalPath"));
// jni.GetMethodID(classID, "getCacheDir", "()Ljava/io/File;"); // or any baseclass thereof!
// jni.CallObjectMethod(objectID, methodID);
// jni.FindClass("java.io.File");
// jni.GetMethodID(classID, "getCanonicalPath", "()Ljava/lang/String;");
// jni.CallObjectMethod(objectID, methodID);
// jni.GetStringUTFChars(javaString);
All these examples are from the documentation
I use your sample, but log show "ClassNotFound" =(
AndroidJavaClass jc = new AndroidJavaClass("com.my.class");
Your answer
![](https://koobas.hobune.stream/wayback/20220613184327im_/https://answers.unity.com/themes/thub/images/avi.jpg)