Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
# Conflicts:
#	docs/topics/gradle/get-started-with-jvm-gradle-project.md
  • Loading branch information
hltj committed May 4, 2024
2 parents 4d4544d + d6cb0fd commit 2fd81d2
Show file tree
Hide file tree
Showing 19 changed files with 2,081 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ object StdlibPrepareDokkaTemplates: BuildType({
param("env.ALGOLIA_INDEX_NAME", "api-core")
param("env.DOKKA_NOINDEX", "true")
param("env.DOKKA_CUSTOM_BODY", "core-api")
param("env.DOKKA_FEEDBACK", "true")
}
})
Binary file added assets/images/core-launch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/gradle/jvm-gradle-view-build.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/images/gradle/jvm-main-kt-initial-gradle.png
Binary file not shown.
Binary file modified docs/images/gradle/jvm-new-gradle-project-advanced.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/gradle/jvm-new-gradle-project.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/gradle/jvm-output-gradle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/gradle/jvm-run-app-gradle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/kr.tree
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@

<toc-element toc-title="Compiler and plugins">
<toc-element toc-title="Compiler">
<toc-element toc-title="K2 compiler migration guide" id="k2-compiler-guide.md"/>
<toc-element toc-title="K2 compiler migration guide" id="k2-compiler-migration-guide.md" accepts-web-file-names="k2-compiler-guide.html"/>
<toc-element toc-title="Command-line compiler" id="command-line.md"/>
<toc-element toc-title="Compiler options" id="compiler-reference.md"/>
</toc-element>
Expand Down
89 changes: 41 additions & 48 deletions docs/topics/gradle/get-started-with-jvm-gradle-project.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
[//]: # (title: 以 Gradle 与 Kotlin/JVM 入门)

本教程演示了如何通过 InteliJ IDEA 和 Gradle 来创建一个控制台应用
本教程演示了如何通过 InteliJ IDEA 和 Gradle 来创建一个 JVM 控制台应用程序

首先,请先下载最新版本的 [IntelliJ IDEA](https://www.jetbrains.com/idea/download/index.html)

## 创建项目

1. 在 IntelliJ IDEA 中选择**文件** | **新建** | **项目**
2. 在面板的左侧,选择**新建项目**
2. 在面板的左侧,选择**Kotlin**
3. 根据需要修改项目的名称和位置。

> 勾选**创建 Git 仓库**来让你的项目弃启用版本控制。
> 你也可以在项目创建之后再来创建。
>
{type="tip"}

4.**语言**这一行选择 **Kotlin**

![Create a console application](jvm-new-gradle-project.png){width=700}

5.**构建系统**这一行选择 **Gradle**
6.**JDK** 这一行选择你想要应用在项目中的 [JDK](https://www.oracle.com/java/technologies/downloads/)
4.**构建系统**这一行选择 **Gradle**
5.**JDK** 这一行选择你想要应用在项目中的 [JDK](https://www.oracle.com/java/technologies/downloads/)
版本。
* 如果你的计算机中已经安装了 JDK,但是 IDE 并没有识别。选择**添加 JDK** 并选择<!--
-->JDK 的 Home 路径。
* 如果你的计算机中尚未安装 JDK,选择 **下载 JDK**

7.**Gradle DSL** 这一行选择 **Kotlin**
8. 勾选**添加示例代码**来创建一个初始的 `"Hello World!"`应用。
6. Select the **Kotlin** DSL for Gradle.
7. 勾选**添加示例代码**来创建一个初始的 `"Hello World!"`应用。

> You can also enable the **Generate code with onboarding tips** option to add some additional useful comments to your
> sample code.
>
{type="tip"}

9. 点击**创建**
8. 点击**创建**

你已经成功创建了一个 Gradle 项目
你已经成功创建了一个 Gradle 项目

#### Specify a Gradle version for your project {initial-collapse-state="collapsed"}

You can explicitly specify a Gradle version for your project under the **Advanced Settings** section, either by using the Gradle Wrapper or a local installation of Gradle:
* **Gradle Wrapper:** From the **Gradle distribution** list, select **Wrapper**. Deselect the **Auto-select** checkbox, and from the **Gradle version** list, select your Gradle version.
* **Local installation:** From the **Gradle distribution** list, select **Local installation**. For **Gradle location**, specify the path of your local Gradle version.
You can explicitly specify a Gradle version for your project under the **Advanced Settings** section,
either by using the Gradle Wrapper or a local installation of Gradle:

* **Gradle Wrapper:**
1. From the **Gradle distribution** list, select **Wrapper**.
2. Disable the **Auto-select** checkbox.
3. From the **Gradle version** list, select your Gradle version.
* **Local installation:**
1. From the **Gradle distribution** list, select **Local installation**.
2. For **Gradle location**, specify the path of your local Gradle version.

![Advanced settings](jvm-new-gradle-project-advanced.png){width=700}

Expand All @@ -51,70 +56,58 @@ You can explicitly specify a Gradle version for your project under the **Advance
打开 `build.gradle.kts` 文件。这个是 Gradle Kotlin的构建脚本,它包含了和 Kotlin 相关的配置以及应用所需的其他部分。

```kotlin
// 用于下方的 `KotlinCompile` 任务
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "%kotlinVersion%" // 使用的 Kotlin 版本
application // application 插件,见下方 1️⃣ 注释
kotlin("jvm") version "%kotlinVersion%" // Kotlin version to use
}

group = "org.example" // 公司域名,例如:`org.jetbrains`
version = "1.0-SNAPSHOT" // 构建工件的版本号

repositories { // 依赖的源,见下方 2️⃣ 注释
mavenCentral() // Maven 中心仓库,见下方 3️⃣ 注释
repositories { // 依赖的源,见下方 1️⃣ 注释
mavenCentral() // Maven 中心仓库,见下方 2️⃣ 注释
}

dependencies { // 项目中需要用到的库,见下方 4️⃣ 注释
dependencies { // 项目中需要用到的库,见下方 3️⃣ 注释
// 在仓库在找到你需要的依赖,并将他们的依赖添加代码复制到这里
testImplementation(kotlin("test")) // Kotlin 的测试库
}

tasks.test { // 见下方 5️⃣ 注释
tasks.test { // 见下方 4️⃣ 注释
useJUnitPlatform() // 用于测试的 JUnitPlatform 依赖,见下方 6️⃣ 注释
}

kotlin { // 额外的扩展选项
jvmToolchain(%jvmLTSVersionSupportedByKotlin%) // 生成 JVM 字节码的目标版本,见下方 7️⃣ 注释
}

application {
mainClass.set("MainKt") // 应用的入口类
}
```

* 1️⃣ [application 插件](https://docs.gradle.org/current/userguide/application_plugin.html)用于构建基于 Java 语言的 CLI 应用。
* 2️⃣ 了解更多关于[依赖源](https://docs.gradle.org/current/userguide/declaring_repositories.html)的信息。
* 3️⃣ [Maven 中心仓库](https://central.sonatype.com/)也可以使用 [Google 的 Maven 仓库](https://maven.google.com/)或者公司的私有仓库替代。
* 4️⃣ 了解更多关于[依赖声明](https://docs.gradle.org/current/userguide/declaring_dependencies.html)的信息。
* 5️⃣ 了解更多关于[任务](https://docs.gradle.org/current/dsl/org.gradle.api.Task.html)的信息。
* 6️⃣ [用于测试的 JUnitPlatform](https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html#useJUnitPlatform).
* 7️⃣ 了解更多关于 [Java 工具链配置](gradle-configure-project.md#gradle-java-toolchains-support)的信息。
* 1️⃣ 了解更多关于[依赖源](https://docs.gradle.org/current/userguide/declaring_repositories.html)的信息。
* 2️⃣ [Maven 中心仓库](https://central.sonatype.com/)也可以使用 [Google 的 Maven 仓库](https://maven.google.com/)或者公司的私有仓库替代。
* 3️⃣ 了解更多关于[依赖声明](https://docs.gradle.org/current/userguide/declaring_dependencies.html)的信息。
* 4️⃣ 了解更多关于[任务](https://docs.gradle.org/current/dsl/org.gradle.api.Task.html)的信息。
* 5️⃣ [用于测试的 JUnitPlatform](https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html#useJUnitPlatform).

如你所见,在 Gradle 的构建文件中也有一些关于 Kotlin 的配置项:

1.`plugins{}` 块中,有一行 `kotlin("jvm")` – 该插件定义了项目中所用的 Kotlin 版本。
1.`plugins {}` 块中,有一行 `kotlin("jvm")`。这个插件定义了项目中所用的 Kotlin 版本。

2.`dependencies` 块中,有一行 `testImplementation(kotlin("test"))`
2.`dependencies {}` 块中,有一行 `testImplementation(kotlin("test"))`
了解更多关于[配置用于测试的库](gradle-configure-project.md#set-dependencies-on-test-libraries)的信息。

3.`dependencies` 块下方,有关于 `KotlinCompile` 任务的配置代码块。
你可以在这里给编译器添加额外的参数来启用或者关闭某些语言特性。

## 运行应用

打开位于 `src/main/kotlin``Main.kt` 文件。
`src` 目录包含了 Kotlin 的源代码和资源文件。`Main.kt` 文件包含了会打印出<!--
-->“Hello World!”的示例代码。
1. Open the Gradle window by selecting **View** | **Tool Windows** | **Gradle**:

![带有main函数的Main.kt](jvm-gradle-view-build.png){width=700}

2. Execute the **build** Gradle task in `Tasks\build\`. In the **Build** window, `BUILD SUCCESSFUL` appears.
It means that Gradle built the application successfully.

![带有main函数的Main.kt](jvm-main-kt-initial-gradle.png){width=700}
3. In `src/main/kotlin`, open the `Main.kt` file:
* `src` directory contains Kotlin source files and resources.
* `Main.kt` file contains sample code that will print `Hello World!`.

运行该应用最简单的办法就是点击对齐线旁绿色的**运行**图标,并选择**运行 'MainKt'**
4. Run the application by clicking the green **Run** icon in the gutter and select **Run 'MainKt'**.

![运行一个控制台应用](jvm-run-app-gradle.png){width=350}

你可以在**运行**的窗口中看到结果
你可以在**运行**的窗口中看到结果

![Kotlin 输出结果](jvm-output-gradle.png){width=600}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This guide:
* Highlights changes you might encounter during migration and how to adapt your code accordingly.
* Describes how you can roll back to the previous version.

> The new K2 compiler is enabled by default starting with Kotlin 2.0.0. For more information on the new features provided
> The new K2 compiler is enabled by default starting with %kotlinEapVersion%. For more information on the new features provided
> in Kotlin 2.0.0, as well as the new K2 compiler, see [What's new in Kotlin %kotlinEapVersion%](whatsnew-eap.md).
>
{type="note"}
Expand Down Expand Up @@ -499,9 +499,9 @@ class Expanded // Visibility is public by defaul

## How to enable the Kotlin K2 compiler

Starting with Kotlin 2.0.0, the Kotlin K2 compiler is enabled by default.
Starting with Kotlin %kotlinEapVersion%, the Kotlin K2 compiler is enabled by default.

To upgrade the Kotlin version, change it to 2.0.0 in your [Gradle](gradle-configure-project.md#apply-the-plugin) and
To upgrade the Kotlin version, change it to %kotlinEapVersion% in your [Gradle](gradle-configure-project.md#apply-the-plugin) and
[Maven](maven.md#configure-and-enable-the-plugin) build scripts.

### Use Kotlin build reports with Gradle
Expand Down Expand Up @@ -547,7 +547,7 @@ IntelliJ IDEA can use the new K2 compiler to analyze your code with its K2 Kotli

## How to roll back to the previous compiler

To use the previous compiler in Kotlin 2.0.0, either:
To use the previous compiler in Kotlin %kotlinEapVersion%, either:

* In your `build.gradle.kts` file, [set your language version](gradle-compiler-options.md#设置-languageversion-的示例) to `1.9`.

Expand Down Expand Up @@ -635,21 +635,20 @@ If you have the following Kotlin code, where instances of the `Container` class
method will always generate an error. However, only from Kotlin 2.0.0 will the synthetic `foo` property trigger an error:

```kotlin
fun exampleFunction(firstContainer: Container<*>, secondContainer: Container<in Number>, sampleString: String) {
container.setFoo(sampleString)
fun exampleFunction(starProjected: Container<*>, inProjected: Container<in Number>, sampleString: String) {
starProjected.setFoo(sampleString)
// Error since Kotlin 1.0

// Synthetic setter `foo` is resolved to the `setFoo()` method
container.foo = sampleString
starProjected.foo = sampleString
// Error since Kotlin 2.0.0

secondContainer.setFoo(sampleString)
inProjected.setFoo(sampleString)
// Error since Kotlin 1.0

// Synthetic setter `foo` is resolved to the `setFoo()` method
secondContainer.foo = sampleString
inProjected.foo = sampleString
// Error since Kotlin 2.0.0

}
```

Expand Down Expand Up @@ -731,6 +730,8 @@ class Generic<T>

// Creates an instance of Generic class with type Some<String>
fun gen() = Generic<Some<String>>()

fun takeString(g: Generic<Some<String>>) {}
```

</td>
Expand All @@ -746,8 +747,6 @@ in module one:

```kotlin
// Module three
fun takeString(g: Generic<Some<String>>) {}

fun test() {
// Triggers a warning in Kotlin 2.0.0
val z = gen()
Expand Down Expand Up @@ -805,7 +804,7 @@ fun main() {
}
```

Given the above, `a` resolves to the Kotlin property within the `Derived` Kotlin class, whereas `b` resolves to
Prior to Kotlin 2.0.0, `a` resolves to the Kotlin property within the `Derived` Kotlin class, whereas `b` resolves to
the Java field in the `Base` Java class.

In Kotlin 2.0.0, the resolution behavior in the example is consistent, ensuring that the Kotlin property supersedes the
Expand Down Expand Up @@ -863,40 +862,6 @@ fun main() {
}
```

In case a Java class contains a field and a synthetic property of the same name, the field supersedes the property. For example:

<table header-style="top">
<tr>
<td>Java</td>
<td>Kotlin</td>
</tr>
<tr>
<td>

```java
public class JavaClass {
public final String name;

public String getName() {
return name;
}
}
```

</td>
<td>

```kotlin
fun main() {
// Resolves Java field: name
JavaClass.name
}
```

</td>
</tr>
</table>

**What's the best practice now?**

If this change affects your code, consider whether you really need to use duplicate names. If you want to have Java or
Expand Down
2 changes: 1 addition & 1 deletion docs/topics/native/native-objc-interop.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ package my.library
fun foo() {}
```

You can them call the `foo()` function from Swift like this:
You can then call the `foo()` function from Swift like this:

```swift
MyLibraryUtilsKt.foo()
Expand Down
Loading

0 comments on commit 2fd81d2

Please sign in to comment.