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

andreasmattsson

08/13/2019, 7:39 AM
My IDE (Android Studio) is complaining about
Overload Resolution Ambiguity
in the Android app modules of my MPP in cases where the disambiguity appears to only be in "comments" for the resolution of typealiases, see example snippet. It compiles fine with gradle, however it would be nice to get syntax highlighting and autocomplete etc on these types... Any suggestions for ways to resolve this?
l

louiscad

08/13/2019, 8:01 AM
@andreasmattsson Does it still complain if you search "New inference" in IDE settings and disable it?
a

andreasmattsson

08/13/2019, 8:43 AM
@louiscad hmm not finding it with that specific search term
Copy code
Android Studio 3.4.2
Build #AI-183.6156.11.34.5692245, built on June 27, 2019
JRE: 1.8.0_152-release-1343-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
l

louiscad

08/13/2019, 8:46 AM
@andreasmattsson Search is not working well for that it seems. You can find the checkbox in Settings > Other Settings > Kotlin Compiler under the name "Enable new type inference algorithm for IDE analysis". It's the second checkbox.
a

andreasmattsson

08/13/2019, 8:48 AM
@louiscad thanks, no immediate changes, nor after gradle resync. Should I do a full rebuild as well or no point in that?
l

louiscad

08/13/2019, 8:49 AM
@andreasmattsson You could reopen the project if you don't observe any changes. No need to restart the IDE.
a

andreasmattsson

08/13/2019, 9:02 AM
@louiscad okay, that changed the actual message, and actually got the same problem on another statement I wasn't getting it on before, but the actual gist of it is the same:
Copy code
Cannot choose among the following candidates without completing type inference:
public fun <T : Any> rxObservableOf(value: Float): RxObservable<Float> defined in se.sdg12.deedster.concurrency.rx
public fun <T : Any> rxObservableOf(value: Float): RxObservable<Float> /* = Flowable<Float> */ defined in se.sdg12.deedster.concurrency.rx
l

louiscad

08/13/2019, 9:03 AM
@andreasmattsson The IDE still shows errors, although different, but the same project/module compiles fine?
a

andreasmattsson

08/13/2019, 9:06 AM
@louiscad Still compiles fine, showing on a few more statements after switching off new inference, but still same type of statements: All of them where I'm using the expect/actual
RxObservable
type which i have typealiased to RxJava2's
Flowable
for my java/android projects
l

louiscad

08/13/2019, 9:08 AM
Then, you should report the bug on kotl.in/issue, preferrably showing the behavior with new type inference off as it's disabled now, and then re-enabling and showing the errors you initially had.
a

andreasmattsson

08/13/2019, 9:16 AM
Hmm... Just tried something else which "solved" the issue. If I remove my "pure java" module from settings.gradle (and its corresponding sourceSet in the common module) it manages to resolve it just fine in the consuming android module (both with "new inference" off and on). The file/directory that the typealias was in was actually "shared" between both the android/pure-java versions of my common module (with
kotlin.srcDir('src/jvmCommonMain/kotlin')
). @louiscad are you aware of if there are any existing issues related to IDE and including same file in multiple sourcesets, or should I still create a new one?
l

louiscad

08/13/2019, 9:18 AM
Oh, this kind of thing doesn't work well for now in the IDE
a

andreasmattsson

08/13/2019, 9:18 AM
Okay I suspected as much.
I previously had jvmCommon as a separate sourceSet with dependsOn from the jvmPure and android sourceSets, but that was much much worse in IDE. (barely anything was resolved in some of my consuming modules).
l

louiscad

08/13/2019, 9:19 AM
I'm doing it anyway to share code between other platforms. I had to hack the sourceSets plus symlinks for when the IDE syncs with Gradle
a

andreasmattsson

08/13/2019, 9:20 AM
Yeah I was really hoping to avoid going the symlinks route... :S
Know if there are any issues to track/follow for this?
l

louiscad

08/13/2019, 9:20 AM
I did a Kotlin script to generate the symlinks too
Do you really need to have an android and a jvm sourceSet? Can't you have a pure jvm module, and another android module depending on the jvm module instead?
a

andreasmattsson

08/13/2019, 9:26 AM
I have stuff I want to be able to access from my jvmCommon that I would like to
expect/actual
with different impls for pure/android (with the android versions needing access to the application context for example). I could get around it with a more dependency injection-like approach (having just an interface in the common module and "injecting" the different impls in consuming platform-specific modules) but that would require pretty significant work, whereas different
expect/actual
impls seemed much easier.
l

louiscad

08/13/2019, 9:59 AM
@andreasmattsson Here's the Kotlin script I made to create the symlinks. You can edit it to add the
jvmCommon
intermediate source set. It'll not delete existing directories, so it's safe to use on existing structure. Be sure to launch the script from the directory of your project's root using
kotlin -script path/to/the/script/dir/Add-Kotlin-Targets.kts
. https://github.com/LouisCAD/Splitties/blob/develop/scripts/Add-Kotlin-Targets.kts#L27 Then, you'll need to make an edited (and probably simpler) version of this
buildSrc
code I made to make the IDE point to the symlinked directories for each target: https://github.com/LouisCAD/Splitties/blob/0318f6047059ef5e97fb7bd53bd363420ad5fdf9/buildSrc/src/main/kotlin/config/KotlinSourceSetsConfig.kt#L35 It targets Kotlin/Native, but you can add support for Kotlin/JVM and Android too. After you made this and renamed the function (since it no longer targets native source sets only if at all), you can simply call it after you define the targets in your build.gradle(.kts). Note that after this is setup, symlinked files need to be manually saved (using cmd/ctrl + S), and the IDE might be a little slow to propagate files creations to other symlinked targets. Let me know if you need further help.
a

andreasmattsson

08/13/2019, 10:04 AM
Thanks a lot @louiscad! I might look later closer, but for now I think I can live with just adding some flag to settings.gradle I can use to toggle between loading one or the other of the android or pure java modules (one at a time seems to work, both at once not) when working in the IDE...
l

louiscad

08/13/2019, 10:05 AM
Not needed, you can detect automatically when the IDE is using Gradle.
Although you might still want that switch if you run some publishing gradle tasks from the IDE
I mean, the toggle is probably simpler to use one or another while developing, and both when building, but I personally prefer being able to use both while developing, and just have to use command line for publishing (instead of using IDE and launching tasks from the Gradle tool window).
20 Views