'Unity android gradle build fails duplicate class

I have been looking for the last few days for a solution to my problem. I have multiple SDKs imported in my project (firebase, facebook, admob, etc) and it was all working fine.

But I then needed to import a AndroidInstallReferrer plugin (this one: https://assetstore.unity.com/packages/tools/integration/android-install-referrer-179422)

And after importing it (it consists of a .jar and an .aar basically) I can't build an APK anymore. This is the error I get:

Note: D:\Work\MyProject\Project\Temp\gradleOut\unityLibrary\src\main\java\com\unity3d\player\UnityPlayerActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':launcher:checkReleaseDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class com.android.installreferrer.api.InstallReferrerClient found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.api.InstallReferrerClient$1 found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.api.InstallReferrerClient$Builder found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.api.InstallReferrerClient$InstallReferrerResponse found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.api.InstallReferrerClientImpl found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.api.InstallReferrerClientImpl$1 found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.api.InstallReferrerClientImpl$ClientState found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.api.InstallReferrerClientImpl$InstallReferrerServiceConnection found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.api.InstallReferrerStateListener found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.api.ReferrerDetails found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.android.installreferrer.commons.InstallReferrerCommons found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.google.android.finsky.externalreferrer.IGetInstallReferrerService found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.google.android.finsky.externalreferrer.IGetInstallReferrerService$Stub found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  Duplicate class com.google.android.finsky.externalreferrer.IGetInstallReferrerService$Stub$Proxy found in modules jetified-installreferrer-1.0-runtime.jar (com.android.installreferrer:installreferrer:1.0) and jetified-installreferrer-2.1-runtime.jar (:installreferrer-2.1:)
  
  Go to the documentation to learn how to Fix dependency resolution errors.


* 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 59s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Any help is greatly appreciated. Also, the jar resolver doesn't work, jetifier and androidx are enabled in gradleTemplate; multidex is enabled in launcher template.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source