Unity Native: Android Library Integration Methods
By Artem Glotov
Many Unity developers face the necessity of writing platform-specific code and integrating it with their Unity project.
Unity provides three distinct methods for incorporating Android dependencies into applications. Let’s evaluate and rank these methods from most to least recommended, outlining their advantages, disadvantages, and usage scenarios.
1. Compiled Library from Maven Repository
In this scenario, Android libraries (.aar
or .jar
) must be published to a Maven repository with the corresponding .pom
file describing their dependencies.
You have two options for describing the dependency on a published Android library in a Unity project:
a. Dependencies file
Add a *Dependencies.xml
file to the Unity package in the Editor
folder. External Dependency Manager for Unity (EDM4U) resolves conflicts and adds dependencies to the project’s Gradle file (mainTemplate.gradle
) before the build process.
<?xml version="1.0" encoding="utf-8"?>
<dependencies>
<androidPackages>
<androidPackage spec="androidx.appcompat:appcompat:1.6.1" />
</androidPackages>
</dependencies>
Most well-known plugins like Facebook, AppsFlyer and Firebase already use EDM4U for dependency management (their packages contain*Dependencies.xml
files).
b. Gradle File
Add a dependency directly to the mainTemplate.gradle
file. This approach is suitable only for project dependencies.
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
}
During the build, Gradle resolves transitive dependencies and conflicts.
Pros:
- Android libraries have their own versioning, independent of the Unity package version
- The Android library can be developed and published independently of the Unity package
- Supports dependency and version resolution
- Reduces Unity package size (no need to include android libraries in the package; they will be downloaded only during the build)
Cons:
- Requires a separate repository and infrastructure for Android libraries.
When to Use:
- In third-party Unity packages
- When you want to share a library between multiple Unity packages
- When you want to share a library between multiple engines
2. Android Library Project
An Android Library* is a special type of Android project that can be imported into a Unity project. It represents a folder with a .androidlib
extension and an Android module structure, containing source code (java
or kt
files) or resources required for the native part of an application. Unity includes the Android Library as a submodule in the final Android project.
It can contain a Gradle configuration file (build.gradle
) with other Android dependencies, which will also be resolved during the build.
Pros:
- Android library sources are located near the Unity project sources, which is convenient for development
- Supports dependency and version resolution
Cons:
- The Android library will not have its own versioning; it will be versioned with the Unity package
- Additional configuration is needed to work with Android Studio
When to use:
- In studio Unity packages or projects
- In shared Unity packages, if you are sure that the Android library will not be used by other packages
You can find more information on how to develop and maintain an Android Library project here.
3. Compiled Android Library as a File
Compiled libraries*, represented as .aar
or .jar
files, are placed in the Assets
folder. Unity includes these files as dependencies in the build.gradle
file of the final Android project.
Cons:
- Compiled libraries (
.aar
or.jar
) do not support dependency management. Any transitive dependency must be added to the Unity project as an.aar
or.jar
file or specified in themainTemplate.gradle
file - They don’t support dependency and version resolution. Any conflicts between versions must be resolved manually
- It is hard to support versioning of compiled libraries. You must manually check for updates and update them in your project
- They have poor compatibility with other solutions and can lead to conflicts
When to use:
- Never
- In small personal projects, plugins or test samples
Summary
Now you know how to deliver Android dependencies to your Unity project and which method is the best for specific cases.
(*) Content in this article is sourced from Unity’s Documentation. Copyright © 2024 Unity Technologies. Publication Date: 2024-02-05.