// Copyright (C) 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import groovy.util.XmlSlurper import groovy.xml.XmlUtil import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream configurations { // Configuration used to resolve the artifacts of dependencies. aarArtifacts.extendsFrom implementation } /** * Validates the Unity GMA plugin dependencies. * Add the following snippet to Assets/Plugins/Android/mainTemplate.gradle in the Unity Editor or * unityLibrary/build.gradle in an Android project to use this script: *
{@code
* gradle.projectsEvaluated {
* apply from: 'GoogleMobileAdsPlugin.androidlib/validate_dependencies.gradle'
* }
* }
*/
task validateDependencies {
def expandedArchiveDirectory
// List of artifacts resolved from the aarArtifacts configuration.
project.configurations.aarArtifacts.
resolvedConfiguration.lenientConfiguration.
getArtifacts(Specs.satisfyAll()).findResults {
ResolvedArtifact artifact ->
File artifactTargetFile = new File(artifact.file.parent , artifact.file.name)
// Desired artifact - com.google.android.gms:play-services-ads-lite:22.4.0
// Group ID - com.google.android.gms
// Artifact ID - play-services-ads-lite
// Since Gradle has different naming convention for the same artifact in
// * modules-2 cache - play-services-ads-lite-22.4.0.aar
// * transforms-2 cache - com.google.android.gms.play-services-ads-lite-22.4.0
// we look for the common segment.
if (artifact.name.contains("play-services-ads-lite")) {
// Explode the archive to a temporary directory.
FileTree expandedArchive = project.zipTree(artifactTargetFile)
expandedArchive.forEach { File androidManifest ->
if (androidManifest.getName() == "AndroidManifest.xml") {
def xml = new XmlSlurper().parse(androidManifest)
def propertyNode = xml.depthFirst().find { it.name() == 'property' }
if (propertyNode) {
// Replace the