From d952b7126b8dcc15b7c9efb7cdbf2de02b97043e Mon Sep 17 00:00:00 2001 From: juncong lee Date: Sun, 28 Sep 2025 10:03:20 +0800 Subject: [PATCH] =?UTF-8?q?admobID=E4=BB=8E=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Editor/ManifestProcessor.cs | 20 +-- .../Editor/AppLovinPostProcessAndroid.cs | 2 +- .../Editor/AppLovinProcessGradleBuildFile.cs | 4 +- Assets/Script/Editor.meta | 8 + .../Editor/WZAndroidBuildPostProcessor.cs | 157 ++++++++++++++++++ .../WZAndroidBuildPostProcessor.cs.meta | 11 ++ .../KwaiAdsManager/KwaiFloorRvManager.cs | 4 +- Assets/Script/Utils/EditorFileUtils.cs | 42 +++++ Assets/Script/Utils/EditorFileUtils.cs.meta | 11 ++ .../Editor/ATPostProcessBuildAndroid.cs | 4 +- 10 files changed, 246 insertions(+), 17 deletions(-) create mode 100644 Assets/Script/Editor.meta create mode 100644 Assets/Script/Editor/WZAndroidBuildPostProcessor.cs create mode 100644 Assets/Script/Editor/WZAndroidBuildPostProcessor.cs.meta create mode 100644 Assets/Script/Utils/EditorFileUtils.cs create mode 100644 Assets/Script/Utils/EditorFileUtils.cs.meta diff --git a/Assets/GoogleMobileAds/Editor/ManifestProcessor.cs b/Assets/GoogleMobileAds/Editor/ManifestProcessor.cs index 89f1d40..e88ab57 100755 --- a/Assets/GoogleMobileAds/Editor/ManifestProcessor.cs +++ b/Assets/GoogleMobileAds/Editor/ManifestProcessor.cs @@ -119,21 +119,21 @@ public class ManifestProcessor : IPreprocessBuild } GoogleMobileAdsSettings instance = GoogleMobileAdsSettings.LoadInstance(); - string appId = instance.GoogleMobileAdsAndroidAppId; + // string appId = instance.GoogleMobileAdsAndroidAppId; - if (appId.Length == 0) - { - StopBuildWithMessage( - "Android Google Mobile Ads app ID is empty. Please enter a valid app ID to run ads properly."); - } + // if (appId.Length == 0) + // { + // StopBuildWithMessage( + // "Android Google Mobile Ads app ID is empty. Please enter a valid app ID to run ads properly."); + // } IEnumerable metas = elemApplication.Descendants() .Where( elem => elem.Name.LocalName.Equals("meta-data")); - SetMetadataElement(elemApplication, - metas, - METADATA_APPLICATION_ID, - appId); + // SetMetadataElement(elemApplication, + // metas, + // METADATA_APPLICATION_ID, + // appId); SetMetadataElement(elemApplication, metas, diff --git a/Assets/MaxSdk/Scripts/IntegrationManager/Editor/AppLovinPostProcessAndroid.cs b/Assets/MaxSdk/Scripts/IntegrationManager/Editor/AppLovinPostProcessAndroid.cs index 0e90c9c..31ad202 100644 --- a/Assets/MaxSdk/Scripts/IntegrationManager/Editor/AppLovinPostProcessAndroid.cs +++ b/Assets/MaxSdk/Scripts/IntegrationManager/Editor/AppLovinPostProcessAndroid.cs @@ -163,7 +163,7 @@ namespace AppLovinMax.Scripts.IntegrationManager.Editor var metaDataElements = elementApplication.Descendants().Where(element => element.Name.LocalName.Equals("meta-data")); EnableVerboseLoggingIfNeeded(elementApplication); - AddGoogleApplicationIdIfNeeded(elementApplication, metaDataElements); + //AddGoogleApplicationIdIfNeeded(elementApplication, metaDataElements); AddGoogleOptimizationFlagsIfNeeded(elementApplication, metaDataElements); DisableAutoInitIfNeeded(elementApplication, metaDataElements); RemoveSdkKeyIfNeeded(metaDataElements); diff --git a/Assets/MaxSdk/Scripts/IntegrationManager/Editor/AppLovinProcessGradleBuildFile.cs b/Assets/MaxSdk/Scripts/IntegrationManager/Editor/AppLovinProcessGradleBuildFile.cs index 464bd04..aa7eab6 100644 --- a/Assets/MaxSdk/Scripts/IntegrationManager/Editor/AppLovinProcessGradleBuildFile.cs +++ b/Assets/MaxSdk/Scripts/IntegrationManager/Editor/AppLovinProcessGradleBuildFile.cs @@ -100,7 +100,7 @@ namespace AppLovinMax.Scripts.IntegrationManager.Editor var sdkKey = AppLovinSettings.Instance.SdkKey; if (string.IsNullOrEmpty(sdkKey)) { - MaxSdkLogger.UserError("Failed to install AppLovin Quality Service plugin. SDK Key is empty. Please enter the AppLovin SDK Key in the Integration Manager."); + //MaxSdkLogger.UserError("Failed to install AppLovin Quality Service plugin. SDK Key is empty. Please enter the AppLovin SDK Key in the Integration Manager."); return; } @@ -109,7 +109,7 @@ namespace AppLovinMax.Scripts.IntegrationManager.Editor var apiKey = qualityServiceData.api_key; if (string.IsNullOrEmpty(apiKey)) { - MaxSdkLogger.UserError("Failed to install AppLovin Quality Service plugin. API Key is empty."); + //MaxSdkLogger.UserError("Failed to install AppLovin Quality Service plugin. API Key is empty."); return; } diff --git a/Assets/Script/Editor.meta b/Assets/Script/Editor.meta new file mode 100644 index 0000000..ca417f8 --- /dev/null +++ b/Assets/Script/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 725371b12142d45e7b38d6fe93bd61e8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Script/Editor/WZAndroidBuildPostProcessor.cs b/Assets/Script/Editor/WZAndroidBuildPostProcessor.cs new file mode 100644 index 0000000..0650d82 --- /dev/null +++ b/Assets/Script/Editor/WZAndroidBuildPostProcessor.cs @@ -0,0 +1,157 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Xml; +using System.Xml.Linq; +using Google.MiniJSON; +using UnityEditor; +using UnityEditor.Android; +using UnityEditor.Build; +using UnityEngine; + +#if UNITY_ANDROID + +namespace WZ +{ + public class Android : IPostGenerateGradleAndroidProject + { + + public int callbackOrder => int.MaxValue; + + public void OnPostGenerateGradleAndroidProject(string path) + { +#if UNITY_2019_3_OR_NEWER + AddAdmobAppId(path); +#endif + } + + private void AddAdmobAppId(string path) + { + var appManifestPath = Path.Combine(path, "src/main/AndroidManifest.xml"); + + var manifestFile = new XmlDocument(); + manifestFile.Load(appManifestPath); + const string gmsAdsApplicationID = "com.google.android.gms.ads.APPLICATION_ID"; + var admobAppId = GetMetaDataValue(manifestFile, gmsAdsApplicationID); + + var projectAdmobAppId = GetAdmobAppId(); + LoggerUtils.Debug("Get admob id:"+projectAdmobAppId); + + if (string.IsNullOrEmpty(admobAppId)) + { + AddMetaDataValue(manifestFile, gmsAdsApplicationID, projectAdmobAppId); + } + else + { + UpdateMetaDataValue(manifestFile, gmsAdsApplicationID, projectAdmobAppId); + } + + var androidManifestFileArray = EditorFileUtils.GetAllFileWhereSuffix(path, "AndroidManifest.xml"); + foreach (var tempPath in androidManifestFileArray) + { + var newText = File.ReadAllText(tempPath).Replace("INSERT_YOUR_ADMOB_APP_ID_HERE", projectAdmobAppId); + File.WriteAllText(tempPath, newText); + } + + const string OPTIMIZE_INITIALIZATION = "com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"; + + var optimizeInitialization = GetMetaDataValue(manifestFile, OPTIMIZE_INITIALIZATION); + if (string.IsNullOrEmpty(optimizeInitialization)) + { + AddMetaDataValue(manifestFile, OPTIMIZE_INITIALIZATION, "true"); + } + else + { + UpdateMetaDataValue(manifestFile, OPTIMIZE_INITIALIZATION, "true"); + } + + const string OPTIMIZE_AD_LOADING = "com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"; + var optimizeAdLoading = GetMetaDataValue(manifestFile, OPTIMIZE_AD_LOADING); + if (string.IsNullOrEmpty(optimizeAdLoading)) + { + AddMetaDataValue(manifestFile, OPTIMIZE_AD_LOADING, "true"); + } + else + { + UpdateMetaDataValue(manifestFile, OPTIMIZE_AD_LOADING, "true"); + } + + manifestFile.Save(appManifestPath); + + + if (string.IsNullOrEmpty(projectAdmobAppId)) + { + // 显示错误提示框 + EditorUtility.DisplayDialog("Error", "Google advertising channel was used, but admobId was not configured in the configuration file.", "Ok"); + throw new Exception("Google advertising channel was used, but admobId was not configured in the configuration file."); + } + } + + private bool UpdateMetaDataValue(XmlDocument manifest, string name, string value) + { + var xpath = $"/manifest/application/meta-data[@android:name='{name}']"; + var node = manifest.DocumentElement?.SelectSingleNode(xpath, GetNamespaceManager(manifest)); + if (!(node is XmlElement element)) return false; + var attributeNode = element.GetAttributeNode("android:value"); + if (attributeNode != null) + { + attributeNode.Value = value; + } + + return false; + } + + private bool AddMetaDataValue(XmlDocument manifest, string name, string value) + { + var metaNode = manifest.CreateElement("meta-data"); + + var nameAttribute = manifest.CreateAttribute("android", "name", "http://schemas.android.com/apk/res/android"); + nameAttribute.InnerText = name; + metaNode.SetAttributeNode(nameAttribute); + + var valueAttribute = manifest.CreateAttribute("android", "value", "http://schemas.android.com/apk/res/android"); + valueAttribute.InnerText = value; + metaNode.SetAttributeNode(valueAttribute); + + + var applicationNode = manifest.SelectSingleNode("/manifest/application"); + if (applicationNode == null) return false; + applicationNode.AppendChild(metaNode); + return true; + } + + + private static string GetAdmobAppId() + { + return FileParse.GetConfigByKey(FileParse.KEY_Admob_AppID); + } + + private static string GetMetaDataValue(XmlDocument manifest, string name) + { + var xpath = $"/manifest/application/meta-data[@android:name='{name}']"; + var node = manifest.DocumentElement?.SelectSingleNode(xpath, GetNamespaceManager(manifest)); + if (node is XmlElement element) + { + return element.GetAttribute("android:value"); + } + + return ""; + } + + private static XmlNamespaceManager GetNamespaceManager(XmlDocument manifest) + { + var namespaceManager = new XmlNamespaceManager(manifest.NameTable); + namespaceManager.AddNamespace("android", "http://schemas.android.com/apk/res/android"); + return namespaceManager; + } + + + + } +} + + +#endif \ No newline at end of file diff --git a/Assets/Script/Editor/WZAndroidBuildPostProcessor.cs.meta b/Assets/Script/Editor/WZAndroidBuildPostProcessor.cs.meta new file mode 100644 index 0000000..4706bc9 --- /dev/null +++ b/Assets/Script/Editor/WZAndroidBuildPostProcessor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 049fe3d95026b4c5c88ebf67e55687b4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Script/SDKManager/AdsSDKManager/KwaiAdsManager/KwaiFloorRvManager.cs b/Assets/Script/SDKManager/AdsSDKManager/KwaiAdsManager/KwaiFloorRvManager.cs index 608119f..74160a5 100644 --- a/Assets/Script/SDKManager/AdsSDKManager/KwaiAdsManager/KwaiFloorRvManager.cs +++ b/Assets/Script/SDKManager/AdsSDKManager/KwaiAdsManager/KwaiFloorRvManager.cs @@ -36,8 +36,8 @@ namespace WZ { _rvParallelRequests = KwaiAdsConfigParser.GetRvParallelRequests(); _rvFloorConfigs = KwaiAdsConfigParser.GetRvFloorConfigs(); - KwaiAdsManager.Instance._appId = KwaiAdsConfigParser.GetKwaiAppId(); - KwaiAdsManager.Instance._token = KwaiAdsConfigParser.GetKwaiAppToken(); + //KwaiAdsManager.Instance._appId = KwaiAdsConfigParser.GetKwaiAppId(); + //KwaiAdsManager.Instance._token = KwaiAdsConfigParser.GetKwaiAppToken(); LoggerUtils.Debug($"[kwai] floor reward bidding config loaded. FloorOpen: {KwaiAdsConfigParser.GetKwaiRvFloorOpen()}, ParallelRequests: {_rvParallelRequests}, Floors: {_rvFloorConfigs.Count}"); } diff --git a/Assets/Script/Utils/EditorFileUtils.cs b/Assets/Script/Utils/EditorFileUtils.cs new file mode 100644 index 0000000..18498ed --- /dev/null +++ b/Assets/Script/Utils/EditorFileUtils.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using UnityEngine; + +namespace WZ +{ + public class EditorFileUtils + { + public static List GetAllFileWhereSuffix(string projectPath, string suffix) + { + return ListFiles(projectPath).Where(file => file.EndsWith(suffix)).ToList(); + } + + + public static List ListFiles(string path, List fileList = null) + { + fileList ??= new List(); + + try + { + // 获取当前目录下所有文件 + var files = Directory.GetFiles(path); + fileList.AddRange(files); + + // 递归获取子文件夹中的所有文件 + var subdirectories = Directory.GetDirectories(path); + foreach (var subdirectory in subdirectories) + { + ListFiles(subdirectory, fileList); + } + } + catch (Exception e) + { + LoggerUtils.Error("An error occurred: " + e.Message); + } + + return fileList; + } + } +} \ No newline at end of file diff --git a/Assets/Script/Utils/EditorFileUtils.cs.meta b/Assets/Script/Utils/EditorFileUtils.cs.meta new file mode 100644 index 0000000..55b3f52 --- /dev/null +++ b/Assets/Script/Utils/EditorFileUtils.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 86e31887016d04347ac602e07c22a9d0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ThinkupTpnPlugin/Script/Editor/ATPostProcessBuildAndroid.cs b/Assets/ThinkupTpnPlugin/Script/Editor/ATPostProcessBuildAndroid.cs index 13f9824..5c56954 100644 --- a/Assets/ThinkupTpnPlugin/Script/Editor/ATPostProcessBuildAndroid.cs +++ b/Assets/ThinkupTpnPlugin/Script/Editor/ATPostProcessBuildAndroid.cs @@ -136,8 +136,8 @@ namespace AnyThink.Scripts.Editor return; } - var metaDataElements = elementApplication.Descendants().Where(element => element.Name.LocalName.Equals("meta-data")); - addGoogleApplicationIdIfNeeded(elementApplication, metaDataElements); + //var metaDataElements = elementApplication.Descendants().Where(element => element.Name.LocalName.Equals("meta-data")); + //addGoogleApplicationIdIfNeeded(elementApplication, metaDataElements); // Save the updated manifest file. manifest.Save(manifestPath); }