add kotlin-playground styles to MULTYPLATFORM.md (#2080)
This commit is contained in:
committed by
Nikolay Igotti
parent
e019477cc3
commit
c058d2b502
+72
-2
@@ -60,10 +60,16 @@ use `./gradlew` to run the build instead of using your local Gradle installation
|
|||||||
Once the wrapper is created we need to describe the project structure in Gradle terms. To do this, create
|
Once the wrapper is created we need to describe the project structure in Gradle terms. To do this, create
|
||||||
a `settings.gradle` file in the root directory of the project and put the following snippet into it:
|
a `settings.gradle` file in the root directory of the project and put the following snippet into it:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
|
||||||
|
```groovy
|
||||||
include ':greeting'
|
include ':greeting'
|
||||||
include ':greeting:common'
|
include ':greeting:common'
|
||||||
include ':greeting:android'
|
include ':greeting:android'
|
||||||
include ':greeting:ios'
|
include ':greeting:ios'
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
Here we declare all subprojects for our `greeting` multiplatform library. All other multiplatform libraries included
|
Here we declare all subprojects for our `greeting` multiplatform library. All other multiplatform libraries included
|
||||||
in the project also must be declared here.
|
in the project also must be declared here.
|
||||||
@@ -106,6 +112,9 @@ Now we have the basic structure of the project and can proceed to implement the
|
|||||||
We need to add buildscript dependencies to be able to use the Kotlin plugins for Gradle in our build. Open
|
We need to add buildscript dependencies to be able to use the Kotlin plugins for Gradle in our build. Open
|
||||||
the `build.gradle` in the `greeting` directory and put the following snippet into it:
|
the `build.gradle` in the `greeting` directory and put the following snippet into it:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
|
||||||
|
```groovy
|
||||||
// Set up a buildscript dependency on the Kotlin plugin.
|
// Set up a buildscript dependency on the Kotlin plugin.
|
||||||
buildscript {
|
buildscript {
|
||||||
// Specify a Kotlin version you need.
|
// Specify a Kotlin version you need.
|
||||||
@@ -130,6 +139,10 @@ the `build.gradle` in the `greeting` directory and put the following snippet int
|
|||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
Now all subprojects of the library can use Kotlin plugins.
|
Now all subprojects of the library can use Kotlin plugins.
|
||||||
|
|
||||||
@@ -137,6 +150,9 @@ Now all subprojects of the library can use Kotlin plugins.
|
|||||||
|
|
||||||
The `common` subproject contains platform-independent code. To build it, add the following snippet in `common/build.gradle`:
|
The `common` subproject contains platform-independent code. To build it, add the following snippet in `common/build.gradle`:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
|
||||||
|
```groovy
|
||||||
apply plugin: 'kotlin-platform-common'
|
apply plugin: 'kotlin-platform-common'
|
||||||
|
|
||||||
// Specify a group and a version of the library to access it in Android Studio.
|
// Specify a group and a version of the library to access it in Android Studio.
|
||||||
@@ -149,10 +165,16 @@ The `common` subproject contains platform-independent code. To build it, add the
|
|||||||
// Set up a compilation dependency on common Kotlin stdlib
|
// Set up a compilation dependency on common Kotlin stdlib
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
Now we can write some logic available for all platforms. Create `common/src/main/kotlin/common.kt` and add some
|
Now we can write some logic available for all platforms. Create `common/src/main/kotlin/common.kt` and add some
|
||||||
functionality into it:
|
functionality into it:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||||
|
|
||||||
|
```kotlin
|
||||||
// greeting/common/src/main/kotlin/common.kt
|
// greeting/common/src/main/kotlin/common.kt
|
||||||
package org.greeting
|
package org.greeting
|
||||||
|
|
||||||
@@ -163,6 +185,9 @@ functionality into it:
|
|||||||
class Greeting {
|
class Greeting {
|
||||||
fun greeting(): String = "Hello, ${Platform().platform}"
|
fun greeting(): String = "Hello, ${Platform().platform}"
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
Here we create a simple class using the `expect`/`actual` paradigm. Find details about platform-specific declarations
|
Here we create a simple class using the `expect`/`actual` paradigm. Find details about platform-specific declarations
|
||||||
[here](https://kotlinlang.org/docs/reference/multiplatform.html#platform-specific-declarations).
|
[here](https://kotlinlang.org/docs/reference/multiplatform.html#platform-specific-declarations).
|
||||||
@@ -173,6 +198,9 @@ The `android` subproject contains platform-dependent implementations of the `exp
|
|||||||
`common` project. We compile it into a Java library which an Android Studio project can depend on. The content
|
`common` project. We compile it into a Java library which an Android Studio project can depend on. The content
|
||||||
of the `android/build.gradle` will be the following:
|
of the `android/build.gradle` will be the following:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
|
||||||
|
```groovy
|
||||||
apply plugin: 'kotlin-platform-jvm'
|
apply plugin: 'kotlin-platform-jvm'
|
||||||
|
|
||||||
// Specify a group and a version of the library to access it in Android Studio.
|
// Specify a group and a version of the library to access it in Android Studio.
|
||||||
@@ -188,23 +216,34 @@ of the `android/build.gradle` will be the following:
|
|||||||
// Specify dependency on a common project for Kotlin multiplatform build.
|
// Specify dependency on a common project for Kotlin multiplatform build.
|
||||||
expectedBy project(':greeting:common')
|
expectedBy project(':greeting:common')
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
As mentioned above this subproject should include actual implementations of the common project's `expect`-declarations.
|
As mentioned above this subproject should include actual implementations of the common project's `expect`-declarations.
|
||||||
Let's write an Android-specific method:
|
Let's write an Android-specific method:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||||
|
|
||||||
|
```kotlin
|
||||||
// greeting/android/src/main/kotlin/android.kt
|
// greeting/android/src/main/kotlin/android.kt
|
||||||
package org.greeting
|
package org.greeting
|
||||||
|
|
||||||
actual class Platform actual constructor() {
|
actual class Platform actual constructor() {
|
||||||
actual val platform: String = "Android"
|
actual val platform: String = "Android"
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
#### 2.3 iOS subproject
|
#### 2.3 iOS subproject
|
||||||
|
|
||||||
This project is compiled into an Objective-C framework using the Kotlin/Native compiler. To do this, declare a framework in
|
This project is compiled into an Objective-C framework using the Kotlin/Native compiler. To do this, declare a framework in
|
||||||
`ios/build.gradle` and add an `expectedBy` dependency in the same manner as was done in the Android project:
|
`ios/build.gradle` and add an `expectedBy` dependency in the same manner as was done in the Android project:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
|
||||||
|
```groovy
|
||||||
apply plugin: 'konan'
|
apply plugin: 'konan'
|
||||||
|
|
||||||
// Specify targets to build the framework: iOS and iOS simulator
|
// Specify targets to build the framework: iOS and iOS simulator
|
||||||
@@ -222,15 +261,25 @@ This project is compiled into an Objective-C framework using the Kotlin/Native c
|
|||||||
// Specify dependency on a common project for Kotlin multiplatform build
|
// Specify dependency on a common project for Kotlin multiplatform build
|
||||||
expectedBy project(':greeting:common')
|
expectedBy project(':greeting:common')
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
As well as `android`, this project contains platform-dependent implementations of `expect`-declarations:
|
As well as `android`, this project contains platform-dependent implementations of `expect`-declarations:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||||
|
|
||||||
|
```kotlin
|
||||||
// greeting/ios/src/main/kotlin/ios.kt
|
// greeting/ios/src/main/kotlin/ios.kt
|
||||||
package org.greeting
|
package org.greeting
|
||||||
|
|
||||||
actual class Platform actual constructor() {
|
actual class Platform actual constructor() {
|
||||||
actual val platform: String = "iOS"
|
actual val platform: String = "iOS"
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
### 3. Android application
|
### 3. Android application
|
||||||
|
|
||||||
@@ -269,7 +318,9 @@ following line in `androidApp/settings.gradle`:
|
|||||||
> creating a composite build. To do this you need to declare them along with their directories in
|
> creating a composite build. To do this you need to declare them along with their directories in
|
||||||
> `androidApp/settings.gradle`:
|
> `androidApp/settings.gradle`:
|
||||||
>
|
>
|
||||||
>```
|
><div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
>
|
||||||
|
>```groovy
|
||||||
>include ':greeting'
|
>include ':greeting'
|
||||||
>include ':greeting:common'
|
>include ':greeting:common'
|
||||||
>include ':greeting:android'
|
>include ':greeting:android'
|
||||||
@@ -279,14 +330,23 @@ following line in `androidApp/settings.gradle`:
|
|||||||
>project(':greeting:android').projectDir = file('../greeting/android')
|
>project(':greeting:android').projectDir = file('../greeting/android')
|
||||||
>```
|
>```
|
||||||
>
|
>
|
||||||
|
></div>
|
||||||
|
>
|
||||||
> Now you can declare dependencies directly on projects instead of using maven-like coordinates:
|
> Now you can declare dependencies directly on projects instead of using maven-like coordinates:
|
||||||
>
|
>
|
||||||
>```
|
><div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
>
|
||||||
|
>```groovy
|
||||||
>implementation project(':greeting:android')
|
>implementation project(':greeting:android')
|
||||||
>```
|
>```
|
||||||
|
>
|
||||||
|
></div>
|
||||||
|
|
||||||
After these steps we can access our library as we would with any other Kotlin code:
|
After these steps we can access our library as we would with any other Kotlin code:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||||
|
|
||||||
|
```kotlin
|
||||||
import org.greeting.*
|
import org.greeting.*
|
||||||
|
|
||||||
/* ... */
|
/* ... */
|
||||||
@@ -294,6 +354,10 @@ After these steps we can access our library as we would with any other Kotlin co
|
|||||||
fun foo() {
|
fun foo() {
|
||||||
println(Greeting().greeting())
|
println(Greeting().greeting())
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
### 4. iOS application
|
### 4. iOS application
|
||||||
|
|
||||||
@@ -357,6 +421,9 @@ Do this for the common code of the library too.
|
|||||||
Now the framework is added and all the Kotlin API are available from Swift code (note that you need to build the
|
Now the framework is added and all the Kotlin API are available from Swift code (note that you need to build the
|
||||||
framework in order to get code completion). Let's print our greeting:
|
framework in order to get code completion). Let's print our greeting:
|
||||||
|
|
||||||
|
<div class="sample" markdown="1" theme="idea" mode="swift">
|
||||||
|
|
||||||
|
```swift
|
||||||
import Greeting
|
import Greeting
|
||||||
|
|
||||||
/* ... */
|
/* ... */
|
||||||
@@ -364,6 +431,9 @@ framework in order to get code completion). Let's print our greeting:
|
|||||||
func foo() {
|
func foo() {
|
||||||
print(GreetingGreeting().greeting())
|
print(GreetingGreeting().greeting())
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
### Sample
|
### Sample
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user