Android Gradle:为库的特定风格添加依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24389091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradle: add dependency for a specific flavour of the library
提问by amarkovits
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 storeflavour for the application I want to use the storeflavour from the library. Also when I build the devflavour for the application I want to use the devflavour from the library. I tried setting the same product flavours for both library and application but it does not work.
我有一个图书馆项目和一个应用程序。我想为库和应用程序提供 2 种产品风格(store、dev)。当我为应用程序构建商店风格时,我想使用库中的商店风格。此外,当我为应用程序构建开发风格时,我想使用库中的开发风格。我尝试为库和应用程序设置相同的产品风格,但它不起作用。
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
我有这个文件:src/main/res/values/strings.xml 和 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_nameis defined in string.xmlin the library (in both mainand storedirectory)
在这种情况下,我收到此错误:错误:(12, 23) 找不到与给定名称匹配的资源(在“文本”处,值为“@string/app_name”)。在APP_NAME中定义string.xml在库(在主及店目录)
If I remove the productFlavors from the library it builds but always use the values.xmlfrom the maindirectory
如果我删除从它建立图书馆的productFlavors但始终使用values.xml从主目录
回答by galex
In your library you need to tell gradle to build every time every variant:
在您的库中,您需要告诉 gradle 每次构建每个变体:
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.
可在Library Publication下的官方文档中找到。
Edit:
编辑:
Since version 0.14.3 (2014/11/18), you can now have Flavor-buildType-Compile directive as well:
从0.14.3 (2014/11/18)版本开始,您现在也可以使用 Flavor-buildType-Compile 指令:
In your build.gradle before the android {} scope add the following:
在 android {} 范围之前的 build.gradle 添加以下内容:
configurations {
devDebugCompile
devReleaseCompile
storeDebugCompile
storeReleaseCompile
}
Then you can declare and use different versions of your library per Flavor-BuildType:
然后,您可以为每个 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.
自Android Gradle Plugin 3.0.0以来,模块之间的依赖管理发生了变化。它会自动尝试匹配您的应用程序与其依赖的库/模块之间的风格。
回答by amarkovits
To define specific dependency for each flavor you can use "nameOfTheFlavorCompile" instead of "compile"
要为每个风味定义特定的依赖项,您可以使用“nameOfTheFlavorCompile”而不是“compile”
dependencies {
storeCompile project(':lib')
}
回答by Hessan Feghhi
I had a similar issue a while ago, what happens is that the compiler ignores product flavors for the library project. Moreover, even when you add more build types, it keeps looking for and compiling release/debug sources.
不久前我遇到了类似的问题,发生的情况是编译器忽略了库项目的产品风格。此外,即使您添加更多构建类型,它也会不断寻找和编译发布/调试源。
The reason is simple:
原因很简单:
- Product flavors are not supported for the library plugin.
- The only build types supported seem to be debug and release.
- 库插件不支持产品风格。
- 唯一支持的构建类型似乎是调试和发布。
The compiler will neverlook for anything you put in the store directory of your library project. So, you need to find a way around it.
编译器永远不会查找您放在库项目的存储目录中的任何内容。因此,您需要找到解决方法。
One way around it would be to include both implementations with a level of abstraction in your library (through interfaces, multiple constructors, etc), and then let your app project decide which part of the library to run. If you use proguard, it will automatically get rid of the unneeded code, if it is decoupled well enough (and possibly so even if it is not).
一种解决方法是在您的库中包含具有抽象级别的两个实现(通过接口、多个构造函数等),然后让您的应用程序项目决定运行库的哪个部分。如果你使用 proguard,它会自动摆脱不需要的代码,如果它解耦得足够好(即使不是这样,也可能如此)。