You can create an Android Library plug-inA set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary in Unity. Once created, you can directly use it within your Unity project or develop it further in Android Studio.
To create an Android Library plug-in, follow these steps:
In your Unity project, create MyFeature.androidlib
folder. This folder will represent a GradleAn Android build system that automates several build processes. This automation means that many common build errors are less likely to occur. More info
See in Glossary module.
Create a file named build.gradle
inside the MyFeature.androidlib
folder and add the following code:
apply plugin: 'com.android.library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
android {
namespace "com.company.myfeature"
compileSdk getProperty("unity.compileSdkVersion") as int
buildToolsVersion = getProperty("unity.buildToolsVersion")
compileOptions {
sourceCompatibility JavaVersion.valueOf(getProperty("unity.javaCompatabilityVersion"))
targetCompatibility JavaVersion.valueOf(getProperty("unity.javaCompatabilityVersion"))
}
defaultConfig {
minSdk getProperty("unity.minSdkVersion") as int
targetSdk getProperty("unity.targetSdkVersion") as int
ndk {
abiFilters.addAll(getProperty("unity.abiFilters").tokenize(','))
debugSymbolLevel getProperty("unity.debugSymbolLevel")
}
versionCode getProperty("unity.versionCode") as int
versionName getProperty("unity.versionName")
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile ('proguard-android-optimize.txt'), 'proguard-rules. pro'
}
}
}
MyFeature.androidlib
folder:
MyFeature.androidlib/src
MyFeature.androidlib/src/main
Create an AndroidManifest.xml
file inside the MyFeature.androidlib/src/main
folder and add the following code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
Create a file named Controller.java
inside the MyFeature.androidlib/src/main/java/com/company/feature/
folder and add the following code:
package com.company.feature;
public class Controller
{
public static String getFoo()
{
return "This is my feature";
}
}
The Android Library plug-in is now created.
To use the Android Library plug-in within your Unity project, use the following code:
using UnityEngine;
public class FeatureUser : MonoBehaviour
{
readonly string ControllerName = "com.company.feature.Controller";
AndroidJavaClass m_Class;
void Start()
{
m_Class = new AndroidJavaClass(ControllerName);
}
private void OnGUI()
{
GUILayout.Space(100);
GUI.skin.label.fontSize = 30;
if (m_Class != null)
{
GUILayout.Label($"{ControllerName}.getFoo() returns " + m_Class.CallStatic<string>("getFoo"));
}
else
{
GUILayout.Label($"{ControllerName} was not found?");
}
}
}
As the source files of the Android Library plug-ins aren’t visible in the Unity C# project, modifying or further developing the plug-in requires additional steps as follows:
You can develop your Android Library plug-in from Android Studio. As the Unity project directly references the plug-in, any modifications to the Android Library plug-in automatically reflect in the Unity project.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.