https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
z

ZabGo

04/27/2020, 3:09 PM
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

Patrick

04/27/2020, 3:22 PM
Are you trying to access it from the common code?
z

ZabGo

04/27/2020, 3:26 PM
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

Patrick

04/28/2020, 8:45 AM
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

ZabGo

04/28/2020, 8:54 AM
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

Patrick

04/28/2020, 8:56 AM
Another angle would be to just invalidate caches and restart. Also restart the computer. Sometimes that fixes issues that weren't issues before.
z

ZabGo

04/28/2020, 8:57 AM
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

magnumrocha

04/28/2020, 9:01 AM
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

ZabGo

04/28/2020, 9:05 AM
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

magnumrocha

04/28/2020, 9:07 AM
have you tried to add some ios arm target?
z

ZabGo

04/28/2020, 9:18 AM
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

magnumrocha

04/28/2020, 9:19 AM
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

ZabGo

04/28/2020, 9:29 AM
No I don't have access to platform any more since the update.
m

magnumrocha

04/28/2020, 9:31 AM
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

ZabGo

04/28/2020, 9:34 AM
yes that's weird. I reinstalled intellij but still the same problem.
m

magnumrocha

04/28/2020, 9:35 AM
it's not really about the IDE, but the gradle (the build tool) itself...
p

Patrick

04/28/2020, 9:36 AM
Yeah you could try reinstalling gradle
z

ZabGo

04/28/2020, 9:38 AM
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

magnumrocha

04/28/2020, 10:07 AM
in your project run the gradle sync
p

Patrick

04/28/2020, 10:15 AM
As a last resort I would try a downgrade. If that doesn't help there is an issue somewhere else.
z

ZabGo

04/28/2020, 10:18 AM
Yes I think I'll do that as it was working for an earlier version. I don't know where it comes from.
m

magnumrocha

04/28/2020, 10:18 AM
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

Patrick

04/28/2020, 10:23 AM
I don't think you can make that conclusion. It might be working for most libraries but not for others, etc
z

ZabGo

04/28/2020, 10:23 AM
The problem is certainly somewhere else but to be honest I do not know what to do now.
m

magnumrocha

04/28/2020, 10:29 AM
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

ZabGo

04/28/2020, 10:39 AM
@magnumrocha so the problem is probably in my setup. Is there anything wrong in my build.gradle you think?
p

Patrick

04/28/2020, 10:40 AM
But you said it worked before? Can you go back to the previous state?
m

magnumrocha

04/28/2020, 10:42 AM
yes @ZabGo, I think it is your setup....
z

ZabGo

04/28/2020, 10:42 AM
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

magnumrocha

04/28/2020, 10:55 AM
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

ZabGo

04/28/2020, 11:22 AM
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

magnumrocha

04/28/2020, 11:29 AM
oh, it's because you should be using kotlin gradle dsl, the code I put above is using pure gradle in groovy language
z

ZabGo

04/28/2020, 11:29 AM
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

magnumrocha

04/28/2020, 1:21 PM
please, make a test: https://github.com/magnumrocha/kmp-slate try to compile this project, it's a minimal setup for kmp projects...
z

ZabGo

04/29/2020, 8:50 AM
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

magnumrocha

04/29/2020, 9:08 AM
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

ZabGo

04/29/2020, 9:16 AM
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

Patrick

05/05/2020, 8:54 AM
Maybe also open an issue or let JB know another way.
z

ZabGo

05/05/2020, 12:22 PM
@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.
81 Views