支持使用 kotlin2js 将 Android Studio 应用程序中的 Kotlin 文件转换为 Javascript 所需的最小 Gradle 设置是什么?
What are the minimal Gradle settings needed to support converting Kotlin files in an Android Studio App into Javascript using kotlin2js?
目前,我有两个build.gradles。一份用于应用程序,一份用于项目。
对于应用 build.gradle,我有:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 19
buildToolsVersion '28.0.2'
defaultConfig {
applicationId "someId"
minSdkVersion 11
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile files('libs/someSDK.jar')
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
对于项目 build.gradle,我有:
buildscript {
ext.kotlin_version = '1.2.71'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
allprojects {
repositories {
mavenCentral()
jcenter()
}
}
sourceSets {
main.kotlin.srcDirs += "${projectDir}/src/main/java/pathToFilesConvertedFromJavaToKotlin"
}
compileKotlin2Js {
kotlinOptions.metaInfo = true
kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js"
kotlinOptions.sourceMap = true
kotlinOptions.moduleKind = 'commonjs'
kotlinOptions.main = "call"
}
ext {
minSdkVersion = 15
targetSdkVersion = 23
compileSdkVersion = 23
buildToolsVersion = '23.0.3'
xmlunitVersion = '1.6'
junitVersion = '4.12'
mockitoVersion = '1.10.19'
hamcrestVersion = '1.3'
}
当我编译 运行 时,应用程序运行正常并且没有出现错误。但是,我没有看到应该从那些 Kotlin 文件生成的 javascript 文件。
关于原因有什么想法吗?
请注意,我已尝试使用:http://kotlinlang.org/docs/tutorials/javascript/getting-started-gradle/getting-started-with-gradle.html 作为指南。此示例的问题在于它建议使用下面的 taskAssemble。但是,这包含文件,出于某种原因,它在我的 build.gradle 中未被识别为关键字。
task assembleWeb(type: Sync) {
configurations.compile.each { File file ->
from(zipTree(file.absolutePath), {
includeEmptyDirs = false
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") ||
!path.startsWith("META-INF/"))
}
})
}
from compileKotlin2Js.destinationDir
into "${projectDir}/web"
dependsOn classes
}
assemble.dependsOn assembleWeb
截至目前,我认为问题在于您无法通过 kotlin2js 将 Java 字节代码编译为 Java 脚本。由于我的代码中有 Android 引用,并且这些引用被编译为 Java 字节代码,因此我的 Kotlin 脚本无法成功转换为 Java 脚本。但是,我发现了一篇有趣的文章,对于希望构建可跨多个平台运行的基于 Kotlin 的应用程序的任何人都可能有用:https://kotlinlang.org/docs/reference/multiplatform.html
目前,我有两个build.gradles。一份用于应用程序,一份用于项目。
对于应用 build.gradle,我有:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 19
buildToolsVersion '28.0.2'
defaultConfig {
applicationId "someId"
minSdkVersion 11
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile files('libs/someSDK.jar')
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
对于项目 build.gradle,我有:
buildscript {
ext.kotlin_version = '1.2.71'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
allprojects {
repositories {
mavenCentral()
jcenter()
}
}
sourceSets {
main.kotlin.srcDirs += "${projectDir}/src/main/java/pathToFilesConvertedFromJavaToKotlin"
}
compileKotlin2Js {
kotlinOptions.metaInfo = true
kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js"
kotlinOptions.sourceMap = true
kotlinOptions.moduleKind = 'commonjs'
kotlinOptions.main = "call"
}
ext {
minSdkVersion = 15
targetSdkVersion = 23
compileSdkVersion = 23
buildToolsVersion = '23.0.3'
xmlunitVersion = '1.6'
junitVersion = '4.12'
mockitoVersion = '1.10.19'
hamcrestVersion = '1.3'
}
当我编译 运行 时,应用程序运行正常并且没有出现错误。但是,我没有看到应该从那些 Kotlin 文件生成的 javascript 文件。
关于原因有什么想法吗?
请注意,我已尝试使用:http://kotlinlang.org/docs/tutorials/javascript/getting-started-gradle/getting-started-with-gradle.html 作为指南。此示例的问题在于它建议使用下面的 taskAssemble。但是,这包含文件,出于某种原因,它在我的 build.gradle 中未被识别为关键字。
task assembleWeb(type: Sync) {
configurations.compile.each { File file ->
from(zipTree(file.absolutePath), {
includeEmptyDirs = false
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") ||
!path.startsWith("META-INF/"))
}
})
}
from compileKotlin2Js.destinationDir
into "${projectDir}/web"
dependsOn classes
}
assemble.dependsOn assembleWeb
截至目前,我认为问题在于您无法通过 kotlin2js 将 Java 字节代码编译为 Java 脚本。由于我的代码中有 Android 引用,并且这些引用被编译为 Java 字节代码,因此我的 Kotlin 脚本无法成功转换为 Java 脚本。但是,我发现了一篇有趣的文章,对于希望构建可跨多个平台运行的基于 Kotlin 的应用程序的任何人都可能有用:https://kotlinlang.org/docs/reference/multiplatform.html