Thread
#multiplatform
    a

    Andreas Jost

    2 years ago
    The Task :app:compileDebugUnitTestKotlinAndroid of my MPP throws this error:
    Quotes\app\src\commonTest\kotlin\de\ajo\quotes\SampleTests.kt: (3, 20): Unresolved reference: Test
    It's that line:
    import kotlin.test.Test
    And then the same error with assertTrue But in app's build.gradle:
    commonTest {
        dependencies {
            implementation kotlin('test-common')
            implementation kotlin('test-annotations-common')
        }
    }
    Why is that? Also the following tasks run without errors: :app:compileTestKotlinJs, app:jsTest, :app:jvmTest, :app:allTests, etc. and IntelliJ doesn't mark these lines as unresolved.
    Top-level build.gradle:
    buildscript {
        ext.kotlin_version = '1.3.61'
    
        repositories {
            maven { url "<https://dl.bintray.com/jetbrains/kotlin-native-dependencies>" }
    
            google()
            jcenter()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:4.0.0-alpha09'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            maven { url "<https://kotlin.bintray.com/kotlinx>" }
            maven { url "<https://kotlin.bintray.com/ktor>" }
    
            google()
            jcenter()
            mavenCentral()
        }
    }
    build.gradle of app:
    apply plugin: 'kotlin-multiplatform'
    apply plugin: 'kotlinx-serialization'
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android-extensions'
    
    def ktor_version = '1.3.0'
    def logback_version = '1.2.3'
    
    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.2"
    
        defaultConfig {
            applicationId "de.ajo.quotes"
            minSdkVersion 21
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), '<http://proguard-rules.pro|proguard-rules.pro>'
            }
        }
        buildFeatures {
            compose true
        }
    }
    
    kotlin {
        jvm()
        js {
            browser {
            }
        }
        android()
        iosX64("ios") {
            binaries {
                framework()
            }
        }
        sourceSets {
            commonMain {
                dependencies {
                    implementation kotlin('stdlib-common')
                }
            }
            commonTest {
                dependencies {
                    implementation kotlin('test-common')
                    implementation kotlin('test-annotations-common')
                }
            }
            jvmMain {
                dependsOn commonMain
                dependencies {
                    implementation kotlin('stdlib-jdk8')
                    implementation "io.ktor:ktor-server-netty:$ktor_version"
                    implementation "io.ktor:ktor-html-builder:$ktor_version"
                    implementation "ch.qos.logback:logback-classic:$logback_version"
                }
            }
            jvmTest {
                dependsOn commonTest
                dependencies {
                    implementation kotlin('test')
                    implementation kotlin('test-junit')
                }
            }
            clientCommonMain {
                dependsOn commonMain
            }
            clientCommonTest {
                dependsOn commonTest
            }
            jsMain {
                dependsOn clientCommonMain
                dependencies {
                    implementation kotlin('stdlib-js')
                }
            }
            jsTest {
                dependsOn clientCommonTest
                dependencies {
                    implementation kotlin('test-js')
                }
            }
            androidMain {
                dependsOn clientCommonMain
                dependencies {
                    implementation fileTree(dir: 'libs', include: ['*.jar'])
                    implementation kotlin('stdlib-jdk7')
                    implementation 'androidx.appcompat:appcompat:1.1.0'
                    implementation 'androidx.core:core-ktx:1.1.0'
                    implementation 'androidx.ui:ui-layout:0.1.0-dev02'
                    implementation 'androidx.ui:ui-material:0.1.0-dev02'
                    implementation 'androidx.ui:ui-tooling:0.1.0-dev02'
                }
            }
            androidTest {
                dependsOn clientCommonTest
                dependencies {
                    implementation 'junit:junit:4.12'
                }
            }
            iosMain {
                dependsOn clientCommonMain
            }
            iosTest {
                dependsOn clientCommonTest
            }
        }
    }
    
    dependencies {
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    }
    
    jvmJar {
        dependsOn(jsBrowserWebpack)
        from(new File(jsBrowserWebpack.entry.name, jsBrowserWebpack.outputPath))
    }
    
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
    
    task run(type: JavaExec, dependsOn: [jvmJar]) {
        group = "application"
        main = "de.ajo.quotes.SampleJvmKt"
        classpath(configurations.jvmRuntimeClasspath, jvmJar)
        args = []
    }
    
    // This task attaches native framework built from ios module to Xcode project
    // (see iosApp directory). Don't run this task directly,
    // Xcode runs this task itself during its build process.
    // Before opening the project from iosApp directory in Xcode,
    // make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
    task copyFramework {
        def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
        def target = project.findProperty('kotlin.target') ?: 'ios'
        dependsOn kotlin.targets."$target".binaries.getFramework(buildType).linkTask
    
        doLast {
            def srcFile = kotlin.targets."$target".binaries.getFramework(buildType).outputFile
            def targetDir = getProperty('configuration.build.dir')
            copy {
                from srcFile.parent
                into targetDir
                include 'app.framework/**'
                include 'app.framework.dSYM'
            }
        }
    }
    Kris Wong

    Kris Wong

    2 years ago
    androidTest
    needs
    implementation(kotlin("test"))
                    implementation(kotlin("test-junit"))
    a

    Andreas Jost

    2 years ago
    You are correct, I could have seen that. Thanks very much! IntelliJ marks everything red
    'implementation' cannot be applied to '(java.lang.String)'
    , that's what makes it pretty confusing, because it obviously works. Also
    'dependsOn' cannot be applied to '(org.gradle.api.tasks.SourceSet)'
    , but it seems to work, too. Now the Android app runs. if I start the "run" task, that is specified in app/build.gradle the JVM server starts, but the JS code isn't executed on the webpage 😕 I'm using the JVM and JS code from the JVM/JS sample project. It prints:
    Hello from JVM from Ktor. Check me value: 42
    Loading...
    But instead of "Loading..." there should be the JS response String
    Kris Wong

    Kris Wong

    2 years ago
    you will have a much nicer experience if you migrate your build files to Kotlin DSL
    a

    Andreas Jost

    2 years ago
    When I started with MPP a few months ago I had problems with build.gradle.kts, so I went back to Groovy. But I'll try again 🙂 The JS/JVM sample project also uses Groovy, so I started from there, this time. Don't know how the JS part broke now.
    I read more about Kotlin/JS and actually found out the issue, but don't know how to fix it: Inspecting the console of the website, it prints:
    The script from "<http://127.0.0.1:8080/static/Quotes-app.js>" was loaded even though its MIME type ("") is not a valid JavaScript MIME type.
    Loading failed for the <script> with source "<http://127.0.0.1:8080/static/Quotes-app.js>".
    Now if I look in the build directory (
    Quotes/app/build
    ), I'd expect the JS file somewhere. The problem is, that the JS output does't land in
    Quotes/app/build
    , where the rest of the MPP lands, but in
    Quotes/build
    . So it's no surprise, that Ktor server can't find the file and can't send it to the client. Two questions: How to achieve, that all files land in the same build directory? And why doesn't Ktor throw an exception, if the resource is not available?
    Ivan Kubyshkin [JetBrains]

    Ivan Kubyshkin [JetBrains]

    2 years ago
    Can you attach a link to your project on github?
    a

    Andreas Jost

    2 years ago
    I just uploaded it on GitHub: https://github.com/anjo96/Quotes
    Ivan Kubyshkin [JetBrains]

    Ivan Kubyshkin [JetBrains]

    2 years ago
    Can you create an issue - http://kotl.in/issue?
    a

    Andreas Jost

    2 years ago
    Is it understandable? It's my first issue 😅 https://youtrack.jetbrains.com/issue/KT-36659?project=kt I hope there is a workaround for that.