包含带有风味android的库

时间:2018-04-13 11:11:00

标签: android gradle

我之前的应用程序gradle文件:

compile project(path: ':zblelib')

但是当我在lib中添加口味时,我的导入功能

我的口味:

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 {
    ? ?
}

2 个答案:

答案 0 :(得分:3)

应用程序遇到的问题是它不知道要使用哪种库的风格。

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

我们必须在你的app 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

<强> PS

我使用implementation代替compile,因为弃用了编译(see here

答案 1 :(得分:1)

您应该在应用的missingDimensionStrategy 文件中使用build.gradle,它与您库中缺少的风味相匹配。检查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']
    }
}