【问题标题】:Include library with flavor android包含带有风味 android 的库
【发布时间】:2018-04-13 11:10:59
【问题描述】:

我之前的应用 gradle 文件:

compile project(path: ':zblelib')

但是当我将口味添加到库中时,我的导入不起作用

我的口味:

flavorDimensions "dim"
    productFlavors {
        nocustomer {
            versionNameSuffix "-nocustomer"
            dimension = "dim"
        }
        customer001 {
            versionNameSuffix "-customer001"
            dimension = "dim"
        }
    }

如何导入我的新库并选择风味?

编辑:我的 build.gradle

图书馆

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 26
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

    }

    flavorDimensions "dim"
    productFlavors {
        nocustomer {
            versionNameSuffix "-nocustomer"
            dimension = "dim"
        }
        customer001 {
            versionNameSuffix "-customer001"
            dimension = "dim"
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:27.1.1'
    compile 'com.android.support:design:27.1.1'
    compile 'com.android.support:support-v4:27.1.1'
    compile project(':criptolib-debug')
}

应用

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultPublishConfig "nocustomerRelease"

    defaultConfig {
        applicationId "com.axesstmc.bleappphone"
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 91
        versionName "8.2"
    }

    buildTypes {
        debug {
            minifyEnabled false
            //proguardFiles 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    ? ?
}

【问题讨论】:

  • 你能把build.gradle文件放到你的应用和你的库吗?
  • @xiaomi 我会编辑我的答案

标签: android gradle


【解决方案1】:

应用程序的问题是它不知道使用哪个库的风格。

关键字matchingFallbacks 将告诉应用您要选择哪个库的风格。但此关键字必须与 Flavor 一起使用。

我们必须在您的应用 build.gradle 上添加风味(+ 维度):

android {
    ...
    //flavorDimensions is mandatory with flavors. Use the same name on your 2 files to avoid other conflicts.
    flavorDimensions "dim"
    productFlavors {
        nocustomer{
            dimension "dim"

            // App and library's flavor have the same name.
            // MatchingFallbacks can be omitted
            matchingFallbacks = ["nocustomer"]
        }
        customerNb{
            dimension "dim"

            // Here the app and library's flavor are different
            // Matching fallbacks will select the library's flavor 'customer001'
            matchingFallbacks = ["customer001"]
        }
    }
    ...
}
dependencies {
    implementation project(':zblelib')
}

这样,当你选择应用的风格nocustomer时,库的风格会自动选择nocustomer,当你选择应用的风格customerNb时,库的风格会自动选择customer001

附言

我使用implementation 而不是compile,因为不推荐使用编译 (see here)

【讨论】:

    【解决方案2】:

    您应该在应用程序的build.gradle 文件中使用missingDimensionStrategy ,它与您的库中缺少的风味相匹配。查看migration docs for Gradle 3.0.0如何实现。

    针对您的特定问题,您的库包含应用未从表中检查部分 A library dependency includes a flavor dimension that your app does not. 的产品风味。

    编辑: 在应用的 build.gradle 文件中定义风味,然后使用 matchingFallbacks 准确指定要从库中匹配的风味。

    productFlavors {
        paid {
            dimension 'tier'
            matchingFallbacks = ['customer001', 'nocustomer']
        }
        free {
            dimension 'tier'
            matchingFallbacks = ['nocustomer', 'customer001']
        }
    }
    

    【讨论】:

    • 如何指定我的应用使用哪种风格?例如,我想(我会:D)使用我的库风格“nocustomer”。
    • 首先,您应该在应用程序的build.gradle 文件中定义产品风味,然后使用matchingFallbacks 。检查此答案的编辑。
    【解决方案3】:

    我应该用另一种方式来实现这个:

    首先为您的应用添加风味和维度build.gradle,其gradle id 为com.android.application,如下所示:

    plugins {
        id 'com.android.application'
    }
    
    android {
        resourcePrefix "app_"
        flavorDimensions "env"
        productFlavors {
            dev {
                dimension "env"
            }
            pre{
                dimension "env"
            }
            prod {
                dimension "env"
            }
        }
    }
    

    然后将以下代码片段添加到您的库模块中

    plugins {
        id 'com.android.library'
    }
    
    android {
        resourcePrefix "base_"
    
        flavorDimensions "env"
        productFlavors {
            register("dev")
            register("pre")
            register("prod")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-29
      • 1970-01-01
      • 2015-09-25
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多