How do I import "com.unity3d.player.UnityPlayer" into my Android Library?

Issue

I’m having issues getting the UnityPlayer dependencies into my Android Library plugin for Unity.

enter image description here

I just can’t figure out the correct way to add dependencies to my plugin.

Before I was trying to add a classes.jar file to my libs directory that contains all the UnityPlayer code, and that got me to the point where I can write code without intellisense errors, but when I build and run I get this massive output.

CommandInvokationFailure: Gradle build failed. 
C:\Program Files\Unity\Hub\Editor\2018.4.14f1\Editor\Data\PlaybackEngines\AndroidPlayer/Tools\OpenJDK\Windows\bin\java.exe -classpath "C:\Program Files\Unity\Hub\Editor\2018.4.14f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-5.1.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"

stderr[

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':checkReleaseDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class bitter.jnibridge.JNIBridge found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class bitter.jnibridge.JNIBridge$a found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.NativeLoader found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.ReflectionHelper found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.ReflectionHelper$1 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.ReflectionHelper$a found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$1 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$10 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$11 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$12 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$13 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$14 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$15 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$16 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$17 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$18 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$19 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$2 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$3 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$4 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$5 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$6 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$7 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$8 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$9 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$a found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$b found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$c found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayerActivity found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a$1 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a$2 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a$a found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.b found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.c found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.d found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.e found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.f found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.g found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.h found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.i found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.j found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.k found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.k$1 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.l found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.m found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.n found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.o found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.p found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$1 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$2 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$3 found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class org.fmod.FMODAudioDevice found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)
  Duplicate class org.fmod.a found in modules classes-2.jar (:coffeeplugin-release:) and unity-classes.jar (unity-classes.jar)

  Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 8s
]
stdout[
> Task :preBuild UP-TO-DATE
> Task :preReleaseBuild UP-TO-DATE
> Task :compileReleaseAidl NO-SOURCE
> Task :compileReleaseRenderscript NO-SOURCE
> Task :checkReleaseManifest UP-TO-DATE
> Task :generateReleaseBuildConfig UP-TO-DATE
> Task :prepareLintJar UP-TO-DATE
> Task :generateReleaseSources UP-TO-DATE
> Task :javaPreCompileRelease
> Task :mainApkListPersistenceRelease UP-TO-DATE
> Task :generateReleaseResValues UP-TO-DATE
> Task :generateReleaseResources UP-TO-DATE
> Task :mergeReleaseResources UP-TO-DATE
> Task :createReleaseCompatibleScreenManifests UP-TO-DATE
> Task :processReleaseManifest
> Task :processReleaseResources
> Task :compileReleaseJavaWithJavac
> Task :compileReleaseSources
> Task :lintVitalRelease
> Task :mergeReleaseShaders UP-TO-DATE
> Task :compileReleaseShaders UP-TO-DATE
> Task :generateReleaseAssets UP-TO-DATE
> Task :mergeReleaseAssets
> Task :validateSigningRelease UP-TO-DATE
> Task :signingConfigWriterRelease UP-TO-DATE
> Task :checkReleaseDuplicateClasses FAILED

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings
19 actionable tasks: 7 executed, 12 up-to-date
]
exit code: 1
UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <f92457847c324e90907abe57dde6951d>:0)
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <f92457847c324e90907abe57dde6951d>:0)
UnityEditor.Android.Command.Run (System.String command, System.String args, System.String workingdir, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <f92457847c324e90907abe57dde6951d>:0)
UnityEditor.Android.AndroidJavaTools.RunJava (System.String args, System.String workingdir, System.Action`1[T] progress, System.String error) (at <f92457847c324e90907abe57dde6951d>:0)
UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <f92457847c324e90907abe57dde6951d>:0)
Rethrow as GradleInvokationException: Gradle build failed
UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <f92457847c324e90907abe57dde6951d>:0)
UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <f92457847c324e90907abe57dde6951d>:0)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <f92457847c324e90907abe57dde6951d>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)

I’m seeing mostly duplicate errors, so I’m guessing I already have everything in the classes.jar in my Android Studio project. I built the project from an exported project from Unity and opened it in Android Studio then made the "coffeeplugin" module.

When I try to add com.unity3d.player.UnityPlayer in the Gradle build script, Android Studio tells me its an invalid string when I sync Gradle.

enter image description here

I understand it’s wanting :SomeTag I’m guessing, but I don’t know what to put there.
The Unity documentation doesn’t really get into this part (at least from what documentation and blogs I’m able to find).

coffeeplugin build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"


    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.unity3d.player'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

task copyPlugin(type: Copy) {
    dependsOn assemble
    from ('build/output/aar')
    into ('N:/Storage/UnityProjects/Coffee/Assets/Plugins/Android')
    include (project.name + "-release.aar")
}

Solution

I figured out exactly what is contained in these AAR files I’ve been importing in to Unity.
I have been doing the correct thing by including classes.jar from Unity into my plugin/libs. The problem was that by building my plugin in Android Studio with classes.jar in libs, classes.jar was being included in the final AAR output, which on the Unity side of things is redundant because Unity already has this data, thus the duplicate classes error I’ve been getting.

The fix was to open the AAR is some archive editor (7-zip or winrar) and extract only the clases.jar within that and import only that into Unity.

enter image description here

I’m surprised there’s not more official documentation on this process.

Also, this fix means no edits to the Gradle build script is necessary unless you want to automate the copy process from intermediate builds to Unity.

Answered By – GhostRavenstorm

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published