Coder Social home page Coder Social logo

wojta / hello-kotlin-multiplatform Goto Github PK

View Code? Open in Web Editor NEW
68.0 3.0 13.0 7.03 MB

Multiplatform Kotlin Hello World (Android/Java/JavaScript)

Kotlin 97.10% HTML 1.08% JavaScript 0.48% Dockerfile 1.33%
kotlin kotlin-multiplatform kotlin-android kotlin-js node-javascript platform-independent android jvm frontend-javascript

hello-kotlin-multiplatform's Introduction

CircleCI

KOTLIN 1.7.20

Multiplatform Kotlin Hello World

This project demonstrates sharing runtime independent code between different Kotlin's runtimes (Java/Android/JavaScript). It uses Gradle build engine.

It uses new support for multiplatform modules with plugin kotlin-multiplatform.

This example uses now Kotlin Multiplatform as is in Kotlin 1.7.20.

Deprecated implementations

Older implementation with kotlin-multiplatform plugin and various hacks was moved to old-kotlin-1.2 branch.

More older implementation that didn't use kotlin-multiplatform plugin and various hacks was moved to old-multiplatform branch.

Oldest implementation that used various hacks was moved to old-multiplatform branch.

What is Kotlin?

Kotlin is a programming language developed by Jetbrains. It's fully compatibile with Java runtimes and also there is support for JavaScript transpilation. Experimental version of Kotlin/Native has goal to also build fully native apps for iOS, Linux, Windows and possibly other platforms.

What is it doing?

  • writes Hello Kotlin!
  • calculates first 1000 prime numbers (this part is shared between runtimes) and prints them

Structure

It's the Gradle multiple modules project.

  • hello_android_app - Android application module, it's compiled to DEX bytecode, it produces APK file upon build
  • hello_js_browser_app - application transpiled for frontend JavaScript, packed in WebPack, it's only statically served by Node.js
  • hello_js_node_app - console application transpiled to Node.js JavaScript
  • hello_jvm_app - console application compiled to Java bytecode for JVM, produces JAR that can be executed by eg. Oracle JVM
  • hello_shared - multiplatform library project, with shared and platform specific code
    • commonMain - shared Kotlin source code, platform independent code
    • commonTest - shared tests, platform independent tests
    • jsMain - JavaScript runtimes platform dependent code
    • jsTest - JavaScript runtimes specific tests
    • jvmMain - Java runtime platform dependent code
    • jvmTest - Java runtime specific tests
    • androidMain - Android runtime platform dependent code
    • androidTest - Android runtime specific tests

Modules dependency

Platform implementation specifics

  • prime number calculation is platform independent, single code shared for all platforms
  • text output on screen is platform dependent
    • Android - it's done by adding with TextView to layout
    • Frontend JavaScript - it adds element in DOM of HTML page
    • Node.js JavaScript - uses console.log()
    • JVM - uses System.out.println()

Note: Ordinary console output can be done by println() function from Kotlin Standard Library.

Implementation in modules

Building and running the demo

It was checked only under Linux Mint, probably there won't be any problems with most Unix-like environments.

Android application

You can use Android Studio to run the application. To build from command line, you can use

# ./gradlew hello_android_app:build

and APK file is located in your build/outputs/apk directory.

Hello Android

JVM console application

# ./gradlew hello_jvm_app:build

You can than run the JAR file using java -jar hello_jvm_app.jar command from build/libs directory.

Hello JVM

Frontend JavaScript application

# ./gradlew hello_js_browser_app:build

Webpack allows to host site directly from Gradle by

# ./gradlew hello_js_browser_app:run 

It will run locally on http://localhost:8088/.

Hello JavaScript Browser

Node.js console application

# ./gradlew hello_js_node_app:build

You can execute it in hello_js_node_app/build/productionLibrary directory by executing:

# node .

Hello JavaScript Node.js

to see all build options

# ./gradlew tasks --all

License

Do whathever you want with this.

hello-kotlin-multiplatform's People

Contributors

riccardom avatar wojta 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

Watchers

 avatar  avatar  avatar

hello-kotlin-multiplatform's Issues

No test classes are found for common and js projects on 'gradle test'

Tests fail to run in hello_lib_common and hello_js_browser_app.
JVM project tests seem to run correctly.

gradle test --info
...
> Task :hello_lib_common:testClasses UP-TO-DATE
Skipping task ':hello_lib_common:testClasses' as it has no actions.
:hello_lib_common:testClasses (Thread[Task worker for ':',5,main]) completed. Took 0.0 secs.
:hello_lib_common:test (Thread[Task worker for ':',5,main]) started.
...
> Task :hello_lib_jvm:testClasses UP-TO-DATE
Skipping task ':hello_lib_jvm:testClasses' as it has no actions.
:hello_lib_jvm:testClasses (Thread[Daemon worker Thread 9,5,main]) completed. Took 0.0 secs.
:hello_lib_jvm:test (Thread[Daemon worker Thread 9,5,main]) started.

Apologies for not showing this more convincingly, or succinctly, but gradle limits my options since it does not print out test results. (Or I don't know how to configure it to in a nice way).

image

image

image

CircleCI

Automatic building and testing using CircleCI.

IOS App

When can we expect a sample app for IOS mobile app (hello world) with Xcode? It is difficult to find examples on the internet.

Use version catalog

  • upgrade Gradle
  • Add version catalog and proper project typesafe referencing

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.