Skip to content

Commit 082eaeb

Browse files
Mobile Ads Developer Relationscopybara-github
authored andcommitted
Added Editor settings to toggle Next Gen SDK usage
PiperOrigin-RevId: 862912818
1 parent 2e4b25f commit 082eaeb

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

source/plugin/Assets/GoogleMobileAds/Editor/AndroidBuildPreProcessor.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using UnityEngine;
1414
using UnityEditor;
1515
using System.IO;
16+
using System.Text.RegularExpressions;
1617
using GooglePlayServices;
1718
using UnityEditor.Build;
1819
using UnityEditor.Build.Reporting;
@@ -38,13 +39,22 @@ public class AndroidBuildPreProcessor : IPreprocessBuildWithReport
3839
const string CustomMainGradleTemplateFileName = "mainTemplate.gradle";
3940
const string JetifierEntry =
4041
"android.jetifier.ignorelist=annotation-experimental-1.4.0.aar";
42+
const string PlayServicesAdsSDK = "com.google.android.gms:play-services-ads:24.9.0";
43+
const string NextGenSDK =
44+
"com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk:0.23.0-beta01";
45+
const string NextGenSDKRegex =
46+
"com\\.google\\.android\\.libraries\\.ads\\.mobile\\.sdk:ads-mobile-sdk:[^\"]*";
47+
const string PlayServicesAdsSDKRegex =
48+
"com\\.google\\.android\\.gms:play-services-ads:[^\"]*";
4149

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

4654
public void OnPreprocessBuild(BuildReport report)
4755
{
56+
UpdateSDKDependencies();
57+
4858
if(!GoogleMobileAdsSettings.LoadInstance().EnableGradleBuildPreProcessor)
4959
{
5060
return;
@@ -55,6 +65,47 @@ public void OnPreprocessBuild(BuildReport report)
5565
#endif
5666
}
5767

68+
private void UpdateSDKDependencies()
69+
{
70+
// Update GoogleMobileAdsDependencies.xml if Next Gen SDK is enabled.
71+
string dependenciesDir =
72+
Path.Combine(Application.dataPath, "GoogleMobileAds", "Editor");
73+
string projectPath = Directory.GetParent(Application.dataPath).ToString();
74+
string[] scripts = Directory.GetFiles(projectPath, "AndroidBuildPreProcessor.cs",
75+
SearchOption.AllDirectories);
76+
if (scripts.Length > 0)
77+
{
78+
dependenciesDir = Path.GetDirectoryName(scripts[0]);
79+
}
80+
string dependenciesFilePath =
81+
Path.Combine(dependenciesDir, "GoogleMobileAdsDependencies.xml");
82+
string content = File.ReadAllText(dependenciesFilePath);
83+
84+
if (File.Exists(dependenciesFilePath))
85+
{
86+
if (GoogleMobileAdsSettings.LoadInstance().EnableNextGenSDK &&
87+
Regex.IsMatch(content, PlayServicesAdsSDKRegex))
88+
{
89+
string newContent = Regex.Replace(content, PlayServicesAdsSDKRegex, NextGenSDK);
90+
if (content != newContent)
91+
{
92+
File.WriteAllText(dependenciesFilePath, newContent);
93+
}
94+
}
95+
else if (!GoogleMobileAdsSettings.LoadInstance().EnableNextGenSDK &&
96+
Regex.IsMatch(content, NextGenSDKRegex))
97+
{
98+
// Reverting from Next Gen SDK to Play Services Ads SDK.
99+
string newContent = Regex.Replace(content, NextGenSDKRegex, PlayServicesAdsSDK);
100+
if (content != newContent)
101+
{
102+
File.WriteAllText(dependenciesFilePath, newContent);
103+
}
104+
}
105+
PlayServicesResolver.ResolveSync(true);
106+
}
107+
}
108+
58109
private void ApplyBuildSettings(BuildReport report)
59110
{
60111
Debug.Log("Running Android Gradle Build Pre-Processor.");

source/plugin/Assets/GoogleMobileAds/Editor/GoogleMobileAdsSettings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ internal static GoogleMobileAdsSettings LoadInstance()
5050
[SerializeField]
5151
private bool disableOptimizeAdLoading;
5252

53+
[SerializeField]
54+
private bool enableNextGenSDK = false;
55+
5356
[SerializeField]
5457
private string userTrackingUsageDescription;
5558

@@ -98,6 +101,13 @@ public bool DisableOptimizeAdLoading
98101
set { disableOptimizeAdLoading = value; }
99102
}
100103

104+
public bool EnableNextGenSDK
105+
{
106+
get { return enableNextGenSDK; }
107+
108+
set { enableNextGenSDK = value; }
109+
}
110+
101111
public string UserTrackingUsageDescription
102112
{
103113
get { return userTrackingUsageDescription; }

source/plugin/Assets/GoogleMobileAds/Editor/GoogleMobileAdsSettingsEditor.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class GoogleMobileAdsSettingsEditor : UnityEditor.Editor
1818
SerializedProperty _enableKotlinXCoroutinesPackagingOption;
1919
SerializedProperty _disableOptimizeInitialization;
2020
SerializedProperty _disableOptimizeAdLoading;
21+
SerializedProperty _enableNextGenSDK;
2122
SerializedProperty _userLanguage;
2223
SerializedProperty _userTrackingUsageDescription;
2324

@@ -44,6 +45,7 @@ public void OnEnable()
4445
serializedObject.FindProperty("enableKotlinXCoroutinesPackagingOption");
4546
_disableOptimizeInitialization = serializedObject.FindProperty("disableOptimizeInitialization");
4647
_disableOptimizeAdLoading = serializedObject.FindProperty("disableOptimizeAdLoading");
48+
_enableNextGenSDK = serializedObject.FindProperty("enableNextGenSDK");
4749
_userLanguage = serializedObject.FindProperty("userLanguage");
4850
_userTrackingUsageDescription =
4951
serializedObject.FindProperty("userTrackingUsageDescription");
@@ -140,6 +142,17 @@ public override void OnInspectorGUI()
140142
MessageType.Info);
141143
}
142144

145+
146+
EditorGUILayout.PropertyField(
147+
_enableNextGenSDK,
148+
new GUIContent(localization.ForKey("ENABLE_NEXT_GEN_SDK")));
149+
150+
if (settings.EnableNextGenSDK)
151+
{
152+
EditorGUILayout.HelpBox(localization.ForKey("ENABLE_NEXT_GEN_SDK_HELPBOX"),
153+
MessageType.Info);
154+
}
155+
143156
EditorGUI.indentLevel--;
144157
EditorGUILayout.Separator();
145158

source/plugin/Assets/GoogleMobileAds/Editor/gma_settings_editor_localization_data.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
"en": "Disable initialization optimization",
5757
"fr": "Désactiver l'optimisation de l'initialisation"
5858
},
59+
"KEY_ENABLE_NEXT_GEN_SDK": {
60+
"en": "Enable Next Gen SDK",
61+
"fr": "Activer le SDK de nouvelle génération"
62+
},
63+
"KEY_ENABLE_NEXT_GEN_SDK_HELPBOX": {
64+
"en": "Utilizes the Google Mobile Ads Next Gen SDK. Please run Clean & Build after enabling or disabling this option for the first time.",
65+
"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."
66+
},
5967
"KEY_UMP_SPECIFIC_SETTINGS_LABEL": {
6068
"en": "UMP-specific settings",
6169
"fr": "Paramètres spécifiques UMP"

0 commit comments

Comments
 (0)