【问题标题】:Android Studio 3.0 Compile Issue (Cannot choose between Configurations)Android Studio 3.0 编译问题(无法在配置之间进行选择)
【发布时间】:2017-08-14 17:29:19
【问题描述】:

最新 3.0 版本(Beta 2)的问题 我的项目有 1 个第三方的子模块,所以我只能访问他们的 build.gradle。

我的项目有 3 种风格,snap、uat、production。每个都有 2 种构建类型,调试和发布。当我尝试构建时,我得到了这个。

Error:Cannot choose between the following configurations of project :lp_messaging_sdk:
  - debugApiElements
  - debugRuntimeElements
  - releaseApiElements
  - releaseRuntimeElements
All of them match the consumer attributes:
  - Configuration 'debugApiElements':
      - Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
      - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
      - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
      - Found org.gradle.api.attributes.Usage 'java-api' but wasn't required.
  - Configuration 'debugRuntimeElements':
      - Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required.
      - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
      - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
      - Found org.gradle.api.attributes.Usage 'java-runtime' but wasn't required.
  - Configuration 'releaseApiElements':
      - Found com.android.build.api.attributes.BuildTypeAttr 'release' but wasn't required.
      - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
      - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
      - Found org.gradle.api.attributes.Usage 'java-api' but wasn't required.
  - Configuration 'releaseRuntimeElements':
      - Found com.android.build.api.attributes.BuildTypeAttr 'release' but wasn't required.
      - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required.
      - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
      - Found org.gradle.api.attributes.Usage 'java-runtime' but wasn't required.

我读到子模块和构建类型存在问题,但后来读到它已修复。 您必须向子模块 build.gradle 添加相同的构建类型或其他内容,然后添加

buildTypeMatching  'debug', 'release'

然而,当我这样做时,我得到了这个错误,

Error:Could not select value from candidates [debug, release] using AlternateDisambiguationRule.BuildTypeRule.

apply plugin: 'com.android.application'

android {

    repositories {
        flatDir {
            dirs project(':lp_messaging_sdk').file('aars')
        }
    }

    // Android parameters
    compileSdkVersion = 26
    buildToolsVersion = '26.0.1'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    dexOptions {
        preDexLibraries true
    }

    defaultConfig {
        minSdkVersion 19
        versionName buildName
        versionCode buildVersion
        multiDexEnabled true
        resConfigs "en", "fr", "fr-rCA"
    }

    signingConfigs {
        release {

        }
    }

    flavorDimensions "default"

    productFlavors {
        snap {
            ext.betaDistributionGroupAliases = "INTERNAL"
            ext.betaDistributionReleaseNotesFilePath = 'changelog.txt'
            ext.betaDistributionNotifications = true
            dimension "default"
        }

        uat {
            ext.betaDistributionGroupAliases = "INTERNAL"
            ext.betaDistributionNotifications = true
        }

        production {
        }
    }

    buildTypes {
        debug {
            versionNameSuffix createVersionNameSuffix()
            applicationIdSuffix '.debug'
            minifyEnabled true
            testCoverageEnabled false
            buildConfigField "String", "PLAY_STORE_VERSION_NAME", '"' + PLAY_STORE_VERSION_NAME + '"'
            // Workaround for : https://code.google.com/p/android/issues/detail?id=212882
            proguardFiles fileTree(dir: 'proguard', include: ['*.pro']).asList().toArray()
            ext.enableCrashlytics = false
        }

        release {
            versionNameSuffix createVersionNameSuffix()
            minifyEnabled true
            testCoverageEnabled = false
            signingConfig signingConfigs.release
            buildConfigField "String", "PLAY_STORE_VERSION_NAME", '"' + PLAY_STORE_VERSION_NAME + '"'
            // Workaround for : https://code.google.com/p/android/issues/detail?id=212882
            proguardFiles fileTree(dir: 'proguard', include: ['*.pro']).asList().toArray()
        }

    }

    //Used to ignore duplicated entries added to meta-inf
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }

    dexOptions {
        javaMaxHeapSize "2048m"
        dexInProcess true
    }

    lintOptions {
        abortOnError true
        xmlReport true
        htmlReport true
        disable 'MissingTranslation', 'InvalidPackage'
        disable 'GradleCompatible', 'GradleCompatible'
        disable 'NewApi', 'NewApi'
        disable 'GradleDependency'
        disable 'UnusedResources'
        disable 'IconDensities'
        disable 'TypographyDashes'
        disable 'ContentDescription'
        htmlOutput file("$project.buildDir/reports/lint/lint-result.html")
        xmlOutput file("$project.buildDir/reports/lint/lint-result.xml")
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }
}

greendao {
    schemaVersion 13
    targetGenDir 'src/main/java/'
}

ext.betaDistributionReleaseNotes = System.getenv("CHANGELOG")

def createVersionNameSuffix() {
    def buildNumber = System.env.BUILD_NUMBER
    def buildTimestamp = new Date().format('HH:mm dd/MM/yy')
    return buildNumber ? " ($buildNumber)" : " ($buildTimestamp)"
}

def getBuildVersionFromName(String buildName) {
    List data = buildName.tokenize(".")
    String resultString = "19";

    for (String s : data) {
        resultString += s;
    }

    if (System.env.BUILD_NUMBER) {
        resultString += System.env.BUILD_NUMBER
    }

    return Integer.parseInt(resultString);
}

//Verify the app before creating a Pull Request
task verifyPR
verifyPR.dependsOn('clean')
verifyPR.dependsOn('lint')
verifyPR.dependsOn('checkstyle')
verifyPR.dependsOn('pmd')
verifyPR.dependsOn('testSnapDebugUnitTest')

dependencies {
    // Android Dependencies
    compile 'com.android.support:appcompat-v7:26.0.1'
    compile 'com.android.support:design:26.0.1'
    compile 'com.android.support:recyclerview-v7:26.0.1'
    compile 'com.android.support:multidex:1.0.2'

    // Dagger Dependencies
    apt 'com.google.dagger:dagger-compiler:2.11'
    compile 'org.glassfish:javax.annotation:10.0-b28'
    compile 'com.google.dagger:dagger:2.11'

    // Rx Dependencies
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.3.0'
    compile 'com.jakewharton.rxbinding:rxbinding-appcompat-v7:0.4.0'
    compile 'com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0'
    compile 'com.squareup.whorlwind:whorlwind:1.0.1'
    compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar'
    compile 'com.jenzz:RxAppState:2.0.0'

    // Tools
    compile 'com.crashlytics.sdk.android:crashlytics:2.6.5'

    // ButterKnife
    compile 'com.jakewharton:butterknife:8.4.0'

    // Google Maps
    compile 'com.google.android.gms:play-services-maps:11.0.4'
    compile "com.google.android.gms:play-services-analytics:11.0.4"
    compile 'com.google.android.gms:play-services-location:11.0.4'
    compile 'com.google.android.gms:play-services-places:11.0.4'
    compile 'com.google.android.gms:play-services-gcm:11.0.4'

    // Geofence
    compile('pl.charmas.android:android-reactive-location:0.10@aar') {
        transitive = true
    }

    // Retrofit
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'

    // OKHTTP
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.7.5'

    // Libphonenumber
    compile 'com.googlecode.libphonenumber:libphonenumber:7.3.2'

    // UI
    compile 'com.tubb.smrv:swipemenu-recyclerview:5.0.2'

    // EventBus
    compile 'org.greenrobot:eventbus:3.0.0'

    // Database
    compile 'org.greenrobot:greendao:3.2.0'

    // Chuck HTTP Inspector
    debugCompile 'com.readystatesoftware.chuck:library:1.0.4'
    releaseCompile 'com.readystatesoftware.chuck:library-no-op:1.0.4'

    // ViewPager Indicator
    compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'

    // Amplitude
    compile 'com.amplitude:android-sdk:2.13.2'

    // TESTS
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.10.19"
    testCompile "org.powermock:powermock-module-junit4:1.6.5"
    testCompile "org.powermock:powermock-module-junit4-rule:1.6.4"
    testCompile "org.powermock:powermock-api-mockito:1.6.5"
    testCompile "org.powermock:powermock-classloading-xstream:1.6.4"

    compile project(':lp_messaging_sdk')
}

这里是第 3 方库 build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 250
        versionName "2.5.0"
    }

    flavorDimensions "default"

    productFlavors {
        snap {
            ext.betaDistributionGroupAliases = "INTERNAL"
            ext.betaDistributionReleaseNotesFilePath = 'changelog.txt'
            ext.betaDistributionNotifications = true
            dimension "default"
        }

        uat {
            ext.betaDistributionGroupAliases = "INTERNAL"
            ext.betaDistributionNotifications = true
        }

        production {

        }
    }

    signingConfigs {
        release {

        }
    }

    buildTypeMatching 'snap', 'debug', 'release'

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            minifyEnabled true
            testCoverageEnabled false
            buildConfigField "String", "PLAY_STORE_VERSION_NAME", '"' + PLAY_STORE_VERSION_NAME + '"'
            // Workaround for : https://code.google.com/p/android/issues/detail?id=212882
            proguardFiles fileTree(dir: 'proguard', include: ['*.pro']).asList().toArray()
            ext.enableCrashlytics = false
        }

        release {
            minifyEnabled true
            testCoverageEnabled = false
            signingConfig signingConfigs.release
            buildConfigField "String", "PLAY_STORE_VERSION_NAME", '"' + PLAY_STORE_VERSION_NAME + '"'
            // Workaround for : https://code.google.com/p/android/issues/detail?id=212882
            proguardFiles fileTree(dir: 'proguard', include: ['*.pro']).asList().toArray()
        }
    }

    defaultConfig {
        consumerProguardFiles 'proguard.cfg'
    }

    repositories {
        flatDir {
            dirs 'aars'
        }
    }

    lintOptions {
        disable 'InvalidPackage'
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:26.0.1'
    compile 'com.android.support:design:26.0.1'
    compile 'com.android.support:recyclerview-v7:26.0.1'
    compile 'com.android.support:percent:26.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.neovisionaries:nv-websocket-client:1.31'
    compile 'com.squareup.okhttp3:okhttp:3.8.0'

    compile(name: 'infra', ext: 'aar')
    compile(name: 'messaging', ext: 'aar')
    compile(name: 'messaging_ui', ext: 'aar')
    compile(name: 'ui', ext: 'aar')
}

有人知道我该如何解决这个问题吗? 谢谢

【问题讨论】:

  • 你能提供你的应用 gradle 吗?
  • @ibrahim 嗨,我提供了 build.gradle
  • 所有口味至少需要指定一个维度。发布库的 build.gradle 以便我们也可以检查它。
  • @EugenPechanec 我把它贴在上面了,谢谢

标签: android android-studio gradle project


【解决方案1】:

试试

implementation project(path: ':lp_messaging_sdk', configuration: 'default')

注意:

您可以通过将 gradle 更新为 4.3 check this 来避免此错误。

解释

使用Dependency Configurations 可以轻松定义和指定要在子项目中使用的内容。

在我的回答中,我们使用了 default 配置,这将仅向其他 Android 项目和模块发布和公开“发布”风格。

假设您只需要在演示风味或发布风味中包含此风味,it would be like

configurations {
  // Initializes placeholder configurations that the Android plugin can use when targeting
  // the corresponding variant of the app.
  demoDebugCompile {}
  fullReleaseCompile {}
  ...
}
dependencies {
  // If the library configures multiple build variants using product flavors,
  // you must target one of the library's variants using its full configuration name.
  demoDebugCompile project(path: ':lp_messaging_sdk', configuration: 'demoDebug')
  fullReleaseCompile project(path: ':lp_messaging_sdk', configuration: 'fullRelease')
  ...
}

因此,在您的情况下,您可以使用您的构建风格,这就是错误日志中出现的内容。

Cannot choose between the following configurations of project :lp_messaging_sdk

这意味着您的lp_messaging_sdk 具有各种构建配置:-

  - debugApiElements
  - debugRuntimeElements
  - releaseApiElements
  - releaseRuntimeElements

而 android-studio 告诉你,“我不能从这些不同的配置中选择一种,你能为我定义一个吗?”

您可以通过here阅读更多内容。

【讨论】:

  • @goodgamerguy 欢迎您,我更新了答案并演示了它。
  • 我不得不使用:compile project(path: ':module', configuration: 'default')
  • 我必须清理并重建项目,但它可以工作。谢谢!
  • implementation project(path: ':lp_messaging_sdk', configuration: 'default') 现在不允许,改用这个implementation project( ':lp_messaging_sdk')
【解决方案2】:

错误:无法在项目的以下配置之间进行选择......

可能存在gradle写作问题 当我改成下面的写法时就没有这样的错误了

//编译项目(':MPChartLib')

implementation project(':MPChartLib')

也许当引用依赖于其他模块时应该写在这个实现中

【讨论】:

  • 有什么可以添加到你的答案中来帮助其他人理解它的含义吗?
  • 而我用的是gradle版本的“4.1”和“4.3”,解决不了这个问题,除了改这个写法。但它有一个新的问题Error:android-apt plugin is incompatible with the Android Gradle plugin。请改用“annotationProcessor”配置。
  • 参见developer.android.com/studio/build/…中的“迁移本地模块的依赖配置”。
【解决方案3】:

如果您使用 android-apt 插件进行注释处理,请尝试删除该插件并按照the migration guide for the Android Gradle Plugin 3.0.0 中的建议将所有apt some_dependency 引用替换为annotationProcessor some_dependency

【讨论】:

  • 很棒的解决方案,谢谢!用 annotationProcessor project(:processor) 替换 kapt project(:processor)
【解决方案4】:

对于 AndroidStudio 3.0+,mainMoudle 有 buildTypes 和 buildTypes 与 libModule buildTypes 和 buildTypes 一样,它想:

主模块:

buildTypes {
    release {
        buildConfigField "boolean", "LOG_DEBUG", "false"
        zipAlignEnabled true
        shrinkResources true
        minifyEnabled true
        proguardFiles 'proguard-rules.pro'
    }

    debug {
        buildConfigField "boolean", "LOG_DEBUG", "true"
        zipAlignEnabled true
        shrinkResources false
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    debug2{

    }
}

lib模块:

buildTypes {
    release {

    }

    debug {

    }

    debug2{

    }
}

或者你可以使用matchingFallbacks解决这个click

【讨论】:

    【解决方案5】:

    当我将我的项目从 API 级别 23 更新到 27 并将 Gradle 更新到 3.1 时,出现了这个错误

    不能选择不同的配置

    所以要解决这个问题。

    替换

    compile project(':your projectName')
    

    implementation project(':projectname')
    

    在 Gradle 中,这解决了问题。

    【讨论】:

      【解决方案6】:

      在我的类似情况下,解决方案是:

      build.gradle:

      android {
          defaultConfig {
              // because I have two project flavors in that library
              missingDimensionStrategy 'project', 'myProjectName' 
              // because I have a "full" and a "debug" flavor in that library
              missingDimensionStrategy 'mode', 'full'
          }
          buildTypes {
              debug { ... }
              release { ... }
          }
      }
      dependencies {
          // because the project(path:'', configuration:'') did not work in this case
          implementation project(':myModuleName1')
          implementation project(':myModuleName2')
      }
      

      也许这有助于其他遇到类似问题的人。

      【讨论】:

        【解决方案7】:

        对我来说,在 Android Studio 3.5.2 上遇到了同样的错误,但原因不同。我试图将应用程序模块添加为库。

        我只是通过 converting the application module to a library module 解决了它。

        【讨论】:

          【解决方案8】:

          如果以下情况不成立,也会出现此错误:

          通过在 A 中包含模块 B,所有存在于 A 中的 producFlavor 都必须存在于 B 中。


          build.gradle (:app) 或 (:module-A)

          android {
              flavorDimensions "dimen"
              productFlavors {
                  someProduct {
                      dimension "dimen"
                  }
              }
          }
          
          dependencies {
              api project(path: ':module-B')
          }
          

          所以someProduct必须存在于B中

          build.gradle (:module-B)

          android {
              flavorDimensions "dimen"
              productFlavors {
                  someProduct {
                      dimension "dimen"
                  }
              }
          }
          

          GL

          【讨论】:

            【解决方案9】:

            我的问题是我正在重命名输出文件名(和路径)

            一旦我删除了更改 aar 名称的 gradle 代码,我的解决方案之路就变得更简单了。

            【讨论】:

              【解决方案10】:

              如果您使用的是复杂的设置,其中有一个模块并且有一些子模块等。那么您需要将构建变体添加到模块(比如顶级模块)而不是子模块和可能正在使用的其他模块你的模块。您不能直接添加到子模块,否则 android studio 会混淆选择哪个。

              举个例子,假设子模块有 snapDebug 作为构建变体,现在它应该与 Top Module 的 snapDebug 或使用它的模块的 snapDebug 结合在一起。如果它们中的任何一个都没有 snapDebug,android studio 会混淆选择哪个与另一个。因此,最新版本的 android studio 中出现错误。

              希望它有助于理解问题和解决方案。

              【讨论】:

                【解决方案11】:

                在这些情况下,当您的主项目使用具有风味维度的模块或库模块 (AAR) 时,您的应用不知道该使用哪一个。您应该在应用的 build.gradle 文件的 defaultConfig 块中使用 missingDimensionStrategy 来指定默认风格。例如:

                missingDimensionStrategy 'dimension', 'flavor1', 'flavor2'
                

                请查看this链接了解更多详情。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2015-11-21
                  • 2018-05-05
                  • 2023-03-11
                  • 2012-10-11
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-10-09
                  相关资源
                  最近更新 更多