androidandroid-gradle-pluginbuild.gradleandroid-buildandroid-module

Unable to build : How to add Android Build Config for new Module


Now i am converting my android code to modularized architectural approach. Facing issues when trying add a dependency on "app" module from "chat" module.

I have the following build config for the "app" module.

android {

lintOptions {
    checkReleaseBuilds false
    abortOnError false
}

signingConfigs {
    companydevconfig {
        keyAlias 'company'
        keyPassword '123456'
        storeFile file('../app/jksFils/company_dev.jks')
        storePassword '123456'
    }
    companyqaconfig {
        keyAlias 'company'
        keyPassword '123456'
        storeFile file('../app/jksFils/company_qa.jks')
        storePassword '123456'
    }
    companyprodconfig {
        keyAlias 'company'
        keyPassword '123456'
        storeFile file('../app/jksFils/release.keystore')
        storePassword '123456'
    }
}

compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
    applicationId "com.company.employee.dev"
    minSdkVersion 21
    targetSdkVersion 28
    versionCode 1
    versionName "1.13"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
aaptOptions {
    cruncherEnabled = false
}
testOptions {
    unitTests.returnDefaultValues = true
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        testCoverageEnabled true
    }

}

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

dataBinding {
    enabled = true
}
packagingOptions {
    exclude 'META-INF/ASL2.0'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/rxjava.properties'
}
flavorDimensions "company"
productFlavors {
    dev {
        dimension "company"
        applicationId "com.company.employee.dev"
        versionCode 277
        versionName "2.0.0.16"
        signingConfig signingConfigs.companydevconfig

        buildConfigField 'String', 'BASEURL', '"https://dev.company.com"'
    }
    qa {
        dimension "company"
        applicationId "com.company.employee.qa"
        versionCode 225
        versionName "2.0.2.2"
        signingConfig signingConfigs.companyqaconfig

        buildConfigField 'String', 'BASEURL', '"https://qa.company.com"'

    }
    prod {
        dimension "company"
        applicationId "com.company.employee.prod"
        versionCode 38
        versionName "1.5.20"
        signingConfig signingConfigs.companyprodconfig

        buildConfigField 'String', 'BASEURL', '"https://cloud.company.com"'

    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
    unitTests.returnDefaultValues = true
    unitTests.all {
        setIgnoreFailures(true)
        jacoco {
            includeNoLocationClasses = true
        }
    }
  }
}

Now i have added a new module "chat". And it has following code in build config.

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

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"

defaultConfig {
    applicationId "com.company.employee.chat"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
    }
}
sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

dataBinding {
    enabled true
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
    jvmTarget = "1.8"
}
}

dependencies {
implementation project(':app')

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

When i try to build i get following Error.

ERROR: Unable to resolve dependency for ':chat@debug/compileClasspath': Could not 
resolve project :app.
Show Details
Affected Modules: chat


ERROR: Unable to resolve dependency for ':chat@debugAndroidTest/compileClasspath': Could not 
resolve project :app.
Show Details
Affected Modules: chat


ERROR: Unable to resolve dependency for ':chat@debugUnitTest/compileClasspath': Could not 
resolve project :app.
Show Details
Affected Modules: chat


ERROR: Unable to resolve dependency for ':chat@release/compileClasspath': Could not resolve 
project :app.
Show Details
Affected Modules: chat


ERROR: Unable to resolve dependency for ':chat@releaseUnitTest/compileClasspath': Could not 
resolve project :app.
Show Details
Affected Modules: chat

Solution

  • Here are some things to consider

    1. There are differences between App Module and Library Module.

    Library Module is complied to .aar/file. However, App Module is Compiled to APK. This means that you can not import App Module as dependency to Library Module. Therefore, Remove This line from Library Module:

     implementation project(':app')
    
    1. Make sure the library is listed at the top of your settings.gradle.

    Open settings.gradle of the App Module and make sure there is your library listed

    include ':app', ':chat'
    
    1. import the Library Module to your App Module Build Gradle

    import your Library Module as dependency

    dependencies {
    implementation project(':chat')
    }
    

    Most Importantly Have a look at :Create an Android library