multiproject gradle build error:包不存在

时间:2013-09-02 12:01:29

标签: android build gradle android-studio

使用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
    }
}

应用/库/核心/库/ 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 个答案:

答案 0 :(得分:6)

您的app模块的build.gradle应包含(除其他外)

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

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

apply plugin: 'com.android.library'

答案 1 :(得分:4)

如果您使用Core作为库,则应在 App / libraries / Core / Core / build.gradle

中将apply plugin: 'android'更改为apply plugin: 'android-library'

答案 2 :(得分:4)

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

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

library模块添加为app模块

的依赖项

答案 3 :(得分:2)

我在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'