Skip to content

Commit c2c1c47

Browse files
authored
feat: Integrate AttributionId Flow (#2386)
* integrate startup-runtime lib Rebase with main branch * fixed all comments * refactor(library): address PR review feedback on build config and dependencies * refactor(snippets): move attribution library dependency to :snippets:common and clean up rebase leftovers
1 parent a7cde34 commit c2c1c47

17 files changed

Lines changed: 302 additions & 1 deletion

File tree

‎ApiDemos/project/common-ui/build.gradle.kts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ android {
6666
}
6767

6868
dependencies {
69+
implementation(project(":library"))
6970
implementation(libs.core.ktx)
7071
implementation(libs.appcompat)
7172
implementation(libs.material)
7273
implementation(libs.play.services.maps)
73-
74+
7475
// Jetpack Compose
7576
implementation(platform(libs.compose.bom))
7677
implementation(libs.ui)

‎FireMarkers/app/build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ android {
9898
}
9999

100100
dependencies {
101+
implementation(project(":library"))
101102
// ---------------------------------------------------------------------------------------------
102103
// AndroidX & Jetpack
103104
//

‎README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ To run instrumentation tests on a connected device or emulator, use:
7070
Alternatively, use the `gradlew build` command to build the project directly or download an APK
7171
under [releases](https://github.com/googlemaps/android-samples/releases).
7272

73+
## Internal usage attribution ID
74+
75+
This library calls the `addInternalUsageAttributionId` method, which helps Google understand which libraries and samples are helpful to developers and is optional. Instructions for opting out of the identifier are provided below.
76+
77+
If you wish to disable this, you can do so by removing the initializer in your `AndroidManifest.xml` using the `tools:node="remove"` attribute:
78+
79+
```xml
80+
<provider
81+
android:name="androidx.startup.InitializationProvider"
82+
android:authorities="${applicationId}.androidx-startup"
83+
android:exported="false"
84+
tools:node="merge">
85+
<meta-data
86+
android:name="com.example.library.utils.attribution.AttributionIdInitializer"
87+
tools:node="remove" />
88+
</provider>
89+
```
90+
7391
## Contributing
7492

7593
Contributions are welcome and encouraged! If you'd like to contribute, send us a [pull request] and refer to our [code of conduct] and [contributing guide].

‎WearOS/Wearable/build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ dependencies {
6363
implementation(libs.core.ktx)
6464
implementation(platform(libs.kotlin.bom))
6565
implementation(libs.kotlin.stdlib)
66+
implementation(project(":library"))
6667
// [END_EXCLUDE]
6768
// Modern Android projects use version catalogs to manage dependencies. To include the necessary dependencies,
6869
// first add the following to your libs.versions.toml file:

‎gradle/libs.versions.toml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ androidxTestRules = "1.7.0"
6262
truth = "1.4.5"
6363
turbine = "1.2.1"
6464
uiautomator = "2.4.0"
65+
mockk = "1.14.11"
6566

6667
# Firebase
6768
firebaseBom = "34.19.0"
@@ -76,12 +77,16 @@ kotlinxSerialization = "1.11.0"
7677
# Code formatting
7778
spotless = "8.10.4"
7879

80+
#Tracker lib
81+
startup-runtime = "1.2.0"
82+
7983
[libraries]
8084
# Kotlin
8185
kotlin-bom = { module = "org.jetbrains.kotlin:kotlin-bom", version.ref = "kotlin" }
8286
kotlin-stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib", version.ref = "kotlin" }
8387
kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }
8488
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinxDatetime" }
89+
startup-runtime = { module = "androidx.startup:startup-runtime", version.ref = "startup-runtime" }
8590

8691
# AndroidX
8792
activity = { module = "androidx.activity:activity", version.ref = "activity" }
@@ -148,6 +153,7 @@ turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine" }
148153
ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose" }
149154
ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "compose" }
150155
uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "uiautomator" }
156+
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
151157

152158
# Firebase
153159
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebaseBom" }

‎library/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

‎library/build.gradle.kts‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}
22+
23+
android {
24+
namespace = "com.example.library"
25+
compileSdk = libs.versions.compileSdk.get().toInt()
26+
27+
defaultConfig {
28+
minSdk = libs.versions.minSdk.get().toInt()
29+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
30+
consumerProguardFiles("consumer-rules.pro")
31+
}
32+
buildTypes {
33+
release {
34+
isMinifyEnabled = false
35+
proguardFiles(
36+
getDefaultProguardFile("proguard-android-optimize.txt"),
37+
"proguard-rules.pro"
38+
)
39+
}
40+
}
41+
compileOptions {
42+
sourceCompatibility = JavaVersion.VERSION_11
43+
targetCompatibility = JavaVersion.VERSION_11
44+
}
45+
lint {
46+
abortOnError = false
47+
disable += setOf("MissingInflatedId", "OnClick")
48+
sarifOutput = layout.buildDirectory.file("reports/lint-results-debug.sarif").get().asFile
49+
}
50+
kotlin {
51+
compilerOptions {
52+
jvmTarget.set(JvmTarget.JVM_11)
53+
javaParameters.set(true)
54+
}
55+
}
56+
}
57+
58+
dependencies {
59+
implementation(libs.startup.runtime)
60+
implementation(libs.play.services.maps)
61+
testImplementation(libs.junit)
62+
testImplementation(libs.robolectric)
63+
testImplementation(libs.mockk)
64+
}
65+
66+
67+
abstract class GenerateArtifactIdTask : DefaultTask() {
68+
@get:OutputDirectory
69+
abstract val outputDir: DirectoryProperty
70+
71+
@get:Input
72+
abstract val version: Property<String>
73+
74+
@TaskAction
75+
fun generate() {
76+
val dir = outputDir.get().asFile
77+
val packageName = "com.example.library.utils.meta"
78+
val packagePath = packageName.replace('.', '/')
79+
val outputFile = File(dir, "$packagePath/ArtifactId.kt")
80+
outputFile.parentFile.mkdirs()
81+
val attributionId = "gmp_git_androidsamples_v${version.get()}"
82+
outputFile.writeText(
83+
"""
84+
package $packageName
85+
86+
/**
87+
* Automatically generated object containing the library's attribution ID.
88+
* This is used to track library usage for analytics.
89+
*/
90+
public object AttributionId {
91+
public const val VALUE: String = "$attributionId"
92+
}
93+
""".trimIndent()
94+
)
95+
}
96+
}
97+
98+
val generateArtifactIdFile = tasks.register<GenerateArtifactIdTask>("generateArtifactIdFile") {
99+
outputDir.set(layout.buildDirectory.dir("generated/source/artifactId"))
100+
version.set(libs.versions.versionName.get())
101+
}
102+
103+
androidComponents {
104+
onVariants { variant ->
105+
variant.sources.java?.addGeneratedSourceDirectory(
106+
generateArtifactIdFile,
107+
GenerateArtifactIdTask::outputDir
108+
)
109+
}
110+
}

‎library/consumer-rules.pro‎

Whitespace-only changes.

‎library/proguard-rules.pro‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2026 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:tools="http://schemas.android.com/tools">
19+
20+
21+
<application>
22+
<provider
23+
android:name="androidx.startup.InitializationProvider"
24+
android:authorities="${applicationId}.androidx-startup"
25+
android:exported="false"
26+
tools:node="merge">
27+
28+
<meta-data
29+
android:name="com.example.library.utils.attribution.AttributionIdInitializer"
30+
android:value="androidx.startup" />
31+
</provider>
32+
</application>
33+
34+
</manifest>

0 commit comments

Comments
 (0)