Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.34k stars 618 forks source link

Unresolved reference: JsonObject #206

Closed just-kip closed 6 years ago

just-kip commented 6 years ago

Hi, I am trying to implement api client using ktor and serialization in common plugin. I disable ios project to make sure that problem not in ios lib. gradle.wrapper

#Mon Sep 03 12:19:06 SAMT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip

settings.gradle

enableFeaturePreview('GRADLE_METADATA')

include ':lib'
include ':lib:common'
include ':lib:android'
//include ':lib:ios'

My lib project build gradle

buildscript {
    ext.kotlin_version = '1.2.60'
    ext.konan_version = '0.8.2'

    ext.ktor_version = '0.9.4'
    ext.kotlinx_coroutines_version = '0.25.0'
    ext.kotlinx_serialization_version = '0.6.1'

    repositories {
        google()
        jcenter()
        maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
        maven { url "https://kotlin.bintray.com/kotlinx" }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-rc02'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:$konan_version"
        classpath "org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:$kotlinx_serialization_version"
    }
}

subprojects {
    repositories {
        jcenter()
        google()
        maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
        maven { url 'https://kotlin.bintray.com/kotlinx' }
        maven { url 'https://dl.bintray.com/kotlin/ktor' }
    }
}

common module build.gradle

apply plugin: 'kotlin-platform-common'
apply plugin: 'kotlinx-serialization'

group = 'kn.common'
version = 0.1

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
    implementation "io.ktor:ktor-client:$ktor_version"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-jsonparser:$kotlinx_serialization_version"

    testImplementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
    testImplementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
}

kotlin {
    experimental {
        coroutines "enable"
    }
}

android module build.gradle

apply plugin: 'com.android.library'
apply plugin: 'kotlin-platform-android'
apply plugin: 'kotlinx-serialization'

group = 'kn.common'
version = 0.1

android {
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        test.java.srcDirs += 'src/test/kotlin'
    }
}

dependencies {
    expectedBy project(':lib:common')

    // Specify Kotlin/JVM stdlib dependency.
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinx_coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlinx_serialization_version"
    implementation "io.ktor:ktor-client-android:$ktor_version"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}

api.kt in common lib

import io.ktor.client.HttpClient
import io.ktor.client.request.post
import io.ktor.client.response.HttpResponse
import io.ktor.client.response.readText
import io.ktor.content.TextContent
import io.ktor.http.ContentType
import io.ktor.http.URLProtocol
import kotlinx.coroutines.experimental.CoroutineDispatcher
import kotlinx.coroutines.experimental.launch
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

internal expect val ApplicationDispatcher: CoroutineDispatcher

private const val API_URL = CommonConfig.API_URL

class Api {

    private val client = HttpClient()

    fun authByPassword(msisdn: Msisdn, password: String, callback: (String) -> Unit) {
        launch(ApplicationDispatcher) {

            val result = client.post<HttpResponse> {
                url {
                    protocol = URLProtocol.HTTPS
                    host = API_URL
                    encodedPath = "/auth"

                    val jsonBody = JsonObject(mapOf(
                            "msisdn" to JsonPrimitive(msisdn),
                            "password" to JsonPrimitive(password)
                    ))

                    body = TextContent(jsonBody.toString(), ContentType.Application.Json)
                }

            }

            callback(result.readText())
        }
    }
}

./gradlew build fails with following errors

e: ~/kn-app/lib/common/src/main/kotlin/api.kt: (14, 35): Unresolved reference: JsonPrimitive
e: ~/kn-app/lib/common/src/main/kotlin/api.kt: (33, 36): Unresolved reference: JsonObject
e: ~/kn-app/lib/common/src/main/kotlin/api.kt: (13, 35): Unresolved reference: JsonObject
e: ~/kn-app/lib/common/src/main/kotlin/api.kt: (33, 47): Type inference failed: Not enough information to infer parameter V in fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> Please specify it explicitly.
e: ~/kn-app/lib/common/src/main/kotlin/api.kt: (34, 41): Unresolved reference: JsonPrimitive
e: ~/kn-app/lib/common/src/main/kotlin/api.kt: (35, 43): Unresolved reference: JsonPrimitive

I follow this guide https://github.com/Kotlin/kotlinx.serialization/blob/master/json/README.md

What is my mistake?

just-kip commented 6 years ago

I added kotlinx-gradle-serialization-plugin in https://github.com/JetBrains/kotlin-mpp-example and gain same error

greetings/build.gradle

// Set up a buildscript dependency on the Kotlin plugin.
buildscript {
    // Specify a Kotlin version you need.
    ext.kotlin_version = '1.2.60'
    ext.konan_version = '0.8.2'
    ext.kotlinx_serialization_version = '0.6.1'

    repositories {
        google()
        jcenter()
        maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
        maven { url "https://kotlin.bintray.com/kotlinx" }
    }

    // Specify all the plugins used as dependencies
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:$konan_version"
        classpath "org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:$kotlinx_serialization_version"
    }
}

// Set up compilation dependency repositories for all projects.
subprojects {
    repositories {
        jcenter()
        google()
        maven { url 'https://kotlin.bintray.com/kotlinx' }

    }
}

greeting/common build.gradle

apply plugin: 'kotlin-platform-common'
apply plugin: 'kotlinx-serialization'

// Specify a group and a version of the library to access it in Android Studio.
// By default the project directory name is used as an artifact name thus the full dependency
// description will be 'org.greeting:common:1.0'
group = 'org.greeting'
version = 1.0

dependencies {
    // Set up compilation dependency on common Kotlin stdlib
    implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-jsonparser:$kotlinx_serialization_version"

    testImplementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
    testImplementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
}

Seems i can not use kotlinx-serialization-runtime-jsonparser in common module, is not it?

just-kip commented 6 years ago

ok, i found solution in https://github.com/Kotlin/kotlinx.serialization/issues/193

just-kip commented 6 years ago

The solution is: