【问题标题】:Using Kotlin in a library module without using it in the app module在库模块中使用 Kotlin,而不在 app 模块中使用它
【发布时间】:2018-01-18 09:13:39
【问题描述】:

我正在尝试在库模块中使用 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。我希望有一个解决方案,只需添加对库的依赖项即可使整个工作正常。

【问题讨论】:

  • 如果是这样,导出 jar 或 aar 文件 android 使用它们。否则,您还必须为应用添加 kotlin 支持。
  • 如果{library root}{project root} 不相同,则库根目录中的buildscript.dependencies 不适用于您的项目。您应该将 ext.kotlin_versionclasspath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 添加到您的项目根 gradle 文件中,或者您可以将库作为 aar 文件导入。

标签: android gradle kotlin


【解决方案1】:

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

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"
}

【讨论】:

  • 这完全回答了这个问题,所以我会给你加分。我希望找到的是如何在项目中使用库而不改变项目中的任何内容,除了对该库的依赖。有了这个答案,Project gradle 仍然需要更新。
  • @NinovanHooff 除了库依赖项之外,您还有任何更改,或者您的意思是在 build.gradle (Project) 文件中?
【解决方案2】:

你只需要在你的模块构建中添加:

buildscript {
  // (Optional) ext.kotlin_version = '1.1.4-3'

  repositories {
    google()
    jcenter()
  }

  dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

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

您可以在该特定构建中声明 kotlin_version 变量,或者在项目根构建文件中声明它,因为可以从其他模块更新 kotlin 版本:

buildscript {
  ext.kotlin_version = '1.1.4-3'

  repositories {
    google()
    jcenter()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0-beta3'
  }
}

allprojects {
  repositories {
    google()
    jcenter()
  }
}

task clean(type: Delete) {
  delete rootProject.buildDir
}

【讨论】:

  • 拥有 :lib 模块和 :app 模块对我来说效果很好
【解决方案3】:

您只能在您的库中添加带有 kotlin 插件的 buildscript 部分:

buildscript {
    ext.kotlin_version = '1.1.4-2'

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

apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"

现在您可以将库包含到应用程序中,而无需修改 root build.gradle

compile project(path: ":library")

【讨论】:

  • 答案不完整。您需要在其他依赖项之前添加 apply plugin: 'com.android.library'。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 2019-07-17
相关资源
最近更新 更多