Skip to content

Commit 2299861

Browse files
committed
Pre: Allow to set NDK version in tests; test multiple NDK versions.
1 parent 7d8dea6 commit 2299861

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.nishtahir
2+
3+
import org.gradle.testkit.runner.BuildResult
4+
import org.gradle.testkit.runner.TaskOutcome
5+
import spock.lang.Unroll
6+
7+
import com.nishtahir.Versions
8+
9+
class NdkVersionTest extends AbstractTest {
10+
@Unroll
11+
def "cargoBuild works with Android NDK version #ndkVersion"() {
12+
given:
13+
def androidVersion = TestVersions.latestAndroidVersionForCurrentJDK()
14+
def target = "x86_64"
15+
def location = "android/x86_64/librust.so"
16+
17+
SimpleAndroidApp.builder(temporaryFolder.root, cacheDir)
18+
.withAndroidVersion(androidVersion)
19+
.withNdkVersion(ndkVersion)
20+
.withKotlinDisabled()
21+
// TODO: .withCargo(...)
22+
.build()
23+
.writeProject()
24+
25+
SimpleCargoProject.builder(temporaryFolder.root)
26+
.withTargets([target])
27+
.build()
28+
.writeProject()
29+
30+
when:
31+
BuildResult buildResult = withGradleVersion(TestVersions.latestSupportedGradleVersionFor(androidVersion).version)
32+
.withProjectDir(temporaryFolder.root)
33+
.withArguments('cargoBuild', '--info', '--stacktrace')
34+
// .withDebug(true)
35+
.build()
36+
37+
// To ease debugging.
38+
temporaryFolder.root.eachFileRecurse {
39+
println(it)
40+
}
41+
42+
then:
43+
buildResult.task(':app:cargoBuild').outcome == TaskOutcome.SUCCESS
44+
buildResult.task(':library:cargoBuild').outcome == TaskOutcome.SUCCESS
45+
new File(temporaryFolder.root, "app/build/rustJniLibs/${location}").exists()
46+
new File(temporaryFolder.root, "library/build/rustJniLibs/${location}").exists()
47+
48+
where:
49+
ndkVersion << [
50+
// NDK versions supported by Github Actions, per
51+
// https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2004-Readme.md.
52+
"21.4.7075529",
53+
"22.1.7171670",
54+
"23.1.7779620",
55+
]
56+
}
57+
}

plugin/src/test/groovy/com/nishtahir/SimpleAndroidApp.groovy

+21-5
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ class SimpleAndroidApp {
99
final File projectDir
1010
private final File cacheDir
1111
final VersionNumber androidVersion
12+
final VersionNumber ndkVersion
1213
final VersionNumber kotlinVersion
1314
private final boolean kotlinEnabled
1415
private final boolean kaptWorkersEnabled
1516

16-
private SimpleAndroidApp(File projectDir, File cacheDir, VersionNumber androidVersion, VersionNumber kotlinVersion, boolean kotlinEnabled, boolean kaptWorkersEnabled) {
17+
private SimpleAndroidApp(File projectDir, File cacheDir, VersionNumber androidVersion, VersionNumber ndkVersion, VersionNumber kotlinVersion, boolean kotlinEnabled, boolean kaptWorkersEnabled) {
1718
this.projectDir = projectDir
1819
this.cacheDir = cacheDir
1920
this.androidVersion = androidVersion
21+
this.ndkVersion = ndkVersion
2022
this.kotlinVersion = kotlinVersion
2123
this.kotlinEnabled = kotlinEnabled
2224
this.kaptWorkersEnabled = kaptWorkersEnabled
@@ -136,7 +138,7 @@ class SimpleAndroidApp {
136138
}
137139
138140
android {
139-
${ndkVersion}
141+
${maybeNdkVersion}
140142
compileSdkVersion 28
141143
buildToolsVersion "29.0.3"
142144
defaultConfig {
@@ -151,10 +153,10 @@ class SimpleAndroidApp {
151153
""".stripIndent()
152154
}
153155

154-
private String getNdkVersion() {
156+
private String getMaybeNdkVersion() {
155157
def isAndroid34x = androidVersion >= android("3.4.0")
156158
if (isAndroid34x) {
157-
return """ndkVersion '21.4.7075529'"""
159+
return """ndkVersion '${ndkVersion}'"""
158160
} else {
159161
return ""
160162
}
@@ -271,6 +273,8 @@ class SimpleAndroidApp {
271273
boolean kaptWorkersEnabled = true
272274

273275
VersionNumber androidVersion = Versions.latestAndroidVersion()
276+
VersionNumber ndkVersion = Versions.latestAndroidVersion() >= android("3.4.0") ? VersionNumber.parse("21.4.7075529") : null
277+
274278
VersionNumber kotlinVersion = VersionNumber.parse("1.3.72")
275279
File projectDir
276280
File cacheDir
@@ -297,13 +301,25 @@ class SimpleAndroidApp {
297301

298302
Builder withAndroidVersion(VersionNumber androidVersion) {
299303
this.androidVersion = androidVersion
304+
if (this.androidVersion < android("3.4.0")) {
305+
this.ndkVersion = null
306+
}
300307
return this
301308
}
302309

303310
Builder withAndroidVersion(String androidVersion) {
304311
return withAndroidVersion(android(androidVersion))
305312
}
306313

314+
Builder withNdkVersion(VersionNumber ndkVersion) {
315+
this.ndkVersion = ndkVersion
316+
return this
317+
}
318+
319+
Builder withNdkVersion(String ndkVersion) {
320+
return withNdkVersion(VersionNumber.parse(ndkVersion))
321+
}
322+
307323
Builder withProjectDir(File projectDir) {
308324
this.projectDir = projectDir
309325
return this
@@ -315,7 +331,7 @@ class SimpleAndroidApp {
315331
}
316332

317333
SimpleAndroidApp build() {
318-
return new SimpleAndroidApp(projectDir, cacheDir, androidVersion, kotlinVersion, kotlinEnabled, kaptWorkersEnabled)
334+
return new SimpleAndroidApp(projectDir, cacheDir, androidVersion, ndkVersion, kotlinVersion, kotlinEnabled, kaptWorkersEnabled)
319335
}
320336
}
321337
}

0 commit comments

Comments
 (0)