Skip to content

Commit 363817b

Browse files
committed
Add Kotlin Gradle script example to Android README section
1 parent c72d9dc commit 363817b

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

README.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ component must be included in your app's build to support `rustls-platform-verif
118118
#### Gradle Setup
119119

120120
`rustls-platform-verifier` bundles the required native components in the crate, but the project must be setup to locate them
121-
automatically and correctly. These steps assume you are using `.gradle` Groovy files because they're the most common, but everything
122-
is 100% applicable to Kotlin script (`.gradle.kts`) configurations too with a few replacements.
121+
automatically and correctly. These steps assume you are using `.gradle` Groovy files because they're the most common, but if you are using
122+
Kotlin scripts (`.gradle.kts`) for configuration instead, an example snippet is included towards the end of this section.
123123

124124
Inside of your project's `build.gradle` file, add the following code and Maven repository definition. If applicable, this should only be the one "app" sub-project that
125125
will actually be using this crate at runtime. With multiple projects running this, your Gradle configuration performance may degrade.
@@ -143,7 +143,7 @@ repositories {
143143
String findRustlsPlatformVerifierProject() {
144144
def dependencyText = providers.exec {
145145
it.workingDir = new File("../")
146-
commandLine("cargo", "metadata", "--format-version", "1", "--manifest-path", "$PATH_TO_DEPENDENT_CRATE/Cargo.toml")
146+
commandLine("cargo", "metadata", "--format-version", "1", "--filter-platform", "aarch64-linux-android", "--manifest-path", "$PATH_TO_DEPENDENT_CRATE/Cargo.toml")
147147
}.standardOutput.asText.get()
148148
149149
def dependencyJson = new JsonSlurper().parseText(dependencyText)
@@ -163,6 +163,67 @@ new releases of `rustls-platform-verifier` are published. If you only use publis
163163
These script snippets can be tweaked as best suits your project, but the `cargo metadata` invocation must be included so that the Android
164164
implementation part can be located on-disk.
165165

166+
##### Kotlin and Gradle
167+
168+
<details>
169+
<summary>Kotlin script example</summary>
170+
171+
`build.gradle.kts`:
172+
```kotlin
173+
import kotlinx.serialization.decodeFromString
174+
import kotlinx.serialization.json.Json
175+
import kotlinx.serialization.json.JsonObject
176+
import kotlinx.serialization.json.jsonArray
177+
import kotlinx.serialization.json.jsonObject
178+
import kotlinx.serialization.json.jsonPrimitive
179+
180+
buildscript {
181+
dependencies {
182+
classpath(libs.kotlinx.serialization.json)
183+
}
184+
}
185+
186+
repositories {
187+
rustlsPlatformVerifier()
188+
}
189+
190+
fun RepositoryHandler.rustlsPlatformVerifier(): MavenArtifactRepository {
191+
@Suppress("UnstableApiUsage")
192+
val manifestPath = let {
193+
val dependencyJson = providers.exec {
194+
workingDir = File(project.rootDir, "../")
195+
commandLine("cargo", "metadata", "--format-version", "1", "--filter-platform", "aarch64-linux-android", "--manifest-path", "$PATH_TO_DEPENDENT_CRATE/Cargo.toml")
196+
}.standardOutput.asText
197+
198+
val path = Json.decodeFromString<JsonObject>(dependencyJson.get())
199+
.getValue("packages")
200+
.jsonArray
201+
.first { element ->
202+
element.jsonObject.getValue("name").jsonPrimitive.content == "rustls-platform-verifier-android"
203+
}.jsonObject.getValue("manifest_path").jsonPrimitive.content
204+
205+
File(path)
206+
}
207+
208+
return maven {
209+
url = uri(File(manifestPath.parentFile, "maven").path)
210+
metadataSources.artifact()
211+
}
212+
}
213+
214+
dependencies {
215+
// `rustls-platform-verifier` is a Rust crate, but it also has a Kotlin component.
216+
implementation(libs.rustls.platform.verifier)
217+
}
218+
```
219+
220+
`libs.version.toml`:
221+
```toml
222+
# We always use the latest release because `cargo` keeps it in sync with the associated Rust crate's version.
223+
rustls-platform-verifier = { group = "rustls", name = "rustls-platform-verifier", version = "latest.release" }
224+
```
225+
</details>
226+
166227
#### Proguard
167228

168229
If your Android application makes use of Proguard for optimizations, its important to make sure that the Android verifier component isn't optimized

0 commit comments

Comments
 (0)