android - 在库模块中使用 Kotlin,而不在 app 模块中使用它

我正在尝试在库模块中使用 Kotlin,而不在 app 模块中使用它。 app 模块仅使用 Java,不使用库中的任何 Kotlin 类。 Gradle 不会编译:

Error:(2, 1) A problem occurred evaluating project ':<Library>'.
> Plugin with id 'kotlin-android' not found.

我为包含 Kotlin 所做的更改:

{库根目录}/build.gradle

buildscript {
ext.kotlin_version = '1.1.3-2'

repositories {
    jcenter()
}
dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    ...
}

allprojects {
repositories {
    jcenter()
}
}

{library root}/{library module}/build.gradle

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
...

dependencies{
    ...
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

当我将相同的内容添加到应用程序模块时,项目编译没有问题,但我想避免将其添加到应用程序模块中,因为我想在多个应用程序中使用此库而不对这些应用程序进行代码更改应用程序

使用的 Gradle 版本:3.3 android gradle 插件版本:2.3.3

编辑:@Jushua 的回答有效,但仍需要更新项目根 build.gradle。我希望有一个解决方案,只需添加对库的依赖项即可使整个工作正常。

最佳答案

我可以毫无问题地做到这一点。

build.gradle(项目)

buildscript {
    ext.kotlin_version = "1.1.4"
    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:2.3.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

build.gradle(应用程序)

apply plugin: "com.android.application"

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    androidTestCompile("com.android.support.test.espresso:espresso-core:2.2.2", {
        exclude group: "com.android.support", module: "support-annotations"
    })
    compile "com.android.support:appcompat-v7:26.0.1"
    testCompile "junit:junit:4.12"
    compile project(path: ":library")
}

build.gradle(库)

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 13
        versionName "1.1.4"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    androidTestCompile("com.android.support.test.espresso:espresso-core:2.2.2", {
        exclude group: "com.android.support", module: "support-annotations"
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "com.android.support:appcompat-v7:26.0.1"
    testCompile "junit:junit:4.12"
}

https://stackoverflow.com/questions/45608316/

相关文章:

generics - Scala 2.10 中的具体泛型

android - 带有 Kotlin 的 Android 中的 HTTP 请求

kotlin - 如何使用 toString 扩展数据类

kotlin - 如何获取给定密封类的所有子类?

parallel-processing - Kotlin 集合的并行操作?

kotlin - 如何组织 Kotlin 扩展方法

kotlin - Kotlin 中 with 和 run 的区别

android - Kotlin : safe lambdas (no memory leak)?

list - Kotlin - 创建具有重复元素的可变列表

java - Kotlin 中的私有(private)构造函数