private static List <string> ReadSKAdNetworkIdentifiersFromXML()
    {
        List <string> skAdNetworkItems = new List <string>();

        string path = Path.Combine(Application.dataPath, SKADNETWORKS_RELATIVE_PATH);

        if (AssetDatabase.IsValidFolder("Packages/com.google.ads.mobile"))
        {
            path = Path.Combine("Packages/com.google.ads.mobile", SKADNETWORKS_RELATIVE_PATH);
        }

        try
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException();
            }
            using (FileStream fs = File.OpenRead(path))
            {
                XmlDocument document = new XmlDocument();
                document.Load(fs);

                XmlNode root = document.FirstChild;

                XmlNodeList nodes = root.SelectNodes(KEY_SK_ADNETWORK_ID);
                foreach (XmlNode node in nodes)
                {
                    skAdNetworkItems.Add(node.InnerText);
                }
            }
        }
        #pragma warning disable 0168
        catch (FileNotFoundException e)
        #pragma warning restore 0168
        {
            GoogleMobileAdsAnalytics.ReportProcessPlistFailedMissingSKAdNetworkIds();
            NotifyBuildFailure("GoogleMobileAdsSKAdNetworkItems.xml not found", false);
        }
        catch (IOException e)
        {
            GoogleMobileAdsAnalytics.ReportProcessPlistFailedSKAdNetworkIdsIoError();
            NotifyBuildFailure("Failed to read GoogleMobileAdsSKAdNetworkIds.xml: " + e.Message, false);
        }

        return(skAdNetworkItems);
    }
    private static void AddSKAdNetworkIdentifier(PlistDocument document, List <string> skAdNetworkIds)
    {
        PlistElementArray array = GetSKAdNetworkItemsArray(document);

        if (array != null)
        {
            foreach (string id in skAdNetworkIds)
            {
                if (!ContainsSKAdNetworkIdentifier(array, id))
                {
                    PlistElementDict added = array.AddDict();
                    added.SetString(KEY_SK_ADNETWORK_ID, id);
                }
            }
        }
        else
        {
            GoogleMobileAdsAnalytics.ReportProcessPlistFailedInvalidSKAdNetworkIds();
            NotifyBuildFailure("SKAdNetworkItems element already exists in Info.plist, but is not an array.", false);
        }
    }
    public static void OnPostProcessBuild(BuildTarget buildTarget, string path)
    {
        GoogleMobileAdsAnalytics.ReportProcessPlistStarted();
        string        plistPath = Path.Combine(path, "Info.plist");
        PlistDocument plist     = new PlistDocument();

        plist.ReadFromFile(plistPath);

        string appId = GoogleMobileAdsSettings.Instance.GoogleMobileAdsIOSAppId;

        if (appId.Length == 0)
        {
            GoogleMobileAdsAnalytics.ReportProcessPlistFailedEmptyGoogleMobileAdsAppId();
            NotifyBuildFailure(
                "iOS Google Mobile Ads app ID is empty. Please enter a valid app ID to run ads properly.");
        }
        else
        {
            plist.root.SetString("GADApplicationIdentifier", appId);
        }

        if (GoogleMobileAdsSettings.Instance.DelayAppMeasurementInit)
        {
            plist.root.SetBoolean("GADDelayAppMeasurementInit", true);
        }

        List <string> skNetworkIds = ReadSKAdNetworkIdentifiersFromXML();

        if (skNetworkIds.Count > 0)
        {
            AddSKAdNetworkIdentifier(plist, skNetworkIds);
        }

        File.WriteAllText(plistPath, plist.WriteToString());
        GoogleMobileAdsAnalytics.ReportProcessPlistSuccessful();
    }
    public void OnPreprocessBuild(BuildTarget target, string path)
#endif
    {
        GoogleMobileAdsAnalytics.ReportProcessManifestStarted();

        string manifestPath = Path.Combine(
            Application.dataPath, MANIFEST_RELATIVE_PATH);

        if (AssetDatabase.IsValidFolder("Packages/com.google.ads.mobile"))
        {
            manifestPath = Path.Combine("Packages/com.google.ads.mobile", MANIFEST_RELATIVE_PATH);
        }

        XDocument manifest = null;

        try
        {
            manifest = XDocument.Load(manifestPath);
        }
        #pragma warning disable 0168
        catch (IOException e)
        #pragma warning restore 0168
        {
            GoogleMobileAdsAnalytics.ReportProcessManifestFailedMissingFile();
            StopBuildWithMessage("AndroidManifest.xml is missing. Try re-importing the plugin.");
        }

        XElement elemManifest = manifest.Element("manifest");
        if (elemManifest == null)
        {
            GoogleMobileAdsAnalytics.ReportProcessManifestFailedInvalidFile();
            StopBuildWithMessage("AndroidManifest.xml is not valid. Try re-importing the plugin.");
        }

        XElement elemApplication = elemManifest.Element("application");
        if (elemApplication == null)
        {
            GoogleMobileAdsAnalytics.ReportProcessManifestFailedInvalidFile();
            StopBuildWithMessage("AndroidManifest.xml is not valid. Try re-importing the plugin.");
        }

        IEnumerable <XElement> metas = elemApplication.Descendants()
                                       .Where(elem => elem.Name.LocalName.Equals("meta-data"));

        XElement elemGMAEnabled = GetMetaElement(metas, META_APPLICATION_ID);
        string   appId          = GoogleMobileAdsSettings.Instance.GoogleMobileAdsAndroidAppId;

        if (appId.Length == 0)
        {
            GoogleMobileAdsAnalytics.ReportProcessManifestFailedEmptyGoogleMobileAdsAppId();
            StopBuildWithMessage(
                "Android Google Mobile Ads app ID is empty. Please enter a valid app ID to run ads properly.");
        }

        if (elemGMAEnabled == null)
        {
            elemApplication.Add(CreateMetaElement(META_APPLICATION_ID, appId));
        }
        else
        {
            elemGMAEnabled.SetAttributeValue(ns + "value", appId);
        }


        XElement elemDelayAppMeasurementInit =
            GetMetaElement(metas, META_DELAY_APP_MEASUREMENT_INIT);
        if (GoogleMobileAdsSettings.Instance.DelayAppMeasurementInit)
        {
            if (elemDelayAppMeasurementInit == null)
            {
                elemApplication.Add(CreateMetaElement(META_DELAY_APP_MEASUREMENT_INIT, true));
            }
            else
            {
                elemDelayAppMeasurementInit.SetAttributeValue(ns + "value", true);
            }
        }
        else
        {
            if (elemDelayAppMeasurementInit != null)
            {
                elemDelayAppMeasurementInit.Remove();
            }
        }

        elemManifest.Save(manifestPath);
        GoogleMobileAdsAnalytics.ReportProcessManifestSuccessful();
    }