Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
using GooglePlayServices;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
Expand All @@ -38,13 +39,22 @@ public class AndroidBuildPreProcessor : IPreprocessBuildWithReport
const string CustomMainGradleTemplateFileName = "mainTemplate.gradle";
const string JetifierEntry =
"android.jetifier.ignorelist=annotation-experimental-1.4.0.aar";
const string PlayServicesAdsSDK = "com.google.android.gms:play-services-ads:24.9.0";
const string NextGenSDK =
"com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk:0.23.0-beta01";
const string NextGenSDKRegex =
"com\\.google\\.android\\.libraries\\.ads\\.mobile\\.sdk:ads-mobile-sdk:[^\"]*";
const string PlayServicesAdsSDKRegex =
"com\\.google\\.android\\.gms:play-services-ads:[^\"]*";

// Set the callback order to be before EDM4U.
// https://github.com/googlesamples/unity-jar-resolver/blob/master/source/AndroidResolver/src/PlayServicesPreBuild.cs#L39
public int callbackOrder { get { return -1; } }

public void OnPreprocessBuild(BuildReport report)
{
UpdateSDKDependencies();

if(!GoogleMobileAdsSettings.LoadInstance().EnableGradleBuildPreProcessor)
{
return;
Expand All @@ -55,6 +65,47 @@ public void OnPreprocessBuild(BuildReport report)
#endif
}

private void UpdateSDKDependencies()
{
// Update GoogleMobileAdsDependencies.xml if Next Gen SDK is enabled.
string dependenciesDir =
Path.Combine(Application.dataPath, "GoogleMobileAds", "Editor");
string projectPath = Directory.GetParent(Application.dataPath).ToString();
string[] scripts = Directory.GetFiles(projectPath, "AndroidBuildPreProcessor.cs",
SearchOption.AllDirectories);
if (scripts.Length > 0)
{
dependenciesDir = Path.GetDirectoryName(scripts[0]);
}
string dependenciesFilePath =
Path.Combine(dependenciesDir, "GoogleMobileAdsDependencies.xml");
string content = File.ReadAllText(dependenciesFilePath);

if (File.Exists(dependenciesFilePath))
{
if (GoogleMobileAdsSettings.LoadInstance().EnableNextGenSDK &&
Regex.IsMatch(content, PlayServicesAdsSDKRegex))
{
string newContent = Regex.Replace(content, PlayServicesAdsSDKRegex, NextGenSDK);
if (content != newContent)
{
File.WriteAllText(dependenciesFilePath, newContent);
}
}
else if (!GoogleMobileAdsSettings.LoadInstance().EnableNextGenSDK &&
Regex.IsMatch(content, NextGenSDKRegex))
{
// Reverting from Next Gen SDK to Play Services Ads SDK.
string newContent = Regex.Replace(content, NextGenSDKRegex, PlayServicesAdsSDK);
if (content != newContent)
{
File.WriteAllText(dependenciesFilePath, newContent);
}
}
PlayServicesResolver.ResolveSync(true);
}
}

private void ApplyBuildSettings(BuildReport report)
{
Debug.Log("Running Android Gradle Build Pre-Processor.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ internal static GoogleMobileAdsSettings LoadInstance()
[SerializeField]
private bool disableOptimizeAdLoading;

[SerializeField]
private bool enableNextGenSDK = false;

[SerializeField]
private string userTrackingUsageDescription;

Expand Down Expand Up @@ -98,6 +101,13 @@ public bool DisableOptimizeAdLoading
set { disableOptimizeAdLoading = value; }
}

public bool EnableNextGenSDK
{
get { return enableNextGenSDK; }

set { enableNextGenSDK = value; }
}

public string UserTrackingUsageDescription
{
get { return userTrackingUsageDescription; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class GoogleMobileAdsSettingsEditor : UnityEditor.Editor
SerializedProperty _enableKotlinXCoroutinesPackagingOption;
SerializedProperty _disableOptimizeInitialization;
SerializedProperty _disableOptimizeAdLoading;
SerializedProperty _enableNextGenSDK;
SerializedProperty _userLanguage;
SerializedProperty _userTrackingUsageDescription;

Expand All @@ -44,6 +45,7 @@ public void OnEnable()
serializedObject.FindProperty("enableKotlinXCoroutinesPackagingOption");
_disableOptimizeInitialization = serializedObject.FindProperty("disableOptimizeInitialization");
_disableOptimizeAdLoading = serializedObject.FindProperty("disableOptimizeAdLoading");
_enableNextGenSDK = serializedObject.FindProperty("enableNextGenSDK");
_userLanguage = serializedObject.FindProperty("userLanguage");
_userTrackingUsageDescription =
serializedObject.FindProperty("userTrackingUsageDescription");
Expand Down Expand Up @@ -140,6 +142,17 @@ public override void OnInspectorGUI()
MessageType.Info);
}


EditorGUILayout.PropertyField(
_enableNextGenSDK,
new GUIContent(localization.ForKey("ENABLE_NEXT_GEN_SDK")));

if (settings.EnableNextGenSDK)
{
EditorGUILayout.HelpBox(localization.ForKey("ENABLE_NEXT_GEN_SDK_HELPBOX"),
MessageType.Info);
}

EditorGUI.indentLevel--;
EditorGUILayout.Separator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
"en": "Disable initialization optimization",
"fr": "Désactiver l'optimisation de l'initialisation"
},
"KEY_ENABLE_NEXT_GEN_SDK": {
"en": "Enable Next Gen SDK",
"fr": "Activer le SDK de nouvelle génération"
},
"KEY_ENABLE_NEXT_GEN_SDK_HELPBOX": {
"en": "Utilizes the Google Mobile Ads Next Gen SDK. Please run Clean & Build after enabling or disabling this option for the first time.",
"fr": "Utilise le SDK de nouvelle génération Google Mobile Ads. Veuillez effectuer un nettoyage et une nouvelle compilation après avoir activé ou désactivé cette option pour la première fois."
},
"KEY_UMP_SPECIFIC_SETTINGS_LABEL": {
"en": "UMP-specific settings",
"fr": "Paramètres spécifiques UMP"
Expand Down