Creating and Using Unity Plugins with Android Studio
Setting Up the Android Studio Project
-
Create a new project:
- In Android Studio, navigate to
File > New > New Project. - Enter a suitable Application Name and Company Domain, ensuring the Package Name matches the Unity project's Bundle Identifier.
- Click
Next, selecting "Phone and Tablet" and specifying the Minimum SDK. - Choose "Empty Activity" and use default values for the Activity and Layout Names.
- Click
Finishto generate the project.
- In Android Studio, navigate to
-
Add Unity's
classes.jar:- Copy the
Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jarfile from the Unity installation directory. - Place it in the
libsdirectory of your Android Studio project. - Right-click on
Project, selectOpen Module Settings, and addclasses.jaras a dependency.
- Copy the
Developnig the Android Code
Edit the main activity to include the necessary Unity integration:
import android.os.Bundle;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void ShowToast(final String message){
runOnUiThread(() -> Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show());
}
public static String GetInformation() {
return "This is a Plugin's content!";
}
}
Modifying the Build Configuration
Adjust build.gradle to configure the project for AAR export:
- Replace
apply plugin: 'com.android.application'withapply plugin: 'com.android.library'. - Remove the
applicationIdentry. - The modified
build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
minSdkVersion 18
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation files('libs/classes.jar')
}
Finalizing the Android Manifest File
Ensure the manifest file contains the appropriate <meta-data> information for the main activity. Remove unnecessary resources such as the styles.xml file.
Exporting and Integrating the Plugin with Unity
- Build the AAR file by selecting
Build > Build APK. - Remove the
classes.jarfile from the generated AAR, as Unity includes its copy during APK generation.
Importing the Plugin into Unity
- Create a Unity project.
- Import the generated AAR file:
- Create the
Plugins/Androiddirectory in your Unity project. - Place the AAR file and Android Manifest in the
Androidfolder.
- Create the
Implementing the Unity Code
Create a script to interface with the Android functionality:
using UnityEngine;
public class Call : MonoBehaviour {
string information_ = null;
void OnGUI() {
if(GUI.Button(new Rect(0, 0, 200, 20), "Show Toast - Hello World!")) {
using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")) {
jo.Call("ShowToast", "Hello World!");
}
}
}
if (GUI.Button(new Rect(0, 40, 200, 20), "Get Plugin's Information")) {
using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")) {
information_ = jo.CallStatic<string>("GetInformation");
}
}
}
GUI.Label(new Rect(220, 40, Screen.width - 220, 20), information_);
}
}
Set the Unity project's Bundle Identifier and Minimum API Level to match the plugin's configuration. Build the Unity APK and test the functionality.