首页 > 解决方案 > Gradle:项目的多个变体 :myLib 匹配消费者属性

问题描述

我编写的一个库myLib使用RemoteMessage来自Firebase,应用程序本身也使用Firebase.

使用 gradle 4.7。在 4.4.1 中也面临这个问题。

如何解决?

项目.gradle

buildscript {
    
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath 'com.google.gms:google-services:3.3.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        mavenLocal()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用程序.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.test.app"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    dexOptions {
        preDexLibraries = false
        jumboMode = false
        javaMaxHeapSize "2048M"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    productFlavors {
    }
}

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

    implementation 'com.android.support:appcompat-v7:27.1.1'

    implementation 'com.google.android.gms:play-services-gcm:12.0.1'
    implementation 'com.google.android.gms:play-services:12.0.1'
    implementation 'com.google.firebase:firebase-core:12.0.1'
    implementation 'com.google.firebase:firebase-messaging:12.0.1'
    implementation 'com.google.firebase:firebase-crash:12.0.1'

    implementation project(":myLib")
}

apply plugin: 'com.google.gms.google-services'

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

}

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

    implementation 'com.android.support:appcompat-v7:27.1.1'

    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
    implementation 'com.j256.ormlite:ormlite-core:5.0'
    implementation 'com.j256.ormlite:ormlite-android:5.0'

    implementation 'com.google.firebase:firebase-messaging:12.0.1'
}

testApp\myLib> gradle clean assembleDebug和的错误输出testApp\app> gradle clean assembleDebug

project :myLib 的多个变体与消费者属性匹配:

  • 配置 ':myLib:debugApiElements' 变体 android-aidl:
    • 找到 artifactType 'android-aidl' 但不是必需的。
    • 需要 com.android.build.api.attributes.BuildTypeAttr 'debug' 并找到兼容值 'debug'。
    • 找到 com.android.build.api.attributes.VariantAttr 'debug' 但不是必需的。
    • 需要 com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' 并找到兼容值 'Aar'。
    • 需要 org.gradle.usage 'java-api' 并找到兼容值 'java-api'。
  • 配置 ':myLib:debugApiElements' 变体 android-classes:
    • 找到 artifactType 'android-classes' 但不是必需的。
    • 需要 com.android.build.api.attributes.BuildTypeAttr 'debug' 并找到兼容值 'debug'。
    • 找到 com.android.build.api.attributes.VariantAttr 'debug' 但不是必需的。
    • 需要 com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' 并找到兼容值 'Aar'。
    • 需要 org.gradle.usage 'java-api' 并找到兼容值 'java-api'。
  • 配置 ':myLib:debugApiElements' 变体 android-manifest:
    • 找到 artifactType 'android-manifest' 但不是必需的。
    • 需要 com.android.build.api.attributes.BuildTypeAttr 'debug' 并找到兼容值 'debug'。
    • 找到 com.android.build.api.attributes.VariantAttr 'debug' 但不是必需的。
    • 需要 com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' 并找到兼容值 'Aar'。
    • 需要 org.gradle.usage 'java-api' 并找到兼容值 'java-api'。
  • 配置 ':myLib:debugApiElements' 变体 android-renderscript:
    • 找到 artifactType 'android-renderscript' 但不是必需的。
    • 需要 com.android.build.api.attributes.BuildTypeAttr 'debug' 并找到兼容值 'debug'。
    • 找到 com.android.build.api.attributes.VariantAttr 'debug' 但不是必需的。
    • 需要 com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' 并找到兼容值 'Aar'。
    • 需要 org.gradle.usage 'java-api' 并找到兼容值 'java-api'。
  • 配置 ':myLib:debugApiElements' 变体 jar:
    • 找到 artifactType 'jar' 但不是必需的。
    • 需要 com.android.build.api.attributes.BuildTypeAttr 'debug' 并找到兼容值 'debug'。
    • 找到 com.android.build.api.attributes.VariantAttr 'debug' 但不是必需的。
    • 需要 com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' 并找到兼容值 'Aar'。
    • 需要 org.gradle.usage 'java-api' 并找到兼容值 'java-api'。

标签: androidandroid-studiogradleandroid-gradle-plugin

解决方案


gradlegoogle-services插件可与 Google Play Services 和 Firebase15+一起使用,但您将其与 version 一起使用12.0.1

解决方案是:

编辑 1在多模块应用程序中使用存在一个已知问题google-services 3.3。这意味着降级到google-servicesto3.2.1可能是您唯一的选择,直到发布修复该错误的新版本。

编辑 2:上述错误已在gradle 插件版本4.0.1中修复!google-services(并且该版本4.0.2还修复了在某些情况下发生的 NPE)

顺便说一句,您应该转到 FCM。 GCM 将在不久的将来停止工作。


推荐阅读