I have a build.gradle.kts and I'd like to store all the versions there as :
plugins {
`java-platform`
}
javaPlatform {
allowDependencies()
}
dependencies {
val version = object {
val androidx = object {
val appCompat = "1.4.1"
val core = "1.7.0"
}
....
api("whatever:${version.androidx.appCompat}") <-- not working because can not find "version"
}
When trying to use this version. is not working, any idea why?
Related
I want to create a script for publish a library on kotlin.
I am trying to convert this gradle config to gradle-kotlin (kts) following the gradle documentation (https://docs.gradle.org/current/userguide/publishing_maven.html#header) but I have this error when project builds:
FAILURE: Build failed with an exception.
Where:
Script '.../gradle-mvn-push.gradle.kts' line: 18
SourceSet with name 'main' not found.
Here is the script in kts:
Kotlin version : 1.6.10
Kotlin-gradle plugin version : 7.2.1
apply(plugin = "maven-publish")
var gitLabPrivateToken: String? = null
val secretPropertiesFile = rootProject.file("example.properties")
if (secretPropertiesFile.exists()) {
val secretProperties = java.util.Properties().apply {
load(java.io.FileInputStream(secretPropertiesFile))
}
gitLabPrivateToken = secretProperties["token"].toString()
}
val gitlabUrl = "exampleUrl"
lateinit var sourcesArtifact: PublishArtifact
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(project.the<SourceSetContainer>()["main"].allSource)
}
artifacts {
sourcesArtifact = add("archives", sourcesJar)
}
}
afterEvaluate {
configure<PublishingExtension> {
publications {
create<MavenPublication>("maven") {
artifact(sourcesArtifact) // Adds source as separate jar.
groupId = "com.example"
artifactId = "lib"
version = "0.1.0"
from(components["release"])
}
}
repositories {
maven {
name = "GitLabPersonal"
url = uri(gitlabUrl)
credentials(HttpHeaderCredentials::class) {
name = "Private-Token"
value = gitLabPrivateToken
}
authentication {
create<HttpHeaderAuthentication>("header")
}
}
maven {
name = "GitLabCi"
url = uri(gitlabUrl)
credentials(HttpHeaderCredentials::class) {
name = "Job-Token"
value = System.getenv("CI_JOB_TOKEN")
}
authentication {
create<HttpHeaderAuthentication>("header")
}
}
}
}
}
I am trying to use SQLDelight in a Kotlin Multiplatform project for android and iOS. I have followed the documentation found here. The project builds successfully, but I can't access com.squareup.* anywhere inside the shared/iosMain folder. Below are my code files.
Project/build.gradle.kts:
buildscript {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
classpath("com.android.tools.build:gradle:7.0.3")
classpath("com.squareup.sqldelight:gradle-plugin:1.5.2")
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
Project/shared/build.gradle.kts:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
id("com.squareup.sqldelight")
}
version = "1.0"
kotlin {
android()
val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = when {
System.getenv("SDK_NAME")?.startsWith("iphoneos") == true -> ::iosArm64
System.getenv("NATIVE_ARCH")?.startsWith("arm") == true -> ::iosSimulatorArm64
else -> ::iosX64
}
iosTarget("ios") {}
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "14.1"
frameworkName = "shared"
podfile = project.file("../iosApp/Podfile")
}
sourceSets {
val commonMain by getting {
dependencies {
// SQLDelight
implementation("com.squareup.sqldelight:runtime:1.5.2")
implementation ("com.squareup.sqldelight:coroutines-extensions:1.5.0")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.squareup.sqldelight:android-driver:1.5.0")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting {
dependencies {
implementation("com.squareup.sqldelight:native-driver:1.5.2")
}
}
val iosTest by getting
}
}
sqldelight {
database("AppDatabase") {
packageName = "com.example.sam.data.db"
dialect = "sqlite:3.25"
}
}
android {
compileSdkVersion(31)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(21)
targetSdkVersion(31)
}
}
Project/shared/src/commonMain/kotlin/com/example/sam/data/db/DatabaseDriverFactory.kt
package com.example.sam.data.db
import com.squareup.sqldelight.db.SqlDriver
expect class DatabaseDriverFactory {
fun createDriver(): SqlDriver
}
Project/shared/src/androidMain/kotlin/com/example/sam/data/db/DatabaseDriverFactory.kt
package com.example.sam.data.db
import android.content.Context
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver
actual class DatabaseDriverFactory(
private val ctx: Context
) {
actual fun createDriver(): SqlDriver {
return AndroidSqliteDriver(AppDatabase.Schema, ctx, "app.db")
}
}
And the file that I am having issues with - Project/shared/iosMain/kotlin/com/example/sam/data/db/DatabaseDriverFactory.kt
package com.example.sam.data.db
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
actual class DatabaseDriverFactory {
actual fun createDriver(): SqlDriver {
return NativeSqliteDriver(AppDatabase.Schema, "app.db")
}
}
It can't find SqlDriver, NativeSqliteDriver or even com.squareup.*.
I have scoured the web, but didn't find anything that helped.
Comments won't let me post code, but I'd like to try some things.
Try this config and see if the IDE can understand it better. You'll still need to add back some form of the more complex one, but I'd like to reduce the variables as much as possible.
kotlin {
android()
iosX64("ios")
cocoapods {
//etc...
I have a project that I made using KMM plugin on android studio for Android and iOS. The project seems to be running fine.
Now I want to add a watchOS target to the existing ios App and have no clue how to go about this.
My shared build.gradle.kts file
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("com.android.library")
id("com.squareup.sqldelight")
}
android {
compileSdkVersion(30)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(21)
targetSdkVersion(30)
}
configurations {
create("androidTestApi")
create("androidTestDebugApi")
create("androidTestReleaseApi")
create("testApi")
create("testDebugApi")
create("testReleaseApi")
}
}
kotlin {
android()
val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iosTarget("ios") {
binaries {
framework {
baseName = "shared"
}
}
}
val ktorVersion = "1.4.1"
val serializationVersion = "1.0.0-RC"
val sqlDelightVersion: String by project
val coroutinesVersion = "1.3.9-native-mt"
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("io.ktor:ktor-client-android:$ktorVersion")
implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:$ktorVersion")
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val iosTest by getting
}
}
sqldelight {
database("AppDatabase") {
packageName = "com.elixer.paws.shared.cache"
}
}
val packForXcode by tasks.creating(Sync::class) {
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val framework = kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)
val targetDir = File(buildDir, "xcode-frameworks")
group = "build"
dependsOn(framework.linkTask)
inputs.property("mode", mode)
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
Up until now, I just tried to open the ios app in Xcode and creating a new "watchOS" target. This sets up the watch OS app and I am able to share code between the iOS and watchOS but unable to "import shared" and all common files into watch OS.
I get this error when I try to run the project on watchOs
Building for watchOS Simulator, but the linked framework
'shared.framework' was built for iOS Simulator. kotlin project
Thanks for any help. I am not very good with gradle or ios dev. Anything will help at this point.
You need to add a watchOS target in your build.gradle.kts similarly how the iOS target is specified:
iosTarget("ios") {
...
}
watchos()
Also, if you'd like to share some of the implementation in your multiplatform code between the apple targets you may want to use dependsOn, for more information:
Documentation
Example
I have a KMM project and want to use SqlDelight library, but when I build the project database schema not generated and table entities also.
actual class DatabaseDriverFactory(private val context: Context) {
actual fun createDriver(): SqlDriver {
//Unresolved reference: CoreDb
return AndroidSqliteDriver(CoreDb.Schema, context, "test.db")
}
}
I have defined sqldelight folder inside my shared module and also created folder for feature generated kotlin classes as it is configured in gradle.build.kts and also have one *.sq file inside sqldelight folder
sqldelight {
database("CoreDb") {
packageName = "com.example.app.core.database"
sourceFolders = listOf("sqldelight")
dialect = "sqlite:3.24"
}
}
When I run generateSqlDelightInterface task I just see those log
> Task :core:generateAndroidDebugCoreDbInterface NO-SOURCE
> Task :core:generateAndroidReleaseCoreDbInterface NO-SOURCE
> Task :core:generateIosMainCoreDbInterface NO-SOURCE
> Task :core:generateMetadataCommonMainCoreDbInterface NO-SOURCE
> Task :core:generateMetadataMainCoreDbInterface NO-SOURCE
> Task :core:generateSqlDelightInterface UP-TO-DATE
can't register checkAndroidModules
BUILD SUCCESSFUL in 311ms
1:40:36 PM: Task execution finished 'generateSqlDelightInterface'.
Here is my full build.gradle.kts
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("com.android.library")
id("kotlin-android-extensions")
id("koin")
id("com.squareup.sqldelight")
}
repositories {
gradlePluginPortal()
google()
jcenter()
mavenCentral()
maven {
url = uri("https://dl.bintray.com/kotlin/kotlin-eap")
}
maven {
url = uri("https://dl.bintray.com/ekito/koin")
}
}
kotlin {
android()
val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iOSTarget("ios") {
binaries {
framework {
baseName = "core"
}
}
}
val coroutinesVersion = "1.3.9-native-mt"
val ktor_version = "1.4.2"
val serializationVersion = "1.0.0-RC"
val koin_version = "3.0.0-alpha-4"
val sqlDelight = "1.4.4"
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-serialization:$ktor_version")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("com.squareup.sqldelight:runtime:$sqlDelight")
// SqlDelight extension
implementation("com.squareup.sqldelight:coroutines-extensions:$sqlDelight")
// Koin for Kotlin
implementation("org.koin:koin-core:$koin_version")
//shared preferences
implementation("com.russhwolf:multiplatform-settings:0.6.3")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("androidx.core:core-ktx:1.3.2")
implementation("io.ktor:ktor-client-android:$ktor_version")
implementation("com.squareup.sqldelight:android-driver:$sqlDelight")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.12")
}
}
val iosMain by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:$ktor_version")
implementation("com.squareup.sqldelight:native-driver:$sqlDelight")
}
}
val iosTest by getting
}
}
android {
compileSdkVersion(29)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(23)
targetSdkVersion(29)
versionCode = 1
versionName = "1.0"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
}
val packForXcode by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
val framework =
kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
val targetDir = File(buildDir, "xcode-frameworks")
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
sqldelight {
database("CoreDb") {
packageName = "com.example.app.core.database"
sourceFolders = listOf("sqldelight")
dialect = "sqlite:3.24"
}
}
and for top level build.gradle
classpath "com.squareup.sqldelight:gradle-plugin:$sqlDelight"
Update
My project folder structure
root
app
src
...
core //(kmm shared module)
androidMain
com.example.app.core
database
commonMain
com.example.app.core
database
repository
...
sqldelight
iosMain
com.example.app.core
database
It seems that you have your sqldelight folder, where you put your *.sq files, in the wrong place.
It should be put inside commonMain directly and inside sqldelight you must specify your package.
If your package is com.example.kmmtest003.database your folder structure should be:
- commonMain
- sqldelight
- com
- example
- kmmtest003
- database
And here you must put your *.sq files.
OTHER POSSIBLE REASONS
As IgorGanapolsky pointed out, this same issue could be caused also by an incompatible gradle plugin version.
This might help someone in the future...I had created a directory in KMM, and it was named sqldelight.com.package.database
I thought this was a proper package, but realized this was not being caught by SQLDelight, and thus my schema wasn't being generated.
I needed to create the structure as a proper directory structure (and then everything worked):
sqldelight
com
package
database
Alternately, you can change the folder to kotlin if you want the SQL code to be in the same directory with your code:
sqldelight {
database("CoreDb") {
...
sourceFolders = listOf("kotlin")
...
}
}
I am trying to get https://github.com/Kotlin/kotlinx.serialization to work with my android library I am building using kotlin mpp. This is my gradle file
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.multiplatform")
kotlin("plugin.serialization") version "1.3.61"
}
group = "org.greeting"
version = 1.0
android {
compileSdkVersion(27)
defaultConfig {
minSdkVersion(15)
}
buildTypes {
getByName("release") {
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}
dependencies {
// Specify Kotlin/JVM stdlib dependency.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7")
implementation(kotlin("stdlib", org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION)) // or "stdlib-jdk8"
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0") // JVM dependency
testImplementation("junit:junit:4.12")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
androidTestImplementation("junit:junit:4.12")
androidTestImplementation("org.jetbrains.kotlin:kotlin-test")
androidTestImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
kotlin {
android("androidLib")
val buildForDevice = project.findProperty("device") as? Boolean ?: false
val iosTarget = if (buildForDevice) iosArm64("ios") else iosX64("ios")
iosTarget.binaries {
framework {
// Disable bitcode embedding for the simulator build.
if (!buildForDevice) {
embedBitcode("disable")
}
}
}
sourceSets["androidLibMain"].dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0")
}
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.14.0")
}
}
commonTest {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-test-common")
implementation("org.jetbrains.kotlin:kotlin-test-annotations-common")
}
}
}
}
tasks.register("copyFramework") {
val buildType = project.findProperty("kotlin.build.type") as? String ?: "DEBUG"
dependsOn("link${buildType.toLowerCase().capitalize()}FrameworkIos")
doLast {
val srcFile = (kotlin.targets["ios"] as KotlinNativeTarget).binaries.getFramework(buildType).outputFile
val targetDir = project.property("configuration.build.dir")!!
copy {
from(srcFile.parent)
into(targetDir)
include("greeting.framework/**")
include("greeting.framework.dSYM")
}
}
}
tasks.register("iosTest") {
val device = project.findProperty("iosDevice") as? String ?: "iPhone 8"
dependsOn("linkDebugTestIos")
group = JavaBasePlugin.VERIFICATION_GROUP
description = "Runs tests for target 'ios' on an iOS simulator"
doLast {
val binary = (kotlin.targets["ios"] as KotlinNativeTarget).binaries.getTest("DEBUG").outputFile
exec {
commandLine("xcrun", "simctl", "spawn", "--standalone", device, binary.absolutePath)
}
}
}
When I try to run build i get Unresolved reference: serialization on line import kotlinx.serialization.ImplicitReflectionSerializer; But if compile the androidTests the linking works fine. Strangely i have to do sourceSets["androidLibMain"] and can't include it in the sourceSet{} block not sure if this is related. Any ideas on why the build task is not linking my serialization library? Thanks