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

我试图在库模块中使用Kotlin,而不是在应用程序模块中使用它。 应用程序模块只使用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() } } 

{库根} / {库模块} / 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(Project)

 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(app)

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

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

 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 } 

你只能在你的库中添加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")