-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: Integrate AttributionId Flow #2386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LoyalAbbas
wants to merge
3
commits into
main
Choose a base branch
from
startup_runtime_integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
|
|
||
| /* | ||
| * Copyright 2026 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. | ||
| */ | ||
|
|
||
| plugins { | ||
| alias(libs.plugins.android.library) | ||
| alias(libs.plugins.kotlin.android) | ||
| } | ||
|
|
||
| android { | ||
| namespace = "com.example.library" | ||
| compileSdk = libs.versions.compileSdk.get().toInt() | ||
|
|
||
| defaultConfig { | ||
| minSdk = libs.versions.minSdk.get().toInt() | ||
| testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
| consumerProguardFiles("consumer-rules.pro") | ||
| } | ||
| buildTypes { | ||
| release { | ||
| isMinifyEnabled = false | ||
| proguardFiles( | ||
| getDefaultProguardFile("proguard-android-optimize.txt"), | ||
| "proguard-rules.pro" | ||
| ) | ||
| } | ||
| } | ||
| lint { | ||
| abortOnError = false | ||
| } | ||
| compileOptions { | ||
| sourceCompatibility = JavaVersion.VERSION_17 | ||
| targetCompatibility = JavaVersion.VERSION_17 | ||
| } | ||
| lint { | ||
| disable += setOf("MissingInflatedId", "OnClick") | ||
| sarifOutput = layout.buildDirectory.file("reports/lint-results-debug.sarif").get().asFile | ||
| } | ||
| kotlin { | ||
| compilerOptions { | ||
| jvmTarget.set(JvmTarget.JVM_17) | ||
| javaParameters.set(true) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(libs.appcompat) | ||
| implementation(libs.core.ktx) | ||
| implementation(libs.material) | ||
| implementation(libs.startup.runtime) | ||
| implementation(libs.play.services.maps) | ||
| testImplementation(libs.junit) | ||
| testImplementation(libs.robolectric) | ||
| testImplementation(libs.mockk) | ||
| testImplementation(libs.espresso.core) | ||
| } | ||
|
|
||
|
|
||
| abstract class GenerateArtifactIdTask : DefaultTask() { | ||
| @get:OutputDirectory | ||
| abstract val outputDir: DirectoryProperty | ||
|
|
||
| @get:Input | ||
| abstract val version: Property<String> | ||
|
|
||
| @TaskAction | ||
| fun generate() { | ||
| val dir = outputDir.get().asFile | ||
| val packageName = "com.example.library.utils.meta" | ||
| val packagePath = packageName.replace('.', '/') | ||
| val outputFile = File(dir, "$packagePath/ArtifactId.kt") | ||
| outputFile.parentFile.mkdirs() | ||
| val attributionId = "gmp_git_androidsamples_v${version.get()}" | ||
| outputFile.writeText( | ||
| """ | ||
| package $packageName | ||
|
|
||
| /** | ||
| * Automatically generated object containing the library's attribution ID. | ||
| * This is used to track library usage for analytics. | ||
| */ | ||
| public object AttributionId { | ||
| public const val VALUE: String = "$attributionId" | ||
| } | ||
| """.trimIndent() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| val generateArtifactIdFile = tasks.register<GenerateArtifactIdTask>("generateArtifactIdFile") { | ||
| outputDir.set(layout.buildDirectory.dir("generated/source/artifactId")) | ||
| version.set(libs.versions.versionName.get()) | ||
| } | ||
|
|
||
| androidComponents { | ||
| onVariants { variant -> | ||
| variant.sources.java?.addGeneratedSourceDirectory( | ||
| generateArtifactIdFile, | ||
| GenerateArtifactIdTask::outputDir | ||
| ) | ||
| } | ||
| } |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Add project specific ProGuard rules here. | ||
| # You can control the set of applied configuration files using the | ||
| # proguardFiles setting in build.gradle. | ||
| # | ||
| # For more details, see | ||
| # http://developer.android.com/guide/developing/tools/proguard.html | ||
|
|
||
| # If your project uses WebView with JS, uncomment the following | ||
| # and specify the fully qualified class name to the JavaScript interface | ||
| # class: | ||
| #-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
| # public *; | ||
| #} | ||
|
|
||
| # Uncomment this to preserve the line number information for | ||
| # debugging stack traces. | ||
| #-keepattributes SourceFile,LineNumberTable | ||
|
|
||
| # If you keep the line number information, uncomment this to | ||
| # hide the original source file name. | ||
| #-renamesourcefileattribute SourceFile | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| Copyright 2026 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 | ||
|
|
||
| https://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. | ||
| --> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools"> | ||
|
|
||
|
|
||
| <application> | ||
| <provider | ||
| android:name="androidx.startup.InitializationProvider" | ||
| android:authorities="${applicationId}.androidx-startup" | ||
| android:exported="false" | ||
| tools:node="merge"> | ||
|
|
||
| <meta-data | ||
| android:name="com.example.library.utils.attribution.AttributionIdInitializer" | ||
| android:value="androidx.startup" /> | ||
| </provider> | ||
| </application> | ||
|
|
||
| </manifest> |
41 changes: 41 additions & 0 deletions
41
library/src/main/java/com/example/library/utils/attribution/AttributionIdInitializer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright 2026 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. | ||
| */ | ||
| package com.example.library.utils.attribution | ||
|
|
||
| import android.content.Context | ||
| import androidx.annotation.Keep | ||
| import androidx.startup.Initializer | ||
| import com.example.library.utils.meta.AttributionId | ||
| import com.google.android.gms.maps.MapsApiSettings | ||
|
|
||
| /** | ||
| * Adds a usage attribution ID to the initializer, which helps Google understand which libraries | ||
| * and samples are helpful to developers, such as usage of this library. | ||
| * To opt out of sending the usage attribution ID, please remove this initializer from your manifest. | ||
| */ | ||
| @Keep | ||
| internal class AttributionIdInitializer : Initializer<Unit> { | ||
| override fun create(context: Context) { | ||
| MapsApiSettings.addInternalUsageAttributionId( | ||
| // context = | ||
| context, | ||
| // internalUsageAttributionId = | ||
| AttributionId.VALUE, | ||
| ) | ||
| } | ||
|
|
||
| override fun dependencies(): List<Class<out Initializer<*>>> = emptyList() | ||
| } |
62 changes: 62 additions & 0 deletions
62
library/src/test/java/com/example/library/utils/AttributionIdInitializerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright 2026 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. | ||
| */ | ||
| package com.example.library.utils | ||
|
|
||
| import android.content.Context | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import com.example.library.utils.attribution.AttributionIdInitializer | ||
| import com.example.library.utils.meta.AttributionId | ||
| import com.google.android.gms.maps.MapsApiSettings | ||
| import io.mockk.every | ||
| import io.mockk.just | ||
| import io.mockk.mockkStatic | ||
| import io.mockk.runs | ||
| import io.mockk.unmockkStatic | ||
| import io.mockk.verify | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class AttributionIdInitializerTest { | ||
| @Before | ||
| fun setUp() { | ||
| mockkStatic(MapsApiSettings::class) | ||
| every { MapsApiSettings.addInternalUsageAttributionId(any(), any()) } just runs | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| unmockkStatic(MapsApiSettings::class) | ||
| } | ||
|
|
||
| @Test | ||
| fun `create adds internal usage attribution id`() { | ||
| val context = ApplicationProvider.getApplicationContext<Context>() | ||
| val initializer = AttributionIdInitializer() | ||
|
|
||
| initializer.create(context) | ||
|
|
||
| verify { | ||
| MapsApiSettings.addInternalUsageAttributionId( | ||
| context, | ||
| AttributionId.VALUE, | ||
| ) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing trailing newline.