在Android Studio 3.1中将问题从Gradle 3迁移到4

时间:2018-04-10 10:32:49

标签: android android-studio gradle android-gradle

我有一个库项目(myLib)和一个用于开发库的测试应用程序。当我使用gradle 3.3时,所有内容都被正确唤醒,但是当我更新到gradle 4时,我开始遇到问题。

库有不同的风格,可以在发布模式和调试中编译。我必须添加一个名为flavorDimensions的{​​{1}}才能使用gradle 4编译库。

测试应用程序依赖于模块default,我必须更改gradle文件中的依赖项部分,如下所示:

myLib

但是当我尝试编译时,我收到以下错误:

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')
}

当我点击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 时它没有显示任何内容,所以我尝试使用命令行运行gradle并得到一个不同的错误:

Show Details

迁移到新的gradle版本时我缺少什么?

提前致谢。

编辑:

MyLibrary gradle文件:

$ ./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

应用程序gradle文件:

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'
}

1 个答案:

答案 0 :(得分:2)

问题在于app模块并不知道要使用哪种mylibrary的风格。

如果您需要将您的应用与每个图书馆的用户一起使用,解决此问题的方法是将您的风味尺寸和产品复制到应用的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
        }
    }
...
}

这将使您可以选择将您的应用程序与库中可用的每种风格一起使用,当您将应用程序的风格更改为构建变体时,它将自动选择好的库的风格。 enter image description here ==> enter image description here