|
| 1 | +import java.util.zip.ZipFile |
| 2 | +import org.gradle.api.artifacts.transform.InputArtifact |
| 3 | +import org.gradle.api.artifacts.transform.TransformAction |
| 4 | +import org.gradle.api.artifacts.transform.TransformOutputs |
| 5 | +import org.gradle.api.artifacts.transform.TransformParameters |
| 6 | +import org.gradle.api.attributes.AttributeCompatibilityRule |
| 7 | +import org.gradle.api.attributes.CompatibilityCheckDetails |
| 8 | +import org.gradle.api.attributes.LibraryElements |
| 9 | + |
| 10 | +apply plugin: 'java' |
| 11 | + |
| 12 | +repositories { |
| 13 | + mavenLocal() |
| 14 | + |
| 15 | + def proxyUrl = System.getenv("MAVEN_REPOSITORY_PROXY") |
| 16 | + if (proxyUrl) { |
| 17 | + println "Using proxy repository: $proxyUrl" |
| 18 | + maven { |
| 19 | + url = proxyUrl |
| 20 | + allowInsecureProtocol = true |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + mavenCentral() |
| 25 | + |
| 26 | + // androidx.test artifacts (Robolectric runtime dependencies) are published only to Google's Maven |
| 27 | + // repository. Scope it strictly to androidx.* so nothing else resolves from it. |
| 28 | + maven { |
| 29 | + url = 'https://maven.google.com' |
| 30 | + content { |
| 31 | + includeGroupByRegex 'androidx\\..*' |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// androidx.test ships as Android archives (.aar), which a plain-JVM build cannot consume directly. |
| 37 | +// Teach Gradle to accept the aar variant and extract its classes.jar — the part the Android Gradle |
| 38 | +// plugin would otherwise do. Safe to apply broadly here: this is a standalone project with no |
| 39 | +// class-directory (project) dependencies. |
| 40 | +abstract class AarToJarCompatibility implements AttributeCompatibilityRule<LibraryElements> { |
| 41 | + @Override |
| 42 | + void execute(CompatibilityCheckDetails<LibraryElements> details) { |
| 43 | + if (details.producerValue != null && details.producerValue.name == 'aar') { |
| 44 | + details.compatible() |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +abstract class ExtractAarClasses implements TransformAction<TransformParameters.None> { |
| 50 | + @InputArtifact |
| 51 | + abstract Provider<FileSystemLocation> getInputArtifact() |
| 52 | + |
| 53 | + @Override |
| 54 | + void transform(TransformOutputs outputs) { |
| 55 | + def aar = inputArtifact.get().asFile |
| 56 | + if (!aar.name.endsWith('.aar')) { |
| 57 | + outputs.file(aar) |
| 58 | + return |
| 59 | + } |
| 60 | + new ZipFile(aar).withCloseable { zip -> |
| 61 | + def entry = zip.getEntry('classes.jar') |
| 62 | + def out = outputs.file(aar.name - '.aar' + '.jar') |
| 63 | + if (entry != null) { |
| 64 | + out.withOutputStream { os -> zip.getInputStream(entry).withCloseable { input -> os << input } } |
| 65 | + } else { |
| 66 | + new java.util.jar.JarOutputStream(out.newOutputStream()).close() |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +def artifactType = Attribute.of('artifactType', String) |
| 73 | + |
| 74 | +dependencies { |
| 75 | + attributesSchema { |
| 76 | + attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE) { |
| 77 | + compatibilityRules.add(AarToJarCompatibility) |
| 78 | + } |
| 79 | + } |
| 80 | + registerTransform(ExtractAarClasses) { |
| 81 | + from.attribute(artifactType, 'aar') |
| 82 | + to.attribute(artifactType, 'jar') |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +configurations.configureEach { |
| 87 | + if (canBeResolved) { |
| 88 | + attributes.attribute(artifactType, 'jar') |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +dependencies { |
| 93 | + testImplementation 'junit:junit:4.13.2' |
| 94 | + testImplementation 'org.robolectric:robolectric:4.16.1' |
| 95 | + // Pre-built Android SDK jar for the level the fixtures configure. |
| 96 | + testImplementation 'org.robolectric:android-all:14-robolectric-10818077' |
| 97 | + // androidx.test:core pulls in androidx.test:monitor (InstrumentationRegistry); ext:junit provides |
| 98 | + // the AndroidJUnit4 runner, which delegates to RobolectricTestRunner for local (host-JVM) tests. |
| 99 | + testImplementation 'androidx.test:core:1.6.1' |
| 100 | + testImplementation 'androidx.test.ext:junit:1.2.1' |
| 101 | +} |
| 102 | + |
| 103 | +test { |
| 104 | + useJUnit() |
| 105 | + // Coverage is validated by the other Gradle smoke scenarios; disable it here so this scenario |
| 106 | + // stays focused on the emulated-SDK (test.android.*) tags. |
| 107 | + environment "DD_CIVISIBILITY_CODE_COVERAGE_ENABLED", "false" |
| 108 | +} |
0 commit comments