Skip to content

Include Android

Denis edited this page May 25, 2021 · 31 revisions

⚡ Before you start
Make sure you have correctly setup project settings.


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:

  1. Go to: Player Settings > Publishing Settings > Build
  2. Select Custom Main Gradle Template checkmark to configure dependencies.

For Unity 2019.3+:

  1. Select Custom Launcher Gradle Template checkmark to enable MultiDEX.
    You can read more about MuliDex on the Android Deleveloper page.
  2. Select Custom Base Gradle Template checkmark 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.
  3. Select Custom Gradle Properties Template to use Jetfier by EDM4U.

image

Configure EDM4U

  1. Go to: Assets > External Dependency Manager > Android > Settings
  2. Select Patch mainTemplate.gradle checkmark
  3. Select Use Jetfier checkmark
  4. 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.

AndroidManifest template

If you do not find the manifest file Plugins/Android/AndroidManifest.xml, you can take it from the example.
Or Unity 2019.3+ makes it possible to activate in Player Settings > Publishing Settings > Build > Custom Main Manifest checkmark.

Update AndroidManifest permissions

Add the following permissions to your Assets/Plugins/Android/AndroidManifest.xml file inside the <manifest> tag but outside the <application> tag:

<manifest>
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>

Optional permissions

VIBRATE permission is used for certain ads that vibrate during play. This is a normal level permission, so this permission just needs to be defined in the manifest to enable this ad feature.

<manifest>
 <uses-permission android:name="android.permission.VIBRATE" />
</manifest>

ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions is not a mandatory permission, however, including it will enable accurate ad targeting.

<manifest>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <!-- OR -->
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

WRITE_EXTERNAL_STORAGE permission is used for certain ads that allow the user to save a screenshot to their phone. Note that with this permission on devices running Android 6.0 (API 23) or higher, this permission must be requested from the user. See Requesting Permissions for more details.

<manifest>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

Included permissions in SDK

Sometimes permissions are included in the partner SDK’s internal AndroidManifest.
To remove a specific embedded permission (for example, ACCESS_COARSE_LOCATION), add the following tag to your top level AndroidManifest during integration:

<manifest>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="remove"/>
</manifest>

Network security configuration

Android 9.0 (API 28) blocks cleartext (non-HTTPS) traffic by default, which can prevent ads from serving correctly.
Read more about Network security configuration

  1. Create Plugins/Android/res/xml/network_security_config.xml file, add base-config tag with sets cleartextTrafficPermitted attribute to true:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
            <certificates src="user" />
        </trust-anchors>
    </base-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">127.0.0.1</domain>
    </domain-config>
</network-security-config>
  1. Add the network_security_config.xml file to your Assets/Plugins/Android/AndroidManifest.xml:
<manifest>
    <application 
		...
        android:networkSecurityConfig="@xml/network_security_config">
    </application>
</manifest>

What’s Next?

Clone this wiki locally