admobID从配置文件读取
This commit is contained in:
parent
6d0bdbd4da
commit
d952b7126b
|
@ -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<XElement> 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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 725371b12142d45e7b38d6fe93bd61e8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 049fe3d95026b4c5c88ebf67e55687b4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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}");
|
||||
}
|
||||
|
||||
|
|
|
@ -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<string> GetAllFileWhereSuffix(string projectPath, string suffix)
|
||||
{
|
||||
return ListFiles(projectPath).Where(file => file.EndsWith(suffix)).ToList();
|
||||
}
|
||||
|
||||
|
||||
public static List<string> ListFiles(string path, List<string> fileList = null)
|
||||
{
|
||||
fileList ??= new List<string>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 86e31887016d04347ac602e07c22a9d0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue