Version: 2022.3
Language : English
Call native plug-in for Android code
Calling Java and Kotlin plug-in code from C# scripts

Java and Kotlin source plug-ins

Unity can interpret individual Java and Kotlin source files as individual plug-insA 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
.

Unity supports Java and Kotlin code written in source files with .java and .kt extensions. To do this, Unity interprets each source file as an individual plug-in and compiles them when it builds the Player. This type of plug-in is useful if you need to write a small amount of code for a single project. If you plan to reuse the code in multiple projects or distribute it to other people, then an Android Library Project or Android Archive plug-in might be more appropriate.

Create a Java or Kotlin source plug-in

To indicate to Unity to create a plug-in from a Java (.java) or Kotlin (.kt) source file, follow these steps:

  1. In the Assets folder, place your Java (.java) or Kotlin (.kt) source file.
    Tip: It’s best practice to create a subfolder to contain your Java and Kotlin source files.
  2. Select the source file and view it in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
    See in Glossary
    window.
  3. In the Inspector window, under the Select Platforms for plugin section, enable Android.
  4. Select Apply.

Note: You can place the source files in any folder in your project, except in special use locations such as StreamingAssets. If you place files in these locations, the Unity Editor doesn’t display the Plugin Inspector.

Example: Create and use Kotlin source plug-in

The following example demonstrates how to create a Kotlin source plug-in and access its code from a C# script. This allows you to use Android-specific functionality in your Unity project.

  1. In the Assets folder of your project, create a subfolder named AndroidPlugins, and add a Kotlin (.kt) file to it with the following code.

      object KotlinStringHelper {
      @JvmStatic
      fun getString(): String {
         return "Hello from Kotlin"
      }
      }
    
  2. Select the Kotlin file. In the Inspector window, under the Select Platforms for plugin section, enable the Android platform.

  3. Select Apply.

  4. In Unity, create a C# script and add the following code. This code uses AndroidJavaClass to access the static Kotlin method getString.

    
      using UnityEngine;
    
      public class KotlinExamples : MonoBehaviour
      {
         void Start()
         {
            using (AndroidJavaClass cls = new AndroidJavaClass("KotlinStringHelper"))
            {
               string value = cls.CallStatic<string>("getString");
               Debug.Log($"KotlinStringHelper.getString returns {value}");
            }
         }
      }
    
  5. Attach the C# Script to a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
    See in Glossary
    . For more information, refer to the documentation on Controlling a GameObject.

Edit Java or Kotlin files in an exported Android Studio project

By default, when you export a Unity project for Android, Unity copies any Java/Kotlin files over to the Android Studio project. If you edit these files in Android Studio, the changes aren’t reflected in the original files in the Unity project. If you export the Unity project again, the export process overwrites your changes in Android Studio.

To resolve this, Unity provides the Symlink Sources build setting. If you select this build setting, Unity creates a symbolic link in the Android Studio project to Java/Kotlin files in the Unity project, instead of copying files over. This means that if you edit the files in Android Studio, the changes will reflect in the original Unity project files.

Additional resources

Call native plug-in for Android code
Calling Java and Kotlin plug-in code from C# scripts