Create and import a custom android library in Unity3D
There are scenarios where we need to access devices or APIs using native libraries in Unity3D. When it comes to hardware API calls such as battery percentage or CPU usage on android builds, there is no direct way to access these on Unity3D.
In this section, we will create a native android plugin in .aar format and integrate it into the Unity3D application.
Getting Started
Table of contents
- Android project setup
- Define your custom library
- Build native library
- Use the native plugin inside the Unity3D project
- Native library integration
- Setup the project
- Run the app
We can use asset store and unity packages for implementing additional functionalities for our unity projects. However, there are scenarios where there are no unity package or asset store assets available for native android modules. We can solve the problem by building a native android plugin using Android studio.
An Android library includes everything required to build an app, including source code, resource files, and an Android manifest. However, an Android library compiles into an Android Archive (AAR) file which can be used be as dependencies on projects.
Let’s start creating our native plugin.
Requirements
- Android Studio
- Unity3D
A. Android project setup
1. Open Android Studio. Click on the new project button to create an android project. Select the Empty activity. Provide necessary information on the create new project window and click on the Finish button to create the project.
2. After the gradle build, right click on the app folder then select New → Module option to create a new Android module. Select the “Android Library” template. Keep the remaining information as defaults (you can alter it if required). Click on the Finish button to create a new Android module.
Define your custom library
We can define a custom library to display the battery level information in the UI using the Toast method. Let’s begin with our native plugin to print a string passed from Unity3D.
Right click on the module → java folder. Select New → Java Class and provide a valid name (In this example, we will use Alert).
Add the following code to the Alert class.
The Alert class has a PrintString()
method which takes two arguments — context and string.
Similarly, we can create one more class named BatteryLevelIndicator
to provide the battery level information.
Add the following code to the BatteryLevelIndicator
class.
Build native library
Press the “Make Project” button ( CTRL + F9 ) in the toolbar to build the Android Module.
After compilation, you will find an android archive file located under app\<library-name>\build\outputs\aar
folder.
B. Use the native plugin inside the Unity3D project
Create a new Unity3D project using Unity Hub.
Open your Unity3D project and create the folder structure: Assets / Plugins / Android
and Place the custom library (mylibrary-debug.aar
) inside the folder.
Native library integration
- Create a C# script to access the imported library from the app. For which, create a folder named
Scripts
. Add the script insideScripts
folder and name itNativeCodeRunner.cs
. Add the following code to it.
2. Create an Empty GameObject and add the NativeCodeRunner
script to it.
Setup the project
- Open project settings (File → Build settings) and switch the target platform to Android.
- Add the current scene to the build by clicking on the Add Open Scenes button.
- Provide a package name and change the Minimum target version to
Android 7.1 ‘Nougat’ (API level 25)
.
- The latest android version has some security restrictions to access the activity from another app. If the android version is greater than 30 then set
android:exported
to “true
" to allow other apps to access it. For which, enable the custom main Android manifest file from the Publishing settings under the Player settings.
- Open the
AndroidManifest.xml
file from thePlugins/Android
folder. Addandroid:exported=”true”
code in the Activity tag and remove the following comment.
<!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
The final AndroidManifest.xml
will look as the following,
Run the app
Connect the mobile device via USB cable and click on the Build and Run button to run the app on the target device. Ensure that USB debugging is enabled on your device.
There you have it! Your own android app with a custom native android library in Unity 3D :)
Thanks for reading this article.
Thanks Gowri M Bhatt for reviewing the content.
If you enjoyed this article, please click on the clap button 👏 and share to help others find it!
The article is also available on Dev.
The full source code for this tutorial can be found here,