For example, i have LiveKit SDK for Android and i need to integrate this plugin into my Unity project for android devices.
I have searched for many tutorials, but none of them had good explanation how to implement this whole SDK to my project and properly use it
In addition, i just took livekit.aar file from .gradle folder and put it in unity project Then, I coded it like that
private void Start() {
AndroidJavaClass liveKit = new AndroidJavaClass("io.livekit.android.LiveKit");
devInf.text = "Found LiveKit Class";
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject applicationContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaObject room = liveKit.CallStatic<AndroidJavaObject>("create", applicationContext);
token = PlayerPrefs.GetString("stream-token", "");
Debug.LogWarning(token);
StartCoroutine(ConnectCoroutine(room, url, token));
//Invoke("CreateRoomWithDelay", 3f);
}
IEnumerator ConnectCoroutine(AndroidJavaObject room, string url, string token) {
devInf.text = "Starting Connection";
yield return room.Call<IEnumerator>("connect", url, token);
AndroidJavaObject localParticipant = room.Call<AndroidJavaObject>("localParticipant");
if (localParticipant != null) {
AndroidJavaObject audioTrack = localParticipant.Call<AndroidJavaObject>("createAudioTrack");
AndroidJavaObject videoTrack = localParticipant.Call<AndroidJavaObject>("createVideoTrack");
localParticipant.Call("publishAudioTrack", audioTrack);
localParticipant.Call("publishVideoTrack", videoTrack);
localParticipant.Call("setCameraEnabled", true);
localParticipant.Call("setMicrophoneEnabled", true);
}
}
this is how i realized livekit in android studio
fun startStream() {
val cfgData = getCfgData.getData()
viewModelScope.launch {
if (token != null) {
room.connect(STREAM_URL, token)
val localParticipant = room.localParticipant
val videoTrack = localParticipant.createVideoTrack(
"video",
LocalVideoTrackOptions(position = CameraPosition.BACK)
)
localParticipant.publishVideoTrack(videoTrack)
val audioTrack = localParticipant.createAudioTrack(
"audio",
LocalAudioTrackOptions()
)
localParticipant.publishAudioTrack(audioTrack)
localParticipant.setCameraEnabled(true)
localParticipant.setMicrophoneEnabled(true)
}
}
}
LiveKit has unity plugin so you shouldn't need to install Android SDK separately.
But if you want to know how to add android libraries to the Unity, here are the steps:
<?xml version="1.0" encoding="UTF-8" ?>
<dependencies>
<androidPackages>
<repositories>
<repository>https://repo1.maven.org/maven2</repository>
<repository>https://jitpack.io</repository>
</repositories>
<androidPackage spec="io.livekit:livekit-android:2.0.1"/>
</androidPackages>
</dependencies>
Custom Main Gradle Template
and Custom Gradle Properties Templete
in Edit > Project Setting > Player > Android > Publishing Settings
. When templates are enabled, External Dependency Manager will use Gradle to resolve dependencies, which works a lot better than Dependency manager's own implementation.Assets > External Dependency Manager > Android Resolver > Resolve
Publishing Settings
and are working with added Android library from C#, you may face class not found/defined errors. In that case you will need to enable Custom Proguard File
and add:
-keep class io.livekit.** { public *; }
Copy .aar file to the Assets/Plugins/Android
directory
If needed, add custom Proguard rules to avoid code obfuscation.
Note that many Android libraries have transitive dependencies, which means that also dependencies' aar files needs to be added.