From 3d3df1beb0c65755ef354dcedd24934116b5c032 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Tue, 18 Aug 2026 15:43:57 -0400 Subject: [PATCH] android: register generated sources through the Variant API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Second Android run failed with AGP 9 refusing the previous fix by name: You cannot add Provider instances to the Android SourceSet API. [...] Instead you should use the Sources interface in the Variant API, in particular SourceDirectories.addGeneratedDirectory AGP cannot tell from a Provider whether the directory holds generated (read-only) or hand-written (read-write) files, which is a distinction the IDE needs. `addGeneratedSourceDirectory` is the supported route and — unlike the plain-path form the error offers as an escape hatch — it carries the task dependency, so Kotlin still cannot compile before the bindings are generated and the APK cannot package a stale .so. Co-Authored-By: Claude Opus 5 (1M context) --- android/app/build.gradle.kts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 656f307..ecf686c 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -187,25 +187,28 @@ android { compose = true } - sourceSets { - getByName("main") { - // The .so and the bindings are build outputs, not checked-in sources. - // Passing the TASK PROVIDERS, not plain paths: Gradle reads each - // task's @OutputDirectory and infers the build ordering itself. A - // bare path would compile Kotlin before the bindings exist, and the - // usual fix — depending on KotlinCompile by type — cannot be written - // here, because AGP 9's built-in Kotlin means that class is not on - // the buildscript classpath. - jniLibs.srcDir(cargoNdkDebug) - java.srcDir(generateBindings) - } - } - packaging { resources.excludes += "/META-INF/{AL2.0,LGPL2.1}" } } +/** + * Register the `.so` and the generated bindings as GENERATED sources. + * + * NOT `sourceSets { ... srcDir(task) }`: AGP 9 rejects a Provider there outright, + * because it cannot tell whether the directory holds generated (read-only) or + * hand-written (read-write) files — a distinction the IDE needs. The Variant API + * is the supported route and, unlike a bare path, `addGeneratedSourceDirectory` + * carries the task dependency, so Kotlin cannot compile before the bindings + * exist and the APK cannot package a stale `.so`. + */ +androidComponents { + onVariants { variant -> + variant.sources.kotlin?.addGeneratedSourceDirectory(generateBindings, UniffiBindgen::outputDir) + variant.sources.jniLibs?.addGeneratedSourceDirectory(cargoNdkDebug, CargoNdkBuild::jniLibsDir) + } +} + dependencies { implementation(libs.androidx.core.ktx) implementation(libs.androidx.activity.compose)