Include Android - cleveradssolutions/CAS-Unity GitHub Wiki

⚡ Before you start
Make sure you have correctly Configuring SDK.


The Clever Ads Solutions Unity plugin is distributed with the EDM4U library. This library is intended for use by any Unity plugin that requires access to Android specific libraries (e.g., AARs) or iOS CocoaPods. It provides Unity plugins the ability to declare dependencies, which are then automatically resolved and copied into your Unity project.

Configure Gradle

Using EDM4U, you will be able to avoid downloading the Android artifacts into your project. Instead, the artifacts will be added to your gradle file during the compilation. To enable this process, follow these steps:

  • Go to: Player Settings > Publishing Settings > Build
  • Custom Main Gradle Template to configure third party dependencies.
  • Custom Base Gradle Template to update Gradle plugin with fix support Android 11.
    You can read more about fix Gradle plugin with support Android 11 on the Android Deleveloper page.
  • Custom Gradle Properties Template to use Jetfier by EDM4U.
  • Custom Main Manifest to modify application manifest.
  • Unity 2022.2+
  • Custom Gradle Settings Template to include maven repositories
    Support Android Gradle Plugin version 7.1.0+

image

Configure EDM4U

  • Go to: Assets > External Dependency Manager > Android > Settings
  • Patch mainTemplate.gradle
    The mainTemplate.gradle file will be patched with dependencies managed by the Android Resolver.
  • Use Jetfier
    Legacy Android support libraries and references to them from other libraries will be rewritten to use Jetpack using the Jetifier tool.
  • Patch gradleTemplate.properties
    For Unity 2019.3 and above, it is recommended to enable Jetfier and AndroidX via gradleTemplate.properties. This has no effect in older versions of Unity.
  • Save your changes, by pressing OK

In the Unity editor, select Assets > External Dependency Manager > Android Resolver > Resolve. The Unity External Dependency Manager library will append dependencies to mainTemplate.gradle of your Unity app.

Android 12 Support

If your app targets Android 12, then you must explicitly declare the android:exported attribute. Add the android:exported="true" attribute to the <activity> tag to Assets/Plugins/Android/AndroidManifest.xml:

<application ...>
  <activity android:name="com.unity3d.player.UnityPlayerActivity"
            android:theme="@style/UnityThemeSelector"
            android:exported="true">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
     <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
   </activity>
</application>

Execute events on Unity Thread

Advertising events for the Android runtime are queued to be called during the next Update() cycle. This behavior is safe for using UnityEngine components in advertising events. However, because Unity does not call Update while displaying fullscreen ads, all ad events will only be triggered after closing the fullscreen ad and returning to the Unity game.

If you want to receive callbacks from the ad without waiting for the ad to close, you can disable the isExecuteEventsOnUnityThread property in the settings. Enabled by default.

CAS.MobileAds.settings.isExecuteEventsOnUnityThread = false;

You will also need to use CAS.EventExecutor.Add(Action) to access Unity Engine components, where Action will be executed during the next Update() cycle.

Note

isExecuteEventsOnUnityThread property used Android runtime only.


🔗 Done! What’s Next?

⚠️ **GitHub.com Fallback** ⚠️