androidBuildGradleTemplate function
Generates android/build.gradle using the modern plugins {} DSL.
Uses plugins {} at the very top as required by Gradle 7+ — no legacy
buildscript {} or apply plugin blocks which break AGP 8/9+.
Implementation
String androidBuildGradleTemplate(String org, String pluginName) =>
'''
plugins {
id "com.android.library"
// Flutter's build infrastructure applies KGP automatically (built-in Kotlin).
// Do NOT add "org.jetbrains.kotlin.android" here — it triggers a deprecation
// warning and will break in future Flutter versions.
// See: https://docs.flutter.dev/release/breaking-changes/migrate-to-built-in-kotlin
}
// A host that gives plugin projects no Kotlin support (DartNative) gets the
// Kotlin Gradle plugin here. Flutter provides Kotlin itself, so this does
// nothing there. It goes through pluginManager because Flutter reads this
// file for an `apply plugin:` line and would then leave Kotlin to the plugin.
if (extensions.findByName("kotlin") == null) {
pluginManager.apply("org.jetbrains.kotlin.android")
}
group = "$org.$pluginName"
version = "1.0"
android {
namespace = "$org.$pluginName"
compileSdk = ${BuildVersions.androidCompileSdk}
ndkVersion = "${BuildVersions.androidNdk}"
externalNativeBuild {
cmake { path = "../src/CMakeLists.txt" }
}
compileOptions {
sourceCompatibility = JavaVersion.${BuildVersions.androidJavaVersion}
targetCompatibility = JavaVersion.${BuildVersions.androidJavaVersion}
}
kotlinOptions { jvmTarget = "${BuildVersions.androidJvmTarget}" }
buildTypes {
// Flutter's profile variant: without an explicit profile buildType,
// AGP builds the plugin's CMake code with the DEBUG config — the
// whole native library ships at -O0 in profile mode. Inherit release
// so profile builds measure optimized native code.
profile {
initWith release
}
}
defaultConfig {
minSdk = ${BuildVersions.androidMinSdk}
}
sourceSets {
main {
// Do NOT add to java.srcDirs — in AGP 8.x that routes .kt files
// through the Java compiler and causes "Unresolved reference" errors.
kotlin.srcDirs += "\${project.projectDir}/../lib/src/generated/kotlin"
}
}
}
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${BuildVersions.kotlinCoroutines}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:${BuildVersions.kotlinCoroutines}"
}
''';