Home » Android » android – multiproject gradle build error: package does not exist

android – multiproject gradle build error: package does not exist

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have some problems with building multiproject with gradle. I read all similar questions but nothing help. The structure of my projects looks like:

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

App/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')

App/app/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')
}

App/libraries/Core/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
    }
}

App/libraries/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']
        }
    }
}

Command “gradle build” in App directory generates error: “package com… does not exist”. This package is used in app module but it is find in App/libraries/Core/Core/src/main/java. Could you help me?

How to&Answers:

If you’re using Core as library you should change apply plugin: 'android' to apply plugin: 'android-library' in App/libraries/Core/Core/build.gradle

Answer:

Your app module’s build.gradle should contain (among other things)

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

Your library module’s build.gradle file should contain

apply plugin: 'com.android.library'

Answer:

To resolve the package not found errors, in the app module’s build.gradle file I added

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

which adds the library module as a dependency to the app module

Answer:

I have the same problem with Gradle 6.4 (multi-module project) and Java 13

Project structure:

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

Main class is contained in base/build.gradle and also base-module depends on backend-module.

SOLUTION: It is necessary to add following rows to backend/build.gradle

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

and check that base/build.gradle has the following rows:

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

Just in case, for newcomers in gradle put the listing of settings.gradle file:

rootProject.name = 'app'
include 'backend', 'base'