Joan P.S
Joan P.S

Reputation: 1573

Migrating issues from Gradle 3 to 4 in Android Studio 3.1

I have a library project (myLib) and a test app that I use to develop the library. While I was using gradle 3.3 everything woked properly, but when I updated to gradle 4 I started getting issues.

The library has different flavors and be compiled in release mode and debug. I had to add a flavorDimensions called default to be able to compile the library using gradle 4.

The test app depends on the module myLib which I had to change the dependencies section in the gradle file as follow:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:23.3.0'
    implementation 'com.android.support:design:23.3.0'

    releaseApi project(path: ':myLib', configuration: 'flavorARelease')
    debugApi project(path: ':myLib', configuration: 'flavorADebug')

    // Old code
    // debugCompile project(path: ':myLib', configuration: 'flavorADebug')
    // releaseCompile project(path: ':mylib', configuration: 'flavorARelease')
}

But when I try to compile I get the following error:

Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve project :myLib.
Open File
Show Details


Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve project :myLib.
Open File
Show Details


Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve project :myLib.
Open File
Show Details


Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve project :myLib.
Open File
Show Details


Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve project :myLib.
Open File
Show Details

When I click Show Details It shows nothing, so I tried to run gradle using comand line and I get a different error:

$ ./gradlew :app:assembleRelease

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:preReleaseBuild'.
> Could not resolve all task dependencies for configuration ':app:releaseRuntimeClasspath'.
   > Could not resolve project :myLib.
     Required by:
         project :app
      > Project :app declares a dependency from configuration 'releaseApi' to configuration 'normalRelease' which is not declared in the descriptor for project :myLib.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s

What am I missing in when migrating to the new gradle version?

Thanks in advance.


Edited:

MyLibrary gradle file:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "default"

    productFlavors {
        flavorA {
            dimension "default"
        }
        flavorB {
            dimension "default"
        }
        flavorC {
            dimension "default"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Application gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.myapplication"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            matchingFallbacks = ['default']

        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation project(':mylibrary')
}

Upvotes: 1

Views: 1693

Answers (1)

xiaomi
xiaomi

Reputation: 6703

The problem is that the app module doesn't know which mylibrary's flavor to use.

If you need to use your app with each of the library's flvors, a way to correct this is to copy your flavor dimensions and products into the app's build.gradle

app build.gradle

android {
...
    flavorDimensions "default"

    productFlavors {
        flavorA { //This flavor will automatically use mylibrary flavorA
            dimension "default" //With only one dimension, this line is optional
        }
        flavorB { //This flavor will automatically use mylibrary flavorB
            dimension "default" //With only one dimension, this line is optional
        }
        flavorC { //This flavor will automatically use mylibrary flavorC
            dimension "default" //With only one dimension, this line is optional
        }
    }
...
}

This will give you the choice to use your app with each flavor available on your library, and when you change the app's flavor into the build variants, it will automatically select the good library's flavor. enter image description here ==> enter image description here

Upvotes: 3

Related Questions