Unity and Android Interactive Notes - Connect to Android SDK

This article will be helpful for those who have relevant experience. If you do not have relevant experience, step by step to learn the Android and Unity basics. If there is something you can't read, please leave a message...

I. Unity and Android Interaction

Currently, there are two main options for Unity to interact with Android:

  1. Android prints out jar packages or aar packages and places them in Unity's plugin/Android folder for invocation;
  2. Unity exports Android projects for Android Studio (Eclipse was also supported here in the early days, but that's too ugly to discuss here).

1. Let's start with how Unity interacts with Java

Unity interacts with Java, and Unity provides us with a very useful tool class, Android JavaClass, with less code and nonsense.

public class SDK : MonoBehaviour
    private static SDK sdkInstance;
    private AndroidJavaClass androidJavaClass;
    private AndroidJavaObject androidJavaObject;
    public static SDK Instance
            if (sdkInstance == null)
                sdkInstance = new SDK();
                return sdkInstance;
                return sdkInstance;
    private SDK()
    public void Init()
        androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity");

//Get device ID
    public string GetDeviceID()
        string s = androidJavaObject.Call<string>("getDeviceId");//This is the interface method that calls the registration in Android Activity. How to register is described below
        return s;
        return "123456";//This is to write a temporary return value without SDK to prevent some exceptions

    public void ShowSDKLoginTips()

        androidJavaObject.Call("showSDKLoginTips");//Here is the no return value method in the interface

Here is the corresponding Android MainActivity method:

public class MainActivity extends UnityPlayerActivity {
    public MainActivity() {
    protected void onCreate(Bundle savedInstanceState) {
    public String showTest() {
        return "show test success";
//Here is the interface method to get the device ID
    public String getDeviceId() {
        String m_szDevIDShort = "35" + Build.BOARD.length() % 10 + Build.BRAND.length() % 10 + Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 + Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10;
        String serial = null;

        try {
            serial = Build.class.getField("SERIAL").get((Object)null).toString();
            return (new UUID((long)m_szDevIDShort.hashCode(), (long)serial.hashCode())).toString();
        } catch (Exception var4) {
            serial = "serial";
            return "null";
//Here is the interface method
    public void showSDKLoginTips()


1. Let's start with Unity's introduction of the Jar package

First create a new project in Android Studio, an Empty Activity, and then introduce Unity's class. Below the jar package is the relative path to the jar package, referring to your Unity installation folder:. / Unity\Editor\Data\Playback Engines\AndroidPlayer\Variations\mono\Development\Classesclass. Jar (how to bring in the Jar package to refer to the Android Studio tutorial yourself, followed by a link to another tutorial)

Make sure you handle build if you want to make a Jar package. Gradle will first apply plugin:'com. Android. Change application'to apply plugin:'com. Android. Library'

) Write your own interface in MainActivity (you can customize the name, but remember to inherit UnityPlayerActivity or something will go wrong), and I copy and don't paste on the code.

Finally, jar-pack it out:

Build in the project root directory. Add the following code at the end of the gradle:

task makeJar(type: Copy) {
    Delete'build/outputs/mydemosdk. The jar'//mydemosdk name is customizable
    from('app/build/intermediates/aar_main_jar/release/')//Only applicable to androidstudio 3.0 or 4.0 and above
    In ('build/outputs/')//output path, customizable
    Rename ('classes.jar','mydemosdk.jar') //111 name customizable

Then in the upper right corner of Android Studio Gradle, you will see the following method path for the screenshot:. / task/other

There is a pit to avoid here: from('app/build/intermediates/aar_main_jar/release/')//only for androidstudio 3.0 or 4.0 and above, where aar_ Main_ Jar may have different names in different versions and can be found in the intermediates folder by itself. (Note: jar packages can be named after they are stuffed into Unity)

If aar is needed, aar looks for itself in the output directory (there is also a hole here, remember to open the aar package with the compression tool and delete the class.jar, otherwise there will be problems).

Finally, importing the jar package into the Unity project requires the introduction of AndroidManifest.xml to project

When Unity publishes APK for Android

If no AndroidManifest file exists in Plugins/Android (Unity special directory), the Unity default AndroidManifest file is used. The default AndroidManifest file directory is: Unity\Editor\Data\PlaybackEnginesAndroidPlayer\Apk If an AndroidManifest file exists, Unity will use it instead of the default AndroidManifest file, which must have an activity and label.

Reference resources:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackge.test"<!--Fill in your own package name here-->
    android:versionName="1.0" >
        android:targetSdkVersion="28" />
        android:usesCleartextTraffic="true" >
        <activity android:name="com.mypackge.test.MainActivity" ><!--Prefix this with your package name to avoid reference conflicts-->
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            <!--Remember to add this parameter here, otherwise you will get an error-->
                android:value="true" />

2. What's more, Unity's solution for exporting Android projects

Here's a trick: Unity writes references to the Android method when exporting a project without importing jar packages; it just needs to export in Android

Export the Android project as shown in the figure. There is a heavenpit, and you will see later:


After exporting the Android project, you can open the project in Android Studio, and the editing work in Android refers to the Activity code above.

3. A sink stepped on

Player settings need to do this when introducing a tripartite SDK when Scripting Backend chooses IL2Cpp (the shelf version needs to support 64 bits), or when you open a plug-in program (such as a tripartite SDK), you will find an unexpected crash, which is initially considered a monolib.so file problem (that is, Unity's pot)

4. Thank you and reference link address

I have just started to pick up what Android and IOS are connected to. Some ideas are not perfect. If they mislead the reader, please forgive me. I accept any comments and criticism and will correct them as soon as possible. *

Thank you here for @ lizhengwei1989@jiaxunnanling The two articles of the two big guys made me avoid some pothole-stepping things (and forgot about them for a long time). Thank the big guys for letting me improve once, by the way, quote the next two articles.

If there is any infringement, please contact and I will delete the reference myself.

Finally, attach the address of the big man's article:

Android interacts with Unity and various pits to step on: https://blog.csdn.net/lizhengwei1989/article/details/54631241  

Unity interacts with Android - basics: https://blog.csdn.net/jiaxunnanling/article/details/89947744

Tags: Android Unity

Posted by snap2000 on Mon, 30 May 2022 04:49:25 +0530