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

Nin*_*off 28 android gradle kotlin

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

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

我做的改变包括Kotlin:

{library root}/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()
}
}
Run Code Online (Sandbox Code Playgroud)

{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"
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Jos*_*hua 7

我能够毫无问题地做到这一点.

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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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")
}
Run Code Online (Sandbox Code Playgroud)

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"
}
Run Code Online (Sandbox Code Playgroud)


ces*_*rds 5

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

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'    
Run Code Online (Sandbox Code Playgroud)

您可以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
}
Run Code Online (Sandbox Code Playgroud)