Gradle:为库的特定风格添加依赖项 [英] Gradle: add dependency for a specific flavour of the library

查看:313
本文介绍了Gradle:为库的特定风格添加依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图书馆项目和一个应用程序。我希望为库和应用程序提供两种产品口味( store dev )。当我为应用程序构建 store 味道时,我想使用库中的 store 味道。另外,当我为应用程序构建 dev 味道时,我想使用库中的 dev 味道。我尝试为库和应用程序设置相同的产品口味,但不起作用。



这是我的配置:
$ b

Library

  apply plugin:'android-library'

android {
compileSdkVersion 19
buildToolsVersion19.1.0

defaultConfig {
applicationIdro.amarkovits.graddletest.lib
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName1.0

buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
productFlavors {
商店{

}
dev {

}
}



依赖关系{
编译fileTree(dir:'libs',include:['* .jar'])
}

我有这样的文件:
src / main / res / values / strings.xml

src / store / res / values / strings.xml



< pre $ apply插件:'android'

android {
compileSdkVersion 19
buildToolsVersion '19 .1.0'
defaultConfig {
applicationId'ro.amarkovits.mymodule.app'
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName'1.0'
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
productFlavors {
商店{

}
dev {

}
}
}

相关性{
compile fileTree(dir:'libs',include:['* .jar'])
编译项目(':lib')
}

在这种情况下,我得到这个错误:Error:(12,23 )没有找到与给定名称匹配的资源(在'text'处,值为'@ string / app_name')。 app_name 在库中的 string.xml 中定义(在 main store 目录中)

>

如果我从构建的库中删除productFlavors,但始终使用 main 目录中的 values.xml

解决方案

在你的库中,你需要告诉gradle每次构建每个变体:

  android {
publishNonDefault true
}

然后在您的应用程序中,因为最近我猜,您可以这样做:

 依赖关系{
(。 ..)
devCompile项目(路径:':lib',配置:'devDebug')//或'devRelease'
storeCompile项目(路径:':lib',配置:'storeRelease')/ /或'storeDebug'
}

在官方文档中找到 Library Publication

由于版本 0.14.3(2014/11/18),您现在可以有Flavor-buildType-Compile指令:

在android {}作用域之前的build.gradle中添加以下内容:

 配置{
devDebugCompile
devReleaseCompile
storeDebugCompile
storeReleaseCompile
}

然后,您可以根据Flavor-BuildType声明和使用不同版本的库:

 依赖项{
(...)
devDebugCompile项目(路径:':lib',配置:'devDebug')
devReleaseCompile项目path:':lib',配置:'devRelease')
storeDebugCompile项目(路径:':lib',配置:'storeDebug')
storeReleaseCo mpile项目(路径:':lib',配置:'storeRelease')
}

编辑:

模块之间的依赖管理自从 Android Gradle Plugin 3.0.0 发生了变化。它会自动尝试匹配应用程序和它所依赖的库/模块之间的风格。



请参阅整个解释的文档


I have a library project and a application. I'd like to have 2 product flavours (store, dev) for both library and application. When I build the store flavour for the application I want to use the store flavour from the library. Also when I build the dev flavour for the application I want to use the dev flavour from the library. I tried setting the same product flavours for both library and application but it does not work.

Here is my configuration:

Library

apply plugin: 'android-library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        applicationId "ro.amarkovits.graddletest.lib"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors{
        store{

        }
        dev{

        }
    }

}

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

and I have this files: src/main/res/values/strings.xml and src/store/res/values/strings.xml

Application

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'
    defaultConfig {
        applicationId 'ro.amarkovits.mymodule.app'
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName '1.0'
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors{
        store{

        }
        dev{

        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':lib')
}

In this situation I get this error: Error:(12, 23) No resource found that matches the given name (at 'text' with value '@string/app_name'). The app_name is defined in string.xml in the library (in both main and store directory)

If I remove the productFlavors from the library it builds but always use the values.xml from the main directory

解决方案

In your library you need to tell gradle to build every time every variant:

android {
    publishNonDefault true
}

Then in your application, since recently I guess, you can do this:

dependencies {
    (...)
    devCompile project(path: ':lib', configuration: 'devDebug') // or 'devRelease'
    storeCompile project(path: ':lib', configuration: 'storeRelease') // or 'storeDebug'
}

Found in the official documentation under Library Publication.

Edit:

Since version 0.14.3 (2014/11/18), you can now have Flavor-buildType-Compile directive as well:

In your build.gradle before the android {} scope add the following:

configurations {
    devDebugCompile
    devReleaseCompile
    storeDebugCompile
    storeReleaseCompile
}

Then you can declare and use different versions of your library per Flavor-BuildType:

dependencies {
    (...)
    devDebugCompile project(path: ':lib', configuration: 'devDebug')
    devReleaseCompile project(path: ':lib', configuration: 'devRelease')
    storeDebugCompile project(path: ':lib', configuration: 'storeDebug')
    storeReleaseCompile project(path: ':lib', configuration: 'storeRelease') 
}

Edit:

Dependency management between modules has changed since Android Gradle Plugin 3.0.0. It automatically tries to matches flavours between your app and the libraries/modules it depends on.

See the documentation for the whole explanation!

这篇关于Gradle:为库的特定风格添加依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆