android - Kotlin gradle 多级项目问题

标签 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'

常见: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'
}

手机: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" }
    }
}

手机: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"
}

最佳答案

你应该只有一个 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

关于android - Kotlin gradle 多级项目问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52863602/

相关文章:

gradle 为来自 gradle 的所有项目设置一个全局属性

android - 将Android Studio项目导入Intellij错误的gradle版本

android - NDK 中的共享内存区域

android - 使用TextInputLayout时出现gradle错误

android - Gradle 依赖项 : Get latest snapshot from remote repo

intellij-idea - 我可以将 IntelliJ 差异查看器/合并工具配置为 tortoiseSVN 中的外部工具吗?

intellij-idea - springboot & thymeleaf 项目中资源下的static 和templates 文件夹有什么区别?

php - 如何将来自服务器的数据更改为其他语言(php mysql)

java - 一次或两次触摸屏幕时,我如何决定使用 Java 发送哪个参数?

android - React Native 中带有 AsyncStorage 的 GetItem 不起作用