I am trying Kotlin Multiplatform on a Mac to devel...
# multiplatform
z
I am trying Kotlin Multiplatform on a Mac to develop an iOS app.  I cannot access UIDevice, I get “unresolved reference: UIDevice”.  Does someone have any idea where the issue could come from? I am maybe missing something in my gradle config. The android studio kotlin plugin has the version 1.3.72. Thanks in advance for your help! Here my gradle files Build.gradle (project)
Copy code
buildscript {
  ext.kotlin_version = '1.3.72'
  repositories {
    google()
    jcenter()
     
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:3.6.3'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"



    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
  }
}

allprojects {
  repositories {
    google()
    jcenter()
     
  }
}
build.gradle(module)
Copy code
plugins {
  kotlin("multiplatform")
}

Kotlin{
iosX64("ios"){
  binaries {
    framework {
      baseName = "SharedCode"
    }
  }
}

jvm("android")


  sourceSets["commonMain"].dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
    implementation("io.ktor:ktor-client-core:1.3.2")
    implementation("io.ktor:ktor-client-json:1.3.2")
    implementation("io.ktor:ktor-client-serialization:1.3.2")
  }

  sourceSets["androidMain"].dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib")
    implementation("io.ktor:ktor-client-android:1.3.2")

  }

  sourceSets["iosMain"].dependencies{
    implementation("org.jetbrains.kotlin:kotlin-stdlib")
    implementation("io.ktor:ktor-client-ios:1.3.2")
    implementation("io.ktor:ktor-client-json-native:1.3.2")
    implementation("io.ktor:ktor-client-serialization-native:1.3.2")

  }
}
p
Are you trying to access it from the common code?
z
Thanks Patrick for your reply. No from the iosMain folder. The code below
Copy code
actual fun platformName(): String {
    return UIDevice.currentDevice.systemName() +
            " " +
            UIDevice.currentDevice.systemVersion
}
p
Do you have access to the stdlib functions in your common code and in the iOS code? I know that for example for JVM you need extra imports for stdlib functions, like stdlib-jdk8 for JVM
z
No I can't actually. For instance I tried to use listOf() in common but it says
unresolved reference
. It's certainly a problem about how I import the dependencies. It was fine before I upgrade to 1.3.72
p
Another angle would be to just invalidate caches and restart. Also restart the computer. Sometimes that fixes issues that weren't issues before.
z
I tried that but it didn't work. I have the same problem on android studio and Intellij
So it's certainly an issue with my settings in the build.gradle
m
it seems the Kotlin plugin couldn't find the native library for iOS SDK in your machine... do you have the XCode installed and setup?
z
Yes I have xcode installed. The thing is, I was able to use kmp with ktor and launch an app in the simulator but since I upgraded to 1.3.72 nothing works.
m
have you tried to add some ios arm target?
z
no I have not as for now I am only planning to run the app on simulator for testing kmp. I can try that though
m
ok, try it, but can you user any other class of native platform in your code? I mean only the UIDevice is not working?
z
No I don't have access to platform any more since the update.
m
hummm, make a test, run the gradle sync again in your project...
it's strange, I think the kotlin gradle plugin is not complete downloaded in your machine, to not be finding its internal modules....
z
yes that's weird. I reinstalled intellij but still the same problem.
m
it's not really about the IDE, but the gradle (the build tool) itself...
p
Yeah you could try reinstalling gradle
z
Yes I'll try that. thanks guys for your help
I reinstalled gradle with the latest version of kotlin gradle plugin but still the same.
m
in your project run the gradle sync
p
As a last resort I would try a downgrade. If that doesn't help there is an issue somewhere else.
z
Yes I think I'll do that as it was working for an earlier version. I don't know where it comes from.
m
I have a multiplatform project with kotlin version 1.3.72 working
so, it's not a problem of the 1.3.72 version...
p
I don't think you can make that conclusion. It might be working for most libraries but not for others, etc
z
The problem is certainly somewhere else but to be honest I do not know what to do now.
m
as the problem presented @ZabGo, the
UIDevice
class is from
UIKit
lib, a platform native lib (from iOS SDK, not thrid-party), that should works with kotlin-native (in a MacOS env) without any other configuration, but the setup of the gradle plugin
z
@magnumrocha so the problem is probably in my setup. Is there anything wrong in my build.gradle you think?
p
But you said it worked before? Can you go back to the previous state?
m
yes @ZabGo, I think it is your setup....
z
yes indeed. I was using the version 1.3.61. Maybe the syntax or the setup changed between 1.3.61 and 1.3.72
m
inside your kotlin configs, after the source folder setup, try to put this:
Copy code
configure([targets.iosX64]) {
    compilations.main.source(sourceSets.iosMain)
}
another thing:
Kotlin{
well, I don't know if gradle is case sensitive, but I set this always as
kotlin { ... }
in lowercase....
but I think this is not the problem...
z
I think I added the capital K when I paste the piece of code.
I tried but I get this error message:
Copy code
Line 38:     configure([targets.iosX64]) {
               ^ Not enough information to infer type variable T

  Line 38:     configure([targets.iosX64]) {
                         ^ Type mismatch: inferred type is Array<???> but (Mutable)Iterable<(???..???)>! was expected

  Line 38:     configure([targets.iosX64]) {
                         ^ Unsupported [Collection literals outside of annotations]

  Line 38:     configure([targets.iosX64]) {
                                  ^ Unresolved reference: iosX64

4 errors
I changed for your code for which resolve the type issue:
Copy code
configure(listOf(targets.iosX64)) {
        compilations.main.source(sourceSets["iosMain"])
    }
but I get that now:
Copy code
* What went wrong:
Script compilation errors:

  Line 38:     configure(listOf(targets.iosX64)) {
                                        ^ Unresolved reference: iosX64

  Line 38:     configure(listOf(targets.iosX64)) {
                                                 ^ Type mismatch: inferred type is () -> [ERROR : Cannot infer type variable TypeVariable(_L)] but Closure<(raw) Any!>! was expected

  Line 39:         compilations.main.source(sourceSets["iosMain"])
                   ^ Unresolved reference: compilations

3 errors
m
oh, it's because you should be using kotlin gradle dsl, the code I put above is using pure gradle in groovy language
z
ah ok
I'll change this
thanks
I updated like that :
Copy code
iosX64("ios") {
        binaries {
            framework {
                baseName = "SharedCode"
            }
        }
    }

    jvm("android")

    sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
    }

    sourceSets["iosMain"].dependencies {

    }

    configure(listOf(targets["ios"])) {
        compilations.getByName("main"){
            source(sourceSets["iosMain"])
        }

    }
And I tried this:
Copy code
iosX64("ios") {
        binaries {
            framework {
                baseName = "SharedCode"
            }
        }

        compilations.getByName("main"){
            source(sourceSets["iosMain"])
        }
    }

    jvm("android")

    sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
    }

    sourceSets["iosMain"].dependencies {

    }
But I still have the same problem
m
please, make a test: https://github.com/magnumrocha/kmp-slate try to compile this project, it's a minimal setup for kmp projects...
z
Thanks @magnumrocha for sharing your project. It did not work. I think something wrong in my environment, but I cannot figure out what exactly. Weirdly enough when I use version 1.3.61 I can see all the dependencies in the gradle menu like uikit and I do not have the error message
The Kotlin/Native distribution used in this build does not provide the standard library.
but I still cannot access uikit.uidevice. I updated xcode to xcode 11.4 (was 11.3 before), tried to use openjdk 14 instead of the embedded one and also to change the kotlin compiler to 1.3.61 but did not change anything.
m
morning... so, I really think the problem is your environment, something is not well ☹️
I recommend you try to install your environment from scratch...
z
Yes I think so too, thank you very much for your help though. have a good day
👍 1
Hi @magnumrocha it's working now. I restarted a new project from scratch with IntelliJ . However, it stopped working with the same issue when I upgraded to version 1.3.72. So I downgraded back to 1.3.71 and I will stay with this version. Thank you again @Patrick and @magnumrocha for your help
👍 2
p
Maybe also open an issue or let JB know another way.
z
@Patrick maybe but I think the issue it's certainly more from my side. I've just started using kotlin and kotlin multiplatform to compare it with React Native and see if we could use it instead. So probably my environment setup that was not correct when I started my project from scratch.
313 Views