Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validate_dependencies.gradle 4.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (C) 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import groovy.util.XmlSlurper
  15. import groovy.xml.XmlUtil
  16. import java.util.zip.ZipEntry
  17. import java.util.zip.ZipOutputStream
  18. configurations {
  19. // Configuration used to resolve the artifacts of dependencies.
  20. aarArtifacts.extendsFrom implementation
  21. }
  22. /**
  23. * Validates the Unity GMA plugin dependencies.
  24. * Add the following snippet to Assets/Plugins/Android/mainTemplate.gradle in the Unity Editor or
  25. * unityLibrary/build.gradle in an Android project to use this script:
  26. * <pre>{@code
  27. * gradle.projectsEvaluated {
  28. * apply from: 'GoogleMobileAdsPlugin.androidlib/validate_dependencies.gradle'
  29. * }
  30. * }</pre>
  31. */
  32. task validateDependencies {
  33. def expandedArchiveDirectory
  34. // List of artifacts resolved from the aarArtifacts configuration.
  35. project.configurations.aarArtifacts.
  36. resolvedConfiguration.lenientConfiguration.
  37. getArtifacts(Specs.satisfyAll()).findResults {
  38. ResolvedArtifact artifact ->
  39. File artifactTargetFile = new File(artifact.file.parent , artifact.file.name)
  40. // Desired artifact - com.google.android.gms:play-services-ads-lite:22.4.0
  41. // Group ID - com.google.android.gms
  42. // Artifact ID - play-services-ads-lite
  43. // Since Gradle has different naming convention for the same artifact in
  44. // * modules-2 cache - play-services-ads-lite-22.4.0.aar
  45. // * transforms-2 cache - com.google.android.gms.play-services-ads-lite-22.4.0
  46. // we look for the common segment.
  47. if (artifact.name.contains("play-services-ads-lite")) {
  48. // Explode the archive to a temporary directory.
  49. FileTree expandedArchive = project.zipTree(artifactTargetFile)
  50. expandedArchive.forEach { File androidManifest ->
  51. if (androidManifest.getName() == "AndroidManifest.xml") {
  52. def xml = new XmlSlurper().parse(androidManifest)
  53. def propertyNode = xml.depthFirst().find { it.name() == 'property' }
  54. if (propertyNode) {
  55. // Replace the <property> node with a comment.
  56. propertyNode.replaceNode {
  57. mkp.comment 'android.adservices.AD_SERVICES_CONFIG property'\
  58. + ' removed by GoogleMobileAds Unity plugin - Release notes: '\
  59. + 'https://github.com/googleads/googleads-mobile-unity/releases/'\
  60. + 'tag/v8.6.0'
  61. }
  62. }
  63. def updatedXml = XmlUtil.serialize(xml)
  64. androidManifest.setWritable(true)
  65. androidManifest.text = updatedXml
  66. expandedArchiveDirectory = androidManifest.parent
  67. }
  68. }
  69. // Update the artifact archive.
  70. artifactTargetFile.withOutputStream { outputStream ->
  71. def zipStream = new ZipOutputStream(outputStream)
  72. file(expandedArchiveDirectory).eachFileRecurse { file ->
  73. if (file.isFile()) {
  74. def entry = new ZipEntry(file.name)
  75. zipStream.putNextEntry(entry)
  76. file.withInputStream { zipStream << it }
  77. zipStream.closeEntry()
  78. }
  79. }
  80. zipStream.close()
  81. }
  82. }
  83. }
  84. // Clean up the temporary directory.
  85. if (expandedArchiveDirectory) delete expandedArchiveDirectory
  86. }
  87. // Run the update task before unityLibrary project is built.
  88. project(':unityLibrary:GoogleMobileAdsPlugin.androidlib') {
  89. tasks.named('preBuild') {
  90. dependsOn validateDependencies
  91. }
  92. }