Giter Site home page Giter Site logo

jarroyoesp / kotlinmultiplatform Goto Github PK

View Code? Open in Web Editor NEW
746.0 28.0 65.0 2.69 MB

Kotlin MultiPlatform App (Android, iOS, JVM & JS). MVVM/MVP - Kotlin MultiPlatform

License: GNU General Public License v3.0

Kotlin 91.50% Swift 7.21% JavaScript 0.56% HTML 0.73%
kotlin kotlin-coroutines dagger mvvm-architecture ktor sqldelight kotlin-android kotlin-multiplatform android ios

kotlinmultiplatform's Introduction

Kotlin-Multiplatform (Android, iOS, JS & JVM Desktop App)

kotlin-version kotlin-weekly

License: GPL v3

This example shows how to create a simple Android/iOS/JVM/JS project sharing some Kotlin code. This app saves on a local database your favourites locations and get the current weather of them from OpenWeatherMap.

In this app we share common code for both platforms (Android/iOS) to get data from an API (in this case OpenWeatherMap). The libraries that we use are:

Tests:

  • MockK: mocking library for Kotlin

Android App - Master Branch

With this app you can get the weather of your locations using KTor:

And add and save on SQLDelight database your favourite locations:

iOS App

Open XCode and select the project:

JVM - Desktop App

To execute this version as desktop app, you have to execute this code on your console:

./gradlew JavaFxApp:run

By default the JDBC SQLite is created in memory. If you want to persist your database in your disk, you have to indicate the path when you create the database driver:

SQLDriver.kt
JdbcSqliteDriver("jdbc:sqlite:$your_database_path")

JS App

If you want to launch a web to get the weather you can try executing the next commands:

./gradlew JsApp:run
./gradlew BackEnd:run
--> localhost:8080

Current weather is not shown when you launch the browser? If you have this problem (Module not found: Error: Can't resolve 'core-js/features/object/assign') try to reinstall the node module core-js:

npm install core-js

I have followed these examples:

https://kotlinlang.org/docs/tutorials/native/mpp-ios-android.html

https://www.raywenderlich.com/1022411-kotlin-multiplatform-project-for-android-and-ios-getting-started

https://proandroiddev.com/kotlin-multiplatform-very-beginners-guide-part-3-database-e34c92daf41c

kotlinmultiplatform's People

Contributors

jarroyoesp avatar louiscad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kotlinmultiplatform's Issues

Question regarding bundling for Xcode

First of all, a big thanks! I had all the problem in the world to have my setting complete with the iOS portion. My Android app is running fine wit the shared code but noting worked with iOS. I did checked how you configured everything and use the same approach and now everything seems to be working fine.

Now, I have a question related to the bundling of Kotlin code into the iOS framework. I thought the iOS project had to import the .framework to be able to use the code. Now after building and using the packForXcode task in a script phase, the code is just magically there to be imported. Do you know what is going on exactly?

Thanks!

Can't build the desktop app

I am using a MacOS Catalina

 ./gradlew JavaFxApp:run
Starting a Gradle Daemon (subsequent builds will be faster)

> Configure project :app
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin

> Configure project :SharedCode
Kotlin Multiplatform Projects are an experimental feature.

> Task :SharedCode:compileKotlinJvm
w: //PathToProject/SharedCode/src/commonMain/kotlin/com/jarroyo/sharedcode/source/network/WeatherApi.kt: (21, 18): This declaration is experimental and its usage should be marked with '@kotlinx.serialization.UnstableDefault' or '@UseExperimental(kotlinx.serialization.UnstableDefault::class)'
w: //PathToProject/SharedCode/src/commonMain/kotlin/com/jarroyo/sharedcode/source/network/WeatherApi.kt: (33, 39): This declaration is experimental and its usage should be marked with '@kotlinx.serialization.UnstableDefault' or '@UseExperimental(kotlinx.serialization.UnstableDefault::class)'
w: //PathToProject/SharedCode/src/commonMain/kotlin/com/jarroyo/sharedcode/source/repository/LocationRepository.kt: (57, 70): Parameter 'failure' is never used

> Task :JavaFxApp:compileKotlin
w: //PathToProject/JavaFxApp/src/main/java/com/jarroyo/sharedcode/AppController.kt: (135, 21): Variable 'cityName' is never used
w: //PathToProject/JavaFxApp/src/main/java/com/jarroyo/sharedcode/AppController.kt: (135, 50): Unnecessary safe call on a non-null receiver of type kotlin.collections.ArrayList<Location> /* = java.util.ArrayList<Location> */

> Task :JavaFxApp:run FAILED
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
//PathToProject/JavaFxApp/build/resources/main/app.fxml

        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at com.jarroyo.sharedcode.JavaFxAppKt.launchApp(JavaFxApp.kt:9)
        at com.jarroyo.sharedcode.Main.start(Main.kt:11)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
        ... 15 more
Caused by: java.sql.SQLException: path to '/Users/javierarroyo/Projects/Pruebas/KotlinMultiplatform/First/JavaFxApp/database/database.db': '/Users/javierarroyo' does not exist
        at org.sqlite.core.CoreConnection.open(CoreConnection.java:192)
        at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
        at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:25)
        at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:24)
        at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
        at org.sqlite.JDBC.createConnection(JDBC.java:114)
        at org.sqlite.JDBC.connect(JDBC.java:88)
        at java.sql.DriverManager.getConnection(DriverManager.java:664)
        at java.sql.DriverManager.getConnection(DriverManager.java:208)
        at com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver.<init>(JdbcSqliteDriver.kt:29)
        at com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver.<init>(JdbcSqliteDriver.kt:20)
        at com.jarroyo.sharedcode.source.disk.SQLDriverKt.getSqlDriver(SQLDriver.kt:15)
        at com.jarroyo.sharedcode.source.disk.DatabaseCreator.getDataBase(SQLDriver.kt:12)
        at com.jarroyo.sharedcode.repository.LocationRepository.<init>(LocationRepository.kt:19)
        at com.jarroyo.sharedcode.di.InjectorCommon$locationRepository$2.invoke(InjectionsCommon.kt:43)
        at com.jarroyo.sharedcode.di.InjectorCommon$locationRepository$2.invoke(InjectionsCommon.kt:17)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at com.jarroyo.sharedcode.di.InjectorCommon.getLocationRepository(InjectionsCommon.kt)
        at com.jarroyo.sharedcode.di.InjectorCommon.provideGetLocationMPPUseCase(InjectionsCommon.kt:48)
        at com.jarroyo.sharedcode.di.InjectorCommon.provideProfilePresenter(InjectionsCommon.kt:65)
        at com.jarroyo.sharedcode.AppController.initPresenter(AppController.kt:75)
        at com.jarroyo.sharedcode.AppController.initialize(AppController.kt:34)
        ... 25 more

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':JavaFxApp:run'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 27s
6 actionable tasks: 6 executed

JavaScript version of the app can't handle starting arguments

In the build JS there's

///...
  var package$feature = package$com.feature || (package$com.feature = {});
  package$feature.main_kand9s$ = main;
  package$feature.Platform = Platform;
  main([]);
}));

You can see it calls main([]);

I don't understand why is that, since regardless if you call like like so

node test.js foo bar

or like so

./test.js foo bar

It's argument structure is always the same:

$ ./test.js foo bar
[ '/Users/danon/.nvm/versions/node/v10.17.0/bin/node',
  '/Users/danon/Projects/test.js',
  'foo',
  'bar' ]

Why couldn't kotlin compile it to JS

main(process.argv.slice(2));

JVM compiled version with the same Kotlin code does handle starting arguments.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.