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

albertgao

02/28/2018, 9:48 AM
ok, now things get complicated. Previously, I made an example of code sharing between iOS and Android via Kotlin native. I even made example of code sharing among jvm, js and kotlin native. They all works. Now the requirement is changed to I need to further separate the platform API into a separate repo. So, now the overall structure becomes something like this: project-platforms: -
common
module: Only contain
expect class
and no implementation just like plain
interface
. Build with plugin
kotlin-platform-common
- `platformsjvm`: actual implementation on jvm. It generates library, and I tried in Android Studio, it works. Build with plugin
kotlin-platform-jvm
project-lib: - `common`: It contains code which mean to be the same across all platforms. But it depends on the API from
common
of
project-platforms
. Which means it just depends on the
common
code rather than the actual implementation of a certain platform. Build with plugin
kotlin-platform-common
- `forBuild:JVM`: Its purpose is for testing
common
and build for jvm. So it just embed the source code from
common
, and add the generated jar file from
project-platforms :platforms:jvm
as dependencies and do the test and build. The testing part works, it will compile and run the test from
common
. And build without error. Now the problem is when use the generated jar file from
forBuild:JVM
in Android studio. It said:
java.lang.NoClassDefFoundError: Failed resolution of: Lnz/salect/kotlinidiom/Platform; Caused by: java.lang.ClassNotFoundException: Didn't find class "nz.salect.kotlinidiom.Platform" on path: DexPathList[[zip file "/data/app/nz.salect.handset-1/base.apk", zip file "/data/app/nz.salect.handset-1/split_lib_dependencies_apk.apk"],nativeLibraryDirectories=[/data/app/nz.salect.handset-1/lib/x86, /system/lib, /vendor/lib]]
And the
Platform
class is from
project-platforms
which already add its jar file as dependencies. I tried built with either
kotlin-platform-jvm
or
org.jetbrains.kotlin.jvm
, both yield the same error. What am I missing here?
a

anton.bannykh

02/28/2018, 9:55 AM
cc @ilya.gorbunov @udalov
Just curious, why do you need to separate project-platforms from project-lib?
a

albertgao

02/28/2018, 8:21 PM
@anton.bannykh To make it more modularize so we can write other libs like
project-lib
. I am not fully agree with this change due to the fact that KMP is still pretty new, but I just think it should work because it is really isn’t that complex, so I decide to give it a try. 🙂 I think I miss something here.
OK, even though it’s ugly, I solved it. In this
forBuild:JVM
, I just embed the whole source code from
project-lib:common
,
project-platforms:common
and
project-platforms:jvm
, and use
kotlin-platform-jvm
to compile the code. It works fine now. And for the test, I just embed the tests from
project-lib:common
, it all works.
i

ilya.gorbunov

03/04/2018, 5:44 AM
@albertgao Do you have a dependency on
:platforms:jvm
in
forBuild:jvm
project?
a

albertgao

03/04/2018, 7:11 AM
@ilya.gorbunov Hi, since they are in different projects, so I add the generated jar file as a dependency like
compile files('../../project-platforms/build/platforms-jvm.jar')
, but it doesn’t work as I described in the post, so I just embed the source code instead, then it works.
i

ilya.gorbunov

03/04/2018, 7:39 AM
Could you check that jar, whether it contains the missing Platform class?
Embedding the common source code manually is definitely not an intended way to consume multiplatform libraries, so even if it has worked in this case, it may cause problems later.
a

albertgao

03/04/2018, 9:30 AM
@ilya.gorbunov So, how could I know that it doesn’t contain that missing platform class?
@ilya.gorbunov Hi, double checked. When I combine all the source code, the generated jar file contains the
Platform.class
. But when I just compile with the generated jar file from
project-platforms :platforms:jvm
( compile files(“jvm-1.0.jar”)), it doesn’t contain that
Platform.class
in the result. This is why android couldn’t resolve it. And I checked the jar from
project-platforms :platforms:jvm
, it indeed contains the
Platform.class
.
6 Views