"Building and using Plug-ins for Android"
1. AAR Plug-ins and Android Libraries
Android Archive (AAR) Plug-ins is bundles that include compiled Java and native (c + +) code, resources, and an Android M Anifest. The. aar file itself is a zip Archive which contains all of the Assets.
To add a AAR plug-in to your project, copy the. aar file into Any of the your project folders, then select It in Unity to open the Import Settings in the Inspector window. Tick the Android checkbox to mark this. aar file as compatible with Unity:
AAR is the recommended plug-in format for Unity Android applications.
2) Android Library
Android Library projects is similar to AAR plug-ins:they contain native and Java code, resources, and an android Manifes T. However, an Android Library was not a single archive file, but a directory with a special structure WH Ich contains all of the Assets.
Unity treats any subfolder of Assets/plugins/android as a potential Android Library, and disables Asset importing From within these subfolders. The subfolder is recognized as an Android Library if it contains the Androidmanifest.xml file, and the Project.Properties file contains the string android.library=true .
3) providing additional Android Assets and resources
If you need to add Assets to your Unity application that should being copied unchanged into the output package, import them I Nto the assets/plugins/android/assets directory. They appear in the assets/directory of your APK, and is accessed by using the Getassets () Android API from your Java cod E.
2. JAR Plug-ins
They can only contain Java code (for example, they can ' t contain Android resources), which makes their use very limited.
To add a jar plug-in to your project, copy the. jar file into Any of the your project folders and then select I T in Unity to open the Import Settings in the Inspector window. Tick the Android checkbox to mark this. jar file as compatible with Android:
Using Java plug-ins Unity uses the Java Native Interface (JNI) both when calling code from Java and when interacting with Java or the Java VM (Virtual machine) from native code or C # scripts.
2) Using your Java plug-in from C # Scripts with helper classes
AndroidJNIHelperthe and AndroidJNI Unity API classes is used as a wrapper around a "raw" JNI interface.
The Androidjavaobject and Androidjavaclass Unity API classes automate a lot of tasks when using JNI calls, and they also u SE caching to make calls to Java faster. AndroidJavaObject AndroidJavaClass AndroidJNI The combination AndroidJNIHelper of and is built in top of and, but also have some additional functionality.
Androidjavaobject Jo =NewAndroidjavaobject ("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); inthash = Jo. call<int> ("hashcode"); //JNI. Getmethodid (ClassID, "Hashcode", "() I"); //JNI. Callintmethod (ObjectID, methodid);View Code
Androidjavaclass JC =NewAndroidjavaclass ("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);View Code
Public classNewbehaviourscript:monobehaviour {voidStart () {Androidjnihelper.debug=true; using(Androidjavaclass JC =NewAndroidjavaclass ("Com.unity3d.player.UnityPlayer") ) {JC. Callstatic ("Unitysendmessage","Main Camera","Javamessage","Newmessage"); } } voidJavamessage (stringmessage) {Debug.Log ("message from Java:"+message); }} View Code
The Mono garbage collector should release all created instances AndroidJavaObject AndroidJavaClass of and after use, but it's advisable to keep them in a using(){} statement to ensure they is deleted as soon as possible.
//Getting the system language safelyvoidStart () {using(Androidjavaclass CLS =NewAndroidjavaclass ("Java.util.Locale")) { using(androidjavaobject locale = cls.) Callstatic<androidjavaobject> ("Getdefault") {Debug.Log ("Current lang ="+ locale. call<string> ("Getdisplaylanguage")); } } }
Building and using Plug-ins for Android