在 Android Studio 中创建 Kotlin 库 [英] Create a Kotlin library in Android Studio

查看:36
本文介绍了在 Android Studio 中创建 Kotlin 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 和 JVM 平台都很陌生.使用 Android Studio,我想创建一个 Android 应用程序并将我的大部分业务逻辑放在一个库中.我也想只用 Kotlin,不用 Java.

I am very new to both the Android and JVM platforms. Using Android Studio, I would like to create an Android app and put most of my business logic in a library. I would also like to just use Kotlin, no Java.

当我转到File > New Module 时,列出的选项是

When I go to File > New Module, the options listed are

  • 电话和平板模块
  • Android 库
  • 免安装应用
  • 功能模块
  • Android Wear 模块
  • Android TV 模块
  • Android Things 模块
  • 导入 Gradle 项目
  • 导入 Eclipse ADT 项目
  • 导入 .JAR/.AAR 包
  • Java 库

我可以使用 Android Library 选项创建一个基于 Kotlin 的库,但这也包括很多用于处理资源和控件的文件.

I can create a Kotlin-based library with the Android Library option, but this also includes a lot of files for dealing with resources and controls.

我只想要一个纯 Kotlin 库.创建一个最简单的方法是什么?

I just want a pure Kotlin library. What is the easiest way to create one?

  • 我可以删除 Android 库的一部分吗?
  • 我可以更改 Java 库中的某些设置吗?
  • 我可以下载一个插件,让我可以选择创建 Kotlin 库吗?

我对Java/Kotlin/Android项目中的文件组织还是有点迷糊.

I am still a bit confused the file organization in Java/Kotlin/Android projects.

推荐答案

您需要一个没有 Android 依赖项和资源的模块 - 这就是 Java 库 模块的作用,因此您首先要创建一个那些.从那里,您只需要在此模块中启用 Kotlin.

You need a module with no Android dependencies and resources - this is what a Java library module does, so you start by creating one of those. From there, you just need to enable Kotlin inside this module.

你在创建模块时得到的 build.gradle 文件是这样的:

The build.gradle file you get when you create your module is something like this:

apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

您需要添加 Kotlin 插件和标准库.如果您还没有为项目中的所有模块添加 mavenCentral 存储库,则还需要添加:

You need to add the Kotlin plugin and standard library. If you don't have the mavenCentral repository added for all modules in your project yet, you need to add that as well:

apply plugin: 'java-library'
apply plugin: 'kotlin'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

repositories {
    mavenCentral()
}

这是假设您在项目级别的 build.gradle 文件中声明了一个 kotlin_version,这是 Android 项目的常用方式,我相信这就是 Android如果您勾选复选框以使用 Kotlin,Studio 将配置一个项目.

This is assuming that you have a kotlin_version declared in your project level build.gradle file, which is the usual way for Android projects, and I believe that's how Android Studio configures a project if you tick the checkbox to use Kotlin.

通过对库的 build.gradle 进行这些更改,您可以从库的源文件夹 (/src/main/java/your/package/name),然后开始添加 Kotlin 文件.如果您愿意,还可以将 /src/main/java 文件夹重命名为 /src/main/kotlin.或者您可以同时使用这两个文件夹,这完全取决于您.

With those changes to your library's build.gradle, you can remove the automatically generated MyClass.java file from your library's source folder (/src/main/java/your/package/name), and start adding Kotlin files instead. If you'd like, you can also rename the /src/main/java folder to /src/main/kotlin. Or you can use both folders, this is entirely up to you.

这篇关于在 Android Studio 中创建 Kotlin 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆