Kotlin Gradle多级项目问题

时间:2018-10-17 21:04:59

标签: android gradle intellij-idea kotlin

我想在移动端和后端都使用通用模块。在后端模块中,一切正常,但是我无法在移动设备上使用通用模块。在构建移动项目时,出现错误:Project with path ':common' could not be found in project ':app'

项目树:

kibar
    .git
    gradle
    idea
    backend
        src
        build.gradle
    common
        src
        build.gradle
    mobile
        .gradle
        .idea
        app
           src
           build.gradle
           proguard-rules.pro
        build.gradle
        settings.gradle
    settings.gradle

kibar:settings.gradle

rootProject.name = 'kibar'
include 'backend', 'mobile', 'common'

common:build.gradle

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.2.71'
}

repositories {
    mavenCentral()
}

sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin'
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:1.2.71"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

后端:build.gradle

buildscript {
    ext.kotlin_version = '1.2.71'

    repositories {
        jcenter()
        mavenCentral()
        maven { url "http://dl.bintray.com/kotlin/kotlin-eap" }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
apply plugin: 'application'

sourceCompatibility = 1.8
mainClassName = "App"

repositories {
    jcenter()
    mavenCentral()
    maven { url "https://dl.bintray.com/kotlin/exposed" }
    maven { url "http://dl.bintray.com/kotlin/kotlin-eap" }
}

dependencies {
    compile project(":common")
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

kotlin {
    experimental {
        coroutines "enable"
    }
}

idea {
    module {
        sourceDirs += files('build/generated/source/kapt/main', 'build/generated/source/kaptKotlin/main')
        generatedSourceDirs += files('build/generated/source/kapt/main', 'build/generated/source/kaptKotlin/main')
    }
}

sourceSets {
    main.resources.srcDir('conf')
    main.java.srcDirs += 'src/main/java'
    main.java.srcDirs += 'src/main/kotlin'
    test.java.srcDirs += 'src/test/kotlin'
}

mobile:build.gradle

buildscript {
    ext{
        kotlin_version          = "1.2.71"
        lifecycle_version       = "2.0.0"
    }

    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url "https://maven.google.com" }
        maven { url "https://jitpack.io" }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0-alpha13'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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

mobile:settings.gradle

include ':app'

mobile.app:build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    buildToolsVersion = '28.0.3'
    compileSdkVersion 28

    defaultConfig {
        versionCode 1
        versionName "1.0"
        applicationId "com.example"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        minSdkVersion 15
        targetSdkVersion 28
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }
}

androidExtensions {
    experimental = true
}

kapt {
    generateStubs = true
}

kotlin {
    experimental {
        coroutines "enable"
    }
}

dependencies {
    compile project(':common')
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.30.2'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.30.2'

    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.core:core-ktx:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.android.material:material:1.0.0'

    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
}

1 个答案:

答案 0 :(得分:1)

在整个项目中,根目录级别应该只有一个settings.gradle文件。删除mobile/settings.gradle

Gradle多项目构建需要一个位于项目根目录中的settings.gradle文件,并且该settings.gradle必须声明所有子项目。在您的情况下,您需要在mobile:app文件中添加kibar:settings.gradle子项目:

rootProject.name = 'kibar'
include 'backend', 'mobile:app', 'common'

以供参考:https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:settings_file