|
| 1 | +import org.jetbrains.kotlin.gradle.dsl.JvmTarget |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2026 Google LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +plugins { |
| 20 | + alias(libs.plugins.android.library) |
| 21 | + alias(libs.plugins.kotlin.android) |
| 22 | +} |
| 23 | + |
| 24 | +android { |
| 25 | + namespace = "com.example.library" |
| 26 | + compileSdk = libs.versions.compileSdk.get().toInt() |
| 27 | + |
| 28 | + defaultConfig { |
| 29 | + minSdk = libs.versions.minSdk.get().toInt() |
| 30 | + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" |
| 31 | + consumerProguardFiles("consumer-rules.pro") |
| 32 | + } |
| 33 | + buildTypes { |
| 34 | + release { |
| 35 | + isMinifyEnabled = false |
| 36 | + proguardFiles( |
| 37 | + getDefaultProguardFile("proguard-android-optimize.txt"), |
| 38 | + "proguard-rules.pro" |
| 39 | + ) |
| 40 | + } |
| 41 | + } |
| 42 | + lint { |
| 43 | + abortOnError = false |
| 44 | + } |
| 45 | + compileOptions { |
| 46 | + sourceCompatibility = JavaVersion.VERSION_17 |
| 47 | + targetCompatibility = JavaVersion.VERSION_17 |
| 48 | + } |
| 49 | + lint { |
| 50 | + disable += setOf("MissingInflatedId", "OnClick") |
| 51 | + sarifOutput = layout.buildDirectory.file("reports/lint-results-debug.sarif").get().asFile |
| 52 | + } |
| 53 | + kotlin { |
| 54 | + compilerOptions { |
| 55 | + jvmTarget.set(JvmTarget.JVM_17) |
| 56 | + javaParameters.set(true) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +dependencies { |
| 62 | + implementation(libs.appcompat) |
| 63 | + implementation(libs.core.ktx) |
| 64 | + implementation(libs.material) |
| 65 | + implementation(libs.startup.runtime) |
| 66 | + implementation(libs.play.services.maps) |
| 67 | + testImplementation(libs.junit) |
| 68 | + testImplementation(libs.robolectric) |
| 69 | + testImplementation(libs.mockk) |
| 70 | + testImplementation(libs.espresso.core) |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +abstract class GenerateArtifactIdTask : DefaultTask() { |
| 75 | + @get:OutputDirectory |
| 76 | + abstract val outputDir: DirectoryProperty |
| 77 | + |
| 78 | + @get:Input |
| 79 | + abstract val version: Property<String> |
| 80 | + |
| 81 | + @TaskAction |
| 82 | + fun generate() { |
| 83 | + val dir = outputDir.get().asFile |
| 84 | + val packageName = "com.example.library.utils.meta" |
| 85 | + val packagePath = packageName.replace('.', '/') |
| 86 | + val outputFile = File(dir, "$packagePath/ArtifactId.kt") |
| 87 | + outputFile.parentFile.mkdirs() |
| 88 | + val attributionId = "gmp_git_androidsamples_v${version.get()}" |
| 89 | + outputFile.writeText( |
| 90 | + """ |
| 91 | + package $packageName |
| 92 | +
|
| 93 | + /** |
| 94 | + * Automatically generated object containing the library's attribution ID. |
| 95 | + * This is used to track library usage for analytics. |
| 96 | + */ |
| 97 | + public object AttributionId { |
| 98 | + public const val VALUE: String = "$attributionId" |
| 99 | + } |
| 100 | + """.trimIndent() |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +val generateArtifactIdFile = tasks.register<GenerateArtifactIdTask>("generateArtifactIdFile") { |
| 106 | + outputDir.set(layout.buildDirectory.dir("generated/source/artifactId")) |
| 107 | + version.set(libs.versions.versionName.get()) |
| 108 | +} |
| 109 | + |
| 110 | +androidComponents { |
| 111 | + onVariants { variant -> |
| 112 | + variant.sources.java?.addGeneratedSourceDirectory( |
| 113 | + generateArtifactIdFile, |
| 114 | + GenerateArtifactIdTask::outputDir |
| 115 | + ) |
| 116 | + } |
| 117 | +} |
0 commit comments