Write code to export the AAR package in Android Studio and interactively call it in Unity.

“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

Steps in AndroidStudio

First, open AndroidStudio to create a new project, the version is different, so the interface and steps may be different, but the core is the package name, the rest of the default is NextFile->New->New Module, select Android Library, and create a New Module. The name of my new Module here is MyunityLibrary. Create an EmptyActivity here and call it MainActivity. It will look like this

Now there are two android manifests, one for the project and one for the new unityLibrary. Here we copy all the red background from the app’s AndroidManifest into another AndroidManifest.

Everything in red is deleted. Android: Label change to the desired application name. And add this:

<meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
Copy the code

As shown in the configuration belowDelete the activity_main. XML file under Layout and delete the setContentView(R.layout.activity_main) from the newly created MainActivity.

Import Unity’s classes.jar file, My path is under D:\Program D:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes. Copy and paste into the libs directory.

Jar -> right ->AddAsLibrary-> UnityLibrary ->Ok.

Modify the MainActivity you just created: make it inherited from UnityPlayerActivity and add a method for Unity to call. Here’s the simplest way to add. UnityPlayer UnitySendMessage is Android calls the method of the Unity, the first parameter to mount script game object name, the second is the method name, the third for passing parameters. The next step is to export the AAR package and let Unity use it.

Build->Make Project Select UnityLibrary ->Build->Make Module” UnityLibrary “and wait for completion.

Find the AndroidMainfest and UnityLibrary.aar packages in the Build directory and copy them both.

Open the aar package and cut the classes.jar package into the libs folder instead of the original classes.jar package in libs

Modify the AndroidManifest in the ARR package to remove the Android :label line. If you don’t delete it, it conflicts with the outside AndroidManifest.Then modify just copied out of AndroidMainfest text: modify the registration packet=, here registration needs to be consistent with the unity package when the registration can be. (Try to modify here, do not be the same as the original package, because when I set the same, I may not find the method class in Android when unity calls it. The reason may be that the AndroidMainfest in the AAR package is the same as the AndroidMainfest package outside the AAR package, causing Unity to not know which one to use. It’s your guess, just try to be different.)At this point, the steps in AndroidStudio are complete, and you have an androidmainfest.xml and a named aar package, and the next steps are in unity.


Steps in Unity

Create a Plugins/Android folder under Project and place the.xml and aar packages in this folder. This configures the files you get in AndroidStudioNext, write a script in Unity to call the methods written in Android for debugging, create a Text, hang the script on the Canvas, create a Text, used to display the results. Create a script that calls the add method written in the arR package and displays the result in the text. If there is an error, you can see the error message directly from the phone. It is not necessary in actual operation.The debugging script is as follows,

 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
Copy the code
These two lines are fixed to get android relatedCopy the code
 jo.Call<int> ("add".2.3);
Copy the code

This line is calling the Add method in Android

The next step is to Package the Unity project. In Player Settings, look for the Package Name to be the same as the Packagename you set in AndroidMainfest, and then Build the Package.

Open the apK package, install it on your phone, and the number 5 is displayed, proving that Unity has successfully invoked Android. The text appears red, indicating that Android has successfully called Unity.