Usercentrics Unity SDK - 2.36.0

The purpose of the Usercentrics Unity SDK is to provide a lightweight and flexible library for the integration of a Consent Management Platform (CMP) into native mobile applications, to enable the collection, storage and management of your user's consent, in order to ensure legal compliance for different legal frameworks.

- Getting Started
Follow our Documentation (https://usercentrics.com/docs/install) to get started with your CMP integration.

- Getting an account
You will need an account to configure and initialize the SDK. See Pricing Packages (https://usercentrics.com/pricing/unity).

- Moving between the .unitypackage and the UPM package
See Migrating to the Unity Package Manager (https://usercentrics.com/docs/unity-migrate-to-upm).
It covers migrating either way, switching between the normal and the static package, installing without a
registry, and the failures that are easy to miss because nothing is logged.


---

Managed Code Stripping
-----------------------

The SDK is compatible with all Unity managed code stripping levels: Minimal, Low, Medium, and High.

A link.xml file is included and preserves the Unity.Usercentrics assembly from IL2CPP/Mono
stripping automatically — no configuration is required, installing the SDK is enough. Where it
lands depends on how you installed:
- .unitypackage:  Assets/Usercentrics/link.xml
- UPM package:    Packages/com.usercentrics.sdk/link.xml
                  Packages/com.usercentrics.sdk.static/link.xml for the static variant

If your project has its own link.xml, Unity merges multiple link.xml files automatically.


---

External Dependency Manager
---------------------------

The UPM packages (com.usercentrics.sdk and com.usercentrics.sdk.static) do not include the External Dependency
Manager for Unity (EDM4U). EDM4U resolves the native Android (Gradle) and iOS (CocoaPods or Swift Package Manager)
dependencies listed in Editor/UsercentricsDependencies.xml, so your project needs its own copy.
The .unitypackage files already contain it.

On Unity 2019.1 and newer, install it from OpenUPM (scoped registry https://package.openupm.com, scope
com.google.external-dependency-manager). Unity 2018.4 has no scoped registries: import the .unitypackage from
https://github.com/googlesamples/unity-jar-resolver instead.

If it is missing, an Android or iOS build logs a warning in the Console that names the missing resolver
(Google.JarResolver or Google.IOSResolver). The build still succeeds, but the app fails at runtime (on Android with
a ClassNotFoundException for com.usercentrics.sdk.unity.UsercentricsUnity). The warning is a false alarm if you add
the native dependencies yourself.

The warning only detects a missing External Dependency Manager. A resolver that was just imported and not yet enabled by
its Version Handler counts as missing, so let the import finish before you build. A misconfigured one is not detected,
for example EDM4U 1.2.120 with "Patch mainTemplate.gradle" off (see below).

Tested with EDM4U 1.2.187. EDM4U 1.2.120 also works with these settings:
- Assets > External Dependency Manager > Android Resolver > Settings: tick "Patch mainTemplate.gradle" (it is off by
  default in 1.2.120) and use a custom main Gradle template. Without it the resolver copies about 70 libraries into
  Assets and Gradle fails on a duplicate Kotlin module.
- Run Unity with a JDK 11 as JAVA_HOME: the 1.2.120 resolver does not work with JDK 21.

The first "Force Resolve" in a project that does not have an Assets/Plugins/Android folder yet fails and
resolves nothing:

    Unable to copy 'mainTemplate.gradle' from Unity engine folder '...' to this project folder
    'Assets/Plugins/Android'.
    System.IO.DirectoryNotFoundException: .../Assets/Plugins/Android/mainTemplate.gradle
      at GooglePlayServices.GradleTemplateResolver.EnsureGradleTemplateEnabled (...)
    Resolution Failed.

EDM4U wants to enable the custom main Gradle template but does not create the folder it goes in. It does create
the folder on the way out, so running Force Resolve a second time succeeds and the Usercentrics dependencies are
written into Assets/Plugins/Android/mainTemplate.gradle. Seen with EDM4U 1.2.187 on Unity 6000.3 at its own
default settings, in a project created from scratch — which a UPM install often is. This is an EDM4U issue, not a
Usercentrics one; run Force Resolve again and continue.

iOS on Unity older than 2021.3 (2018.4 to 2020.3): Swift Package Manager is not supported there, but EDM4U 1.2.187
still drops the CocoaPods entry for UsercentricsUI while it is enabled (the default), so the iOS project ends up
without UsercentricsUI. Turn off Swift Package Manager in Assets > External Dependency Manager > iOS Resolver >
Settings and the pod is added again.

On Unity 2021.3 and newer a new project uses Swift Package Manager by default. The generated Podfile then has no
UsercentricsUI entry, which is expected: the package is added to the Xcode project instead.


---

Static linkage package and Unity versions
-----------------------------------------

The normal package supports Unity 2018.4 and newer.

The static linkage package (com.usercentrics.sdk.static, or the .unitypackage with "static" in its name) needs
Unity 2019.3 or newer for iOS builds. It uses the UnityFramework target, which Unity only exports from 2019.3, so on
older versions the iOS build fails in Xcode. Use the normal package there. Android builds are not affected.

Package Manager does not block a tarball install on an older Unity version. The static package logs a warning
in the Console instead.


---

Testing Consent Mediation (Firebase)
--------------------------------------

The sample app supports a Firebase Analytics integration to test the consent mediation workflow
on both iOS and Android.

Prerequisites:
- iOS: add GoogleService-Info.plist at Assets/GoogleService-Info.plist
- Android: add google-services.json at Assets/google-services.json

Steps:

1. The demo is part of the sample project in the SDK repository
   (SampleProject-2019.4/Assets/Usercentrics/Demo/DemoScript.cs); neither the .unitypackage
   nor the UPM packages ship it. Open it there and ensure the flag is set:
      private const bool CONSENT_MEDIATION_ENABLED = true;

2. Build and run the sample app on iOS or Android.

3. Tap "Show First Layer" — the Usercentrics consent banner will appear.

4. Accept or deny consent.

5. Check the device logs (Logcat on Android / Xcode console on iOS) for:
      [UC_DEMO] OnConsentMediation fired. sdks=<count>
      [UC_DEMO]   SDK: <name> consent=<true/false> mediated=<true/false>

6. Verify that Firebase Analytics appears in the mediated SDK list with the expected consent value.

Notes:
- To disable mediation testing and revert to the standard flow, set CONSENT_MEDIATION_ENABLED = false.


---

Android build requirements on Unity 2018.4 and 2019.4
-----------------------------------------------------

Since 2.21.1 the Usercentrics Android library is compiled for Java 11 (it was Java 8 before).
The Gradle toolchain bundled with Unity 2018.4 and 2019.4 (Android Gradle Plugin 3.4.0,
Gradle 5.1.1, JDK 8) cannot build it, and the Android build fails in Gradle.
You need Android Gradle Plugin 4.2.2, Gradle 6.7.1, a JDK 11 for Gradle, AndroidX and no Jetifier,
plus the Android SDK platform 33 or higher installed. This is the compileSdk the Android libraries of SDK 2.33.1 require (their AndroidX dependencies state it; the build stops in Gradle with "minCompileSdk (33) ... is greater than this module's compileSdkVersion" otherwise) and may rise in later versions. They also need minSdk 21 (the Unity 2018.4 to 2020.3 default is 19, raise it in Player Settings).

Unity 2019.4
- baseProjectTemplate.gradle (custom template): set `classpath 'com.android.tools.build:gradle:4.2.2'`
- gradleTemplate.properties (custom template): add `android.useAndroidX=true` and `org.gradle.java.home=<path to a JDK 11>`
- Edit > Preferences > External Tools: uncheck "Gradle Installed with Unity (recommended)" and select Gradle 6.7.1.
  Leave the JDK on the one installed with Unity: Unity 2019.4 only accepts JDK 8 there, and Gradle uses the JDK 11 from `org.gradle.java.home`
- Assets > External Dependency Manager > Android Resolver > Settings: uncheck "Use Jetifier"
- Player Settings > Other Settings > Target API Level: Automatic (highest installed, with platform 33 or higher installed), or a fixed 33 or higher

Unity 2018.4
The two templates above do not exist in 2018.4, so the build has to leave the Editor:
- Assets > External Dependency Manager > Android Resolver > Settings: uncheck "Use Jetifier"
- Player Settings > Other Settings > Target API Level: a fixed value below 31, for example 29 (not Automatic, which picks the highest
  installed platform). The 2018.4 manifest does not declare `android:exported`, which Android 12 and higher require.
  Set `compileSdkVersion` to 33 or higher in your custom mainTemplate.gradle
- Build Settings > Android: tick "Export Project" and export
- In the exported project: change the AGP classpath in build.gradle to `com.android.tools.build:gradle:4.2.2`
  and add `android.useAndroidX=true` to gradle.properties
- Build the exported project with Gradle 6.7.1 on JDK 11 (`gradle assembleRelease`, or Android Studio)

Verified with the Usercentrics sample: on Unity 2019.4.41f2 the Editor build, run on an arm64 Android 14 emulator (the first layer shows),
and on Unity 2018.4.36f1 the exported project built with Gradle (not run on a device).
Unity 6000.3 builds with its default toolchain. On Unity 2020.3 an IL2CPP build with the External Dependency Manager works with a fixed Target API Level (Automatic can pick a level whose aapt2 fails) and minSdk 21; the first layer shows on an arm64 Android 14 emulator.
