@@ -10,9 +10,12 @@ import com.powersync.compile.JniTarget
1010import kotlin.io.path.absolutePathString
1111
1212plugins {
13+ base // For the clean task
1314 alias(libs.plugins.downloadPlugin)
1415}
1516
17+ val lastKotlinSdkRelease = project.property(" LIBRARY_VERSION" ) as String
18+
1619val sqlite3McVersion = " 2.2.6"
1720val sqlite3BaseVersion = " 3.51.1"
1821val sqlite3ReleaseYear = " 2025"
@@ -41,6 +44,14 @@ val downloadSqlite3MultipleCipherSources by tasks.registering(Download::class) {
4144 overwrite(false )
4245}
4346
47+ val downloadPrebuiltsFromRelease by tasks.registering(Download ::class ) {
48+ val zipFileName = " prebuilts-$lastKotlinSdkRelease .zip"
49+ src(" https://github.com/powersync-ja/powersync-kotlin/releases/download/v$lastKotlinSdkRelease /prebuilt_libraries.zip" )
50+ dest(layout.buildDirectory.dir(" downloads" ).map { it.file(zipFileName) })
51+ onlyIfNewer(true )
52+ overwrite(false )
53+ }
54+
4455val unzipSQLiteSources by tasks.registering(UnzipSqlite ::class ) {
4556 val zip = downloadSQLiteSources.map { it.outputs.files.singleFile }
4657 inputs.file(zip)
@@ -62,6 +73,33 @@ val unzipSqlite3MultipleCipherSources by tasks.registering(UnzipSqlite::class) {
6273 )
6374}
6475
76+ abstract class ExtractPrebuilts : Copy () {
77+ @get:OutputDirectory
78+ abstract val outputDir: DirectoryProperty
79+
80+ init {
81+ into(outputDir)
82+ }
83+ }
84+
85+ val unzipPrebuiltsFromLastRelease by tasks.registering(ExtractPrebuilts ::class ) {
86+ val zip = downloadPrebuiltsFromRelease.map { it.outputs.files.singleFile }
87+
88+ from(zipTree(zip)) {
89+ include(" internal/prebuild-binaries/build/output/**" )
90+
91+ eachFile {
92+ relativePath = RelativePath (
93+ relativePath.isFile,
94+ // Remove segments: [internal, prebuild-binaries, build, output]
95+ * relativePath.segments.drop(4 ).toTypedArray()
96+ )
97+ }
98+ }
99+
100+ outputDir.set(layout.buildDirectory.dir(" downloads/from_last_release" ))
101+ }
102+
65103val prepareAndroidBuild by tasks.registering(Copy ::class ) {
66104 from(unzipSqlite3MultipleCipherSources.flatMap { it.destination }) {
67105 include(
@@ -186,7 +224,6 @@ val compileAll by tasks.registering {
186224 dependsOn(compileJni)
187225}
188226
189- val hasPrebuiltAssets = providers.gradleProperty(" hasPrebuiltAssets" ).map { it.toBooleanStrict() }
190227
191228val nativeSqliteConfiguration by configurations.creating {
192229 isCanBeResolved = false
@@ -202,12 +239,33 @@ val androidBuildSourceConfiguration by configurations.creating {
202239}
203240
204241artifacts {
242+ val hasPrebuiltAssets = providers.gradleProperty(" hasPrebuiltAssets" ).map { it.toBooleanStrict() }
243+ val isInCI = providers.environmentVariable(" CI" ).isPresent
244+
245+ // There are three ways to obtain the static and JNI libraries:
246+ //
247+ // 1. We can download and compile from source. This requires special toolchain dependencies (see readme) and is
248+ // slow.
249+ // 2. We can download compiled binaries from the last GitHub release. We don't want to do this in CI to ensure
250+ // updates to the build are reflected in tests. It's a good fit for builds on developer machines though.
251+ // 3. In CI, we run 1 in a separate step and re-use its outputs across multiple builds. -PhasPrebuiltAssets=true is
252+ // passed for consumers of those artifacts.
205253 if (hasPrebuiltAssets.getOrElse(false )) {
206- // In CI builds, we set hasPrebuiltAssets=true. In that case, contents of build/output have been downloaded from
207- // cache and don't need to be rebuilt.
254+ // Case 3: Re-use downloaded assets.
208255 add(nativeSqliteConfiguration.name, layout.buildDirectory.dir(" output/static" ))
209256 add(jniSqlite3McConfiguration.name, layout.buildDirectory.dir(" output/jni" ))
257+ } else if (! isInCI) {
258+ // Developer machine, case 2. Download from last release.
259+ add(
260+ nativeSqliteConfiguration.name,
261+ unzipPrebuiltsFromLastRelease.flatMap { it.outputDir.dir(" static" ) }
262+ )
263+ add(
264+ jniSqlite3McConfiguration.name,
265+ unzipPrebuiltsFromLastRelease.flatMap { it.outputDir.dir(" jni" ) }
266+ )
210267 } else {
268+ // Case 1, compile from source.
211269 add(nativeSqliteConfiguration.name, compileNative)
212270 add(jniSqlite3McConfiguration.name, compileJni)
213271 }
0 commit comments