Skip to content

Commit 74e5103

Browse files
committed
refactor(snippets): use version catalog in documentation snippets and update Places SDK
- Update dependencies in snippets (app-compose, app-utils, app-places-ktx) to use the Gradle version catalog (libs.*), resolving UseTomlInstead lint findings flagged by code scanning. - Add documentation comments within region tags showing both the recommended version catalog configuration (TOML block) and the standalone coordinate declaration for non-catalog projects. - Replace deprecated places-ktx dependency with core Places SDK (libs.places), as Kotlin extensions and coroutines are built directly into the Places SDK. - Add mapsUtils to gradle/libs.versions.toml. - Add missing androidTest dependencies (ext.junit, espresso.idling.resource) to ApiDemos:kotlin-app and update LatLngSubject / LatLngBoundsSubject custom Truth subject factories for Kotlin 2.4 / Truth 1.4.5 compatibility. - Add .kotlin/ compiler directory to .gitignore.
1 parent eaa0655 commit 74e5103

8 files changed

Lines changed: 71 additions & 11 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ secrets.properties
99

1010
# This covers new IDEs, like Antigravity
1111
.vscode/
12-
**/bin/
12+
**/bin/
13+
.kotlin/

ApiDemos/project/kotlin-app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ dependencies {
9393

9494
// Tests
9595
testImplementation(libs.junit)
96+
androidTestImplementation(libs.ext.junit)
97+
androidTestImplementation(libs.espresso.idling.resource)
9698
androidTestImplementation(libs.junit)
9799
androidTestImplementation(libs.espresso.core)
98100
androidTestImplementation(libs.truth)

ApiDemos/project/kotlin-app/src/androidTest/java/com/example/kotlindemos/truth/LatLngBoundsSubject.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ import com.google.common.truth.Truth
2626
*/
2727
class LatLngBoundsSubject private constructor(
2828
failureMetadata: FailureMetadata,
29-
private val actual: LatLngBounds
29+
private val actual: LatLngBounds?
3030
) : Subject(failureMetadata, actual) {
3131

3232
/**
3333
* Asserts that the given [LatLng] is contained within the actual bounds, allowing for
3434
* a small tolerance to account for floating-point inaccuracies.
3535
*/
3636
fun containsWithTolerance(expected: LatLng) {
37+
if (actual == null) {
38+
failWithActual("expected to contain", expected)
39+
return
40+
}
3741
val tolerance = 1e-5
3842
val contains = actual.southwest.latitude - tolerance <= expected.latitude &&
3943
expected.latitude <= actual.northeast.latitude + tolerance &&
@@ -60,11 +64,13 @@ class LatLngBoundsSubject private constructor(
6064
}
6165

6266
companion object {
67+
fun latLngBounds(): Factory<LatLngBoundsSubject, LatLngBounds> = Factory(::LatLngBoundsSubject)
68+
6369
/**
6470
* Creates a [LatLngBoundsSubject] for asserting on the given [LatLngBounds].
6571
*/
66-
fun assertThat(actual: LatLngBounds): LatLngBoundsSubject {
67-
return Truth.assertAbout(::LatLngBoundsSubject).that(actual)
72+
fun assertThat(actual: LatLngBounds?): LatLngBoundsSubject {
73+
return Truth.assertAbout(latLngBounds()).that(actual)
6874
}
6975
}
7076
}

ApiDemos/project/kotlin-app/src/androidTest/java/com/example/kotlindemos/truth/LatLngSubject.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ import kotlin.math.abs
3232
*/
3333
class LatLngSubject private constructor(
3434
failureMetadata: FailureMetadata,
35-
private val actual: LatLng
35+
private val actual: LatLng?
3636
) : Subject(failureMetadata, actual) {
3737

3838
/**
3939
* Asserts that the actual [LatLng] is within a small tolerance of the expected value.
4040
* This is useful for accounting for floating-point inaccuracies.
4141
*/
4242
fun isNear(expected: LatLng) {
43+
if (actual == null) {
44+
failWithActual("expected to be near", expected)
45+
return
46+
}
4347
val tolerance = 1e-6
4448
if (abs(actual.latitude - expected.latitude) > tolerance ||
4549
abs(actual.longitude - expected.longitude) > tolerance
@@ -63,6 +67,10 @@ class LatLngSubject private constructor(
6367
* Asserts that the actual [LatLng] is within [tolerance] meters of the [expected] [LatLng].
6468
*/
6569
fun of(expected: LatLng) {
70+
if (actual == null) {
71+
failWithActual("expected to be within $tolerance meters of $expected", expected)
72+
return
73+
}
6674
val distance = SphericalUtil.computeDistanceBetween(actual, expected)
6775
if (distance > tolerance) {
6876
failWithActual("expected to be within $tolerance meters of $expected [was $distance meters away]", expected)
@@ -71,11 +79,13 @@ class LatLngSubject private constructor(
7179
}
7280

7381
companion object {
82+
fun latLngs(): Factory<LatLngSubject, LatLng> = Factory(::LatLngSubject)
83+
7484
/**
7585
* Creates a [LatLngSubject] for asserting on the given [LatLng].
7686
*/
77-
fun assertThat(actual: LatLng): LatLngSubject {
78-
return Truth.assertAbout(::LatLngSubject).that(actual)
87+
fun assertThat(actual: LatLng?): LatLngSubject {
88+
return Truth.assertAbout(latLngs()).that(actual)
7989
}
8090
}
8191
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ materialIconsExtended = "1.7.8"
3838
# Google Maps & Places
3939
mapsCompose = "8.4.0"
4040
mapsKtx = "6.3.0"
41+
mapsUtils = "5.1.1"
4142
places = "5.3.0"
4243
playServicesMaps = "20.0.0"
4344
secretsGradlePlugin = "2.0.1"
@@ -102,6 +103,7 @@ ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", versio
102103
# Maps & Places
103104
maps-compose = { module = "com.google.maps.android:maps-compose", version.ref = "mapsCompose" }
104105
maps-ktx = { group = "com.google.maps.android", name = "maps-ktx", version.ref = "mapsKtx" }
106+
maps-utils = { module = "com.google.maps.android:android-maps-utils", version.ref = "mapsUtils" }
105107
maps-utils-ktx = { group = "com.google.maps.android", name = "maps-utils-ktx", version.ref = "mapsKtx" }
106108
places = { group = "com.google.android.libraries.places", name = "places", version.ref = "places" }
107109
play-services-maps = { group = "com.google.android.gms", name = "play-services-maps", version.ref = "playServicesMaps" }

snippets/app-compose/build.gradle.kts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,21 @@ dependencies {
8585
implementation(libs.lifecycle.runtime.ktx)
8686
// [END_EXCLUDE]
8787

88+
// Modern Android projects use version catalogs to manage dependencies. To include the Maps Compose library,
89+
// first add the following to your gradle/libs.versions.toml file:
90+
//
91+
// [versions]
92+
// mapsCompose = "8.4.0"
93+
//
94+
// [libraries]
95+
// maps-compose = { module = "com.google.maps.android:maps-compose", version.ref = "mapsCompose" }
96+
//
8897
// Android Maps Compose composables for the Maps SDK for Android
89-
implementation("com.google.maps.android:maps-compose:8.4.0")
98+
implementation(libs.maps.compose)
99+
100+
// If your project does not use a version catalog, you can use the following dependency instead:
101+
//
102+
// implementation("com.google.maps.android:maps-compose:8.4.0")
90103
}
91104
// [END maps_android_compose_dependency]
92105

snippets/app-places-ktx/build.gradle.kts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,21 @@ dependencies {
8080
androidTestImplementation(libs.espresso.core)
8181
// [END_EXCLUDE]
8282

83-
// KTX for the Places SDK for Android library
84-
implementation("com.google.maps.android:places-ktx:5.0.0")
83+
// Modern Android projects use version catalogs to manage dependencies. To include the Places SDK for Android,
84+
// first add the following to your gradle/libs.versions.toml file:
85+
//
86+
// [versions]
87+
// places = "5.3.0"
88+
//
89+
// [libraries]
90+
// places = { group = "com.google.android.libraries.places", name = "places", version.ref = "places" }
91+
//
92+
// Places SDK for Android (Kotlin extensions and coroutines are built directly into the Places SDK)
93+
implementation(libs.places)
94+
95+
// If your project does not use a version catalog, you can use the following dependency instead:
96+
//
97+
// implementation("com.google.android.libraries.places:places:5.3.0")
8598
}
8699
// [END places_android_ktx_install_snippet]
87100

snippets/app-utils/build.gradle.kts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,23 @@ dependencies {
7979
androidTestImplementation(libs.espresso.core)
8080
// [END_EXCLUDE]
8181

82+
// Modern Android projects use version catalogs to manage dependencies. To include the Maps Utility library,
83+
// first add the following to your gradle/libs.versions.toml file:
84+
//
85+
// [versions]
86+
// mapsUtils = "5.1.1"
87+
//
88+
// [libraries]
89+
// maps-utils = { module = "com.google.maps.android:android-maps-utils", version.ref = "mapsUtils" }
90+
//
8291
// Utility Library for Maps SDK for Android
8392
// You do not need to add a separate dependency for the Maps SDK for Android
8493
// since this library builds in the compatible version of the Maps SDK.
85-
implementation("com.google.maps.android:android-maps-utils:5.1.1")
94+
implementation(libs.maps.utils)
95+
96+
// If your project does not use a version catalog, you can use the following dependency instead:
97+
//
98+
// implementation("com.google.maps.android:android-maps-utils:5.1.1")
8699
}
87100
// [END maps_android_utils_install_snippet]
88101

0 commit comments

Comments
 (0)