2017-08-10 97 views
20

我正在试图在库模块中使用Kotlin,而未在应用程序模块中使用它。应用程序模块只使用Java,不使用库中的任何Kotlin类。摇篮不会编译hoever:在库模块中使用Kotlin而不在应用程序模块中使用它

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

变化我制成包括科特林:

{库根} /的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" 
} 

当我添加相同的应用模块,项目编译没有问题,但我会升IKE避免应用模块中添加它,因为我想使用在多个应用这个库,而不更改代码将这些应用程序

使用

摇篮版本:3.3 安卓gradle这个插件版本:2.3.3

编辑:@Jushua的答案有用,但它仍然需要更新项目根build.gradle。我希望有一个解决方案,只需要添加库的依赖关系就可以使整个系统正常工作。

+1

如果是这样,导出jar或aar文件android使用它们。否则,您必须将kotlin支持添加到应用程序中。 – chandil03

+1

如果'{library root}'和'{project root}'不同,你的''''''''''root'''''''''''''''您应该将'ext.kotlin_version'和'classpath'org.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version“'添加到您的项目根gradle文件中,或者您可以将库导入为aar文件。 – lllyct

回答

4

我能够做到这一点,没有任何问题。

的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(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" 
} 
+0

这严格限制了这个问题,所以我会奖励你的观点。我希望找到的东西是,如何在项目中使用库而不改变项目中的任何内容,除了依赖于该库。有了这个答案,Project gradle仍然需要更新。 –

+0

@NinovanHooff除了库依赖关系之外,你有任何改变,或者你的意思是在build.gradle(Project)文件中? – crgarridos

-1

只能在你的库中添加与科特林插件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" 

现在你可以包含库的应用程序,而无需根修改的build.gradle

compile project(path: ":library") 
+0

这不适合我:https://imgur.com/a/slxb8 – cesards

+0

答案并不完整。您需要在其他依赖项之前添加apply插件:'com.android.library'。 – cesards

1

你只需要添加您的模块装配:

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 
} 
相关问题