8

我在使用 gradle 构建多项目时遇到了一些问题。我阅读了所有类似的问题,但没有任何帮助。我的项目结构如下:

App/
    settings.gradle
    app/
        build.gradle
    libraries/
        Core(git submodule)/
            Core/
                build.gradle
            libraries/
                ZBarLibrary/
                    build.gradle
            settings.gradle

应用程序/settings.gradle

include ':App', ':libraries:Core', ':libraries:ZBarLibrary'
project(':libraries:Core').projectDir = new File(settingsDir, 'libraries/Core/Core')
project(':libraries:ZBarLibrary').projectDir = new File(settingsDir, 'libraries/Core/libraries/ZBarLibrary')

应用程序/应用程序/build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 16
    }
}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile project(':libraries:Core')
}

应用程序/库/核心/核心/build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    instrumentTestCompile "junit:junit:4.5+"
    compile project(':libraries:ZBarLibrary')
    compile fileTree(dir: 'libs', include: '*.jar')

}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 16
    }
}

应用程序/库/Core/libraries/ZBarLibrary/build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android-library'

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 16
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

App目录中的命令“gradle build”产生错误:“package com...不存在”。这个包在 app 模块中使用,但它在 App/libraries/Core/Core/src/main/java 中找到。你可以帮帮我吗?

4

4 回答 4

6

您的应用程序模块的 build.gradle 应该包含(除其他外)

apply plugin: 'com.android.application'
dependencies {
    compile project(':my_library_module')
}

你的库模块的 build.gradle 文件应该包含

apply plugin: 'com.android.library'
于 2014-09-03T11:52:29.573 回答
4

如果您使用 Core 作为库,则应更改为apply plugin: 'android'App /libraries/Core/Core/build.gradleapply plugin: 'android-library'

于 2013-09-05T07:20:17.897 回答
4

为了解决未找到包的错误,在我添加的app模块build.gradle文件中

dependencies {
    // ...
    compile project(':library')
}

它将library模块作为依赖项添加到app模块

于 2014-08-25T21:19:21.557 回答
3

我对 Gradle 6.4(多模块项目)和 Java 13 有同样的问题

项目结构:

app:
    backend:
        /src
        build.gradle
    base:
        /src
        build.gradle
    build.gradle
    settings.gradle

主类包含在base/build.gradle中,并且base-module依赖于backend-module

解决方案:有必要将以下行添加到backend/build.gradle

jar {
    enabled = true
}
bootJar {
    enabled = false
}

并检查base/build.gradle是否有以下行:

bootJar {
    archivesBaseName = 'base'
}
dependencies {
    implementation project(":backend")
}

以防万一,对于 gradle 中的新手,请列出settings.gradle 文件

rootProject.name = 'app'
include 'backend', 'base'
于 2020-05-15T14:08:22.073 回答