【问题标题】:What are the minimal Gradle settings needed to support converting Kotlin files in an Android Studio App into Javascript using kotlin2js?支持使用 kotlin2js 将 Android Studio 应用程序中的 Kotlin 文件转换为 Javascript 所需的最小 Gradle 设置是什么?
【发布时间】:2018-09-29 18:36:42
【问题描述】:

目前,我有两个 build.gradle。一个用于应用程序,一个用于项目。

对于 App 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。但是,这包含 File,由于某种原因,它在我的 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

【问题讨论】:

  • 检查thisthis
  • @itsmysterybox,我已经看过那些了。使用该任务 assembleWeb 时,无法识别关键字 File。
  • @itsmysterybox,我更新了我的回复,指出我在使用这些指南时遇到的问题。
  • 尝试使用 File 关键字和同步项目。显示任何错误?
  • @itsmysterybox 是的。我什至清理了我的项目。我还尝试使缓存无效。没用。

标签: android kotlin android-gradle-plugin kotlin-android-extensions


【解决方案1】:

截至目前,我认为问题在于您无法通过 kotlin2js 将 Java 字节码编译成 Javascript。由于我的代码中有 Android 引用,并且这些引用被编译为 Java 字节码,因此我的 Kotlin 脚本无法成功转换为 Javascript。但是,我遇到了一篇有趣的文章,对任何希望构建可跨多个平台工作的基于 Kotlin 的应用程序的人都有用:https://kotlinlang.org/docs/reference/multiplatform.html

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 2012-03-04
    • 2016-10-05
    相关资源
    最近更新 更多