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

Sergioedcigreja

07/11/2019, 12:58 PM
Hello everyone, I'm trying to get a device id from both platforms however I'm having trouble with android. I can't seem to use the android sdk in the androidMain module. I've already set the sdk.dir in local.properties so I don't know if I'm missing something..
k

Kris Wong

07/11/2019, 1:09 PM
did you add the android plugin to your build file?
s

Sam

07/11/2019, 1:44 PM
The default project template just sets up the android portion of an MP project as a jvm target. Use
fromPreset(presets.android, 'android')
and add the rest of the required standard android gradle config.
s

Sergioedcigreja

07/11/2019, 2:07 PM
plugins {
id 'kotlin-multiplatform' version '1.3.40'
id 'kotlinx-serialization' version '1.3.40'
}
repositories {
google()
jcenter()
mavenCentral()
maven { url "<https://kotlin.bintray.com/kotlinx>" }
}
group "pt.fyi"
version "0.0.1"
apply plugin: 'maven-publish'
apply plugin: 'kotlinx-serialization'
kotlin {
def coroutine_version = "1.3.0-M2"
def ktor_version = "1.2.2"
def serializer_version = "0.11.1"
jvm(
)
iosArm64("ios") {
binaries {
framework()
}
}
sourceSets {
commonMain {
dependencies {
implementation "io.ktor:ktor-client-json:$ktor_version"
implementation kotlin('stdlib-common')
implementation "io.ktor:ktor-client-core:$ktor_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.0-M2'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.11.1"
implementation "io.ktor:ktor-client-serialization:$ktor_version"
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmMain {
dependencies {
implementation kotlin('stdlib')
implementation "io.ktor:ktor-client-android:$ktor_version"
implementation "io.ktor:ktor-client-json-jvm:$ktor_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0-M2'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.11.1'
implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"
}
}
jvmTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
iosMain {
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serializer_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutine_version"
implementation "io.ktor:ktor-client-ios:$ktor_version"
implementation "io.ktor:ktor-client-core-native:$ktor_version"
implementation "io.ktor:ktor-client-json-native:$ktor_version"
implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
}
}
iosTest {
}
}
}
configurations {
compileClasspath
}
I don't have a presets nor am I applying any android plugin.. maybe that's it then
this is working well though as I have published to both platforms.. but now I need to execute some android code and need to implement an use case that need Files..
s

Sam

07/11/2019, 3:30 PM
I have my targets defined as:
Copy code
targets {
        final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
                              ? presets.iosArm64 : presets.iosX64

        fromPreset(iOSTarget, 'ios') {
            binaries {
                framework {
                    baseName = "MyFramework"
                    freeCompilerArgs.add("-Xobjc-generics")
                }
            }
        }

        fromPreset(presets.android, 'android')
        jvm()
    }
This builds an iOS framework, an Android AAR and a java jar. I don't use the jar for anything except to run common tests quickly. You'll need to have an
android { }
section and to import the
com.android.library
plugin as well.
l

louiscad

07/11/2019, 8:46 PM
You should use an app instance id rather than a device/hardware id whenever possible.
s

Sergioedcigreja

07/12/2019, 11:45 AM
The thing is that I cant seem to target for android.. note that in the source sets it appears only the common and iOS source sets. Also, if it matters in any way, note that in the folders the androidMain doesn't have the blue symbol
it may build the aar but when I try to test this and publish it to mavenLocal for example, it only appears the poms for the shared library and the sharedLibrary-iOS module..
and the aar appears to not have anything in it because I imported it to a test project and could use any classes.
If if use the expect/actual mechanism though it says that I need to implement in android so I don't know how to solve this
s

Sam

07/12/2019, 12:32 PM
Did you add androidMain in your sourcesets?
s

Sergioedcigreja

07/12/2019, 12:41 PM
yes I did
20 Views