add kotlin-playground styles to MULTYPLATFORM.md (#2080)
This commit is contained in:
committed by
Nikolay Igotti
parent
e019477cc3
commit
c058d2b502
+158
-88
@@ -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:
|
||||||
|
|
||||||
include ':greeting'
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
include ':greeting:common'
|
|
||||||
include ':greeting:android'
|
```groovy
|
||||||
include ':greeting:ios'
|
include ':greeting'
|
||||||
|
include ':greeting:common'
|
||||||
|
include ':greeting:android'
|
||||||
|
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,30 +112,37 @@ 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:
|
||||||
|
|
||||||
// Set up a buildscript dependency on the Kotlin plugin.
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
buildscript {
|
|
||||||
// Specify a Kotlin version you need.
|
|
||||||
ext.kotlin_version = '1.2.41'
|
|
||||||
|
|
||||||
repositories {
|
```groovy
|
||||||
jcenter()
|
// Set up a buildscript dependency on the Kotlin plugin.
|
||||||
maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
|
buildscript {
|
||||||
}
|
// Specify a Kotlin version you need.
|
||||||
|
ext.kotlin_version = '1.2.41'
|
||||||
|
|
||||||
// Specify all the plugins used as dependencies
|
repositories {
|
||||||
dependencies {
|
jcenter()
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
|
||||||
classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.7"
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up compilation dependency repositories for all projects.
|
// Specify all the plugins used as dependencies
|
||||||
subprojects {
|
dependencies {
|
||||||
repositories {
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
jcenter()
|
classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.7"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up compilation dependency repositories for all projects.
|
||||||
|
subprojects {
|
||||||
|
repositories {
|
||||||
|
jcenter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
Now all subprojects of the library can use Kotlin plugins.
|
Now all subprojects of the library can use Kotlin plugins.
|
||||||
|
|
||||||
@@ -137,32 +150,44 @@ 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`:
|
||||||
|
|
||||||
apply plugin: 'kotlin-platform-common'
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
|
||||||
// Specify a group and a version of the library to access it in Android Studio.
|
```groovy
|
||||||
// By default the project directory name is used as an artifact name thus the full dependency
|
apply plugin: 'kotlin-platform-common'
|
||||||
// description will be 'org.greeting:common:1.0'
|
|
||||||
group = 'org.greeting'
|
|
||||||
version = 1.0
|
|
||||||
|
|
||||||
dependencies {
|
// Specify a group and a version of the library to access it in Android Studio.
|
||||||
// Set up a compilation dependency on common Kotlin stdlib
|
// By default the project directory name is used as an artifact name thus the full dependency
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
|
// description will be 'org.greeting:common:1.0'
|
||||||
}
|
group = 'org.greeting'
|
||||||
|
version = 1.0
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
// Set up a compilation dependency on common Kotlin stdlib
|
||||||
|
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:
|
||||||
|
|
||||||
// greeting/common/src/main/kotlin/common.kt
|
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||||
package org.greeting
|
|
||||||
|
|
||||||
expect class Platform() {
|
```kotlin
|
||||||
val platform: String
|
// greeting/common/src/main/kotlin/common.kt
|
||||||
}
|
package org.greeting
|
||||||
|
|
||||||
class Greeting {
|
expect class Platform() {
|
||||||
fun greeting(): String = "Hello, ${Platform().platform}"
|
val platform: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Greeting {
|
||||||
|
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,64 +198,88 @@ 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:
|
||||||
|
|
||||||
apply plugin: 'kotlin-platform-jvm'
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
|
||||||
// Specify a group and a version of the library to access it in Android Studio.
|
```groovy
|
||||||
// By default the project directory name is used as an artifact name thus the full dependency
|
apply plugin: 'kotlin-platform-jvm'
|
||||||
// description will be 'org.greeting:android:1.0'
|
|
||||||
group = 'org.greeting'
|
|
||||||
version = 1.0
|
|
||||||
|
|
||||||
dependencies {
|
// Specify a group and a version of the library to access it in Android Studio.
|
||||||
// Specify Kotlin/JVM stdlib dependency.
|
// By default the project directory name is used as an artifact name thus the full dependency
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
|
// description will be 'org.greeting:android:1.0'
|
||||||
|
group = 'org.greeting'
|
||||||
|
version = 1.0
|
||||||
|
|
||||||
// Specify dependency on a common project for Kotlin multiplatform build.
|
dependencies {
|
||||||
expectedBy project(':greeting:common')
|
// Specify Kotlin/JVM stdlib dependency.
|
||||||
}
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
|
||||||
|
|
||||||
|
// Specify dependency on a common project for Kotlin multiplatform build.
|
||||||
|
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:
|
||||||
|
|
||||||
// greeting/android/src/main/kotlin/android.kt
|
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||||
package org.greeting
|
|
||||||
|
|
||||||
actual class Platform actual constructor() {
|
```kotlin
|
||||||
actual val platform: String = "Android"
|
// greeting/android/src/main/kotlin/android.kt
|
||||||
}
|
package org.greeting
|
||||||
|
|
||||||
|
actual class Platform actual constructor() {
|
||||||
|
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:
|
||||||
|
|
||||||
apply plugin: 'konan'
|
<div class="sample" markdown="1" theme="idea" mode="groovy">
|
||||||
|
|
||||||
// Specify targets to build the framework: iOS and iOS simulator
|
```groovy
|
||||||
konan.targets = ['ios_arm64', 'ios_x64']
|
apply plugin: 'konan'
|
||||||
|
|
||||||
konanArtifacts {
|
// Specify targets to build the framework: iOS and iOS simulator
|
||||||
// Declare building into a framework.
|
konan.targets = ['ios_arm64', 'ios_x64']
|
||||||
framework('Greeting') {
|
|
||||||
// The multiplatform support is disabled by default.
|
konanArtifacts {
|
||||||
enableMultiplatform true
|
// Declare building into a framework.
|
||||||
}
|
framework('Greeting') {
|
||||||
|
// The multiplatform support is disabled by default.
|
||||||
|
enableMultiplatform true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// 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:
|
||||||
|
|
||||||
// greeting/ios/src/main/kotlin/ios.kt
|
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||||
package org.greeting
|
|
||||||
|
```kotlin
|
||||||
|
// greeting/ios/src/main/kotlin/ios.kt
|
||||||
|
package org.greeting
|
||||||
|
|
||||||
|
actual class Platform actual constructor() {
|
||||||
|
actual val platform: String = "iOS"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
actual class Platform actual constructor() {
|
|
||||||
actual val platform: String = "iOS"
|
|
||||||
}
|
|
||||||
|
|
||||||
### 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,21 +330,34 @@ 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:
|
||||||
|
|
||||||
import org.greeting.*
|
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||||
|
|
||||||
/* ... */
|
```kotlin
|
||||||
|
import org.greeting.*
|
||||||
|
|
||||||
|
/* ... */
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
println(Greeting().greeting())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
fun foo() {
|
|
||||||
println(Greeting().greeting())
|
|
||||||
}
|
|
||||||
|
|
||||||
### 4. iOS application
|
### 4. iOS application
|
||||||
|
|
||||||
@@ -357,13 +421,19 @@ 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:
|
||||||
|
|
||||||
import Greeting
|
<div class="sample" markdown="1" theme="idea" mode="swift">
|
||||||
|
|
||||||
/* ... */
|
```swift
|
||||||
|
import Greeting
|
||||||
|
|
||||||
func foo() {
|
/* ... */
|
||||||
print(GreetingGreeting().greeting())
|
|
||||||
}
|
func foo() {
|
||||||
|
print(GreetingGreeting().greeting())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
### Sample
|
### Sample
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user