在 Gradle 中为 Android 中的库项目构建变体 [英] Build variants in Gradle for a Library Project in Android
问题描述
我正在尝试使用 Gradle 配置一个包含一些外部库的项目.通过 Gradle,我可以使用 Build Variants 为主应用程序设置不同的环境配置(在配置文件中包含一个类),以便我可以根据此变量执行代码.
I am trying to configure with Gradle a project which contains some external libraries. With Gradle I can setup different Environmental Configuration (with a class inside a config file) for the main application using the Build Variants so I can execute code according to this variables.
问题是我如何为图书馆项目做同样的事情?我为这个项目创建了这个库,我想为不同的场景设置不同的构建变体.
The problem is that how I can do the same for a library project? I created this library for this project and I would like to setup different Build Variants for different scenarios.
举个例子:在库中,在调试模式下运行时,打印所有日志,以便我在开发时可以看到它们.在发布模式不要.
As an example: In the Library, when running in debug mode, then print all the logs so I can see them while developing. In release mode dont.
文件结构:
src ----- > debug -> java -> config -> PlayerEnvConfig
main -> com.mypackagename -> etc...
release -> java -> config -> PlayerEnvConfig
调试中的代码:包配置;
Code in debug: package config;
/**
* Environment configuration for Release
*/
public final class PlayerEnvConfig {
public static final boolean USE_REPORTING = true;
public static final boolean USE_ANALYTICS = true;
public static final boolean USE_LOGGING = false;
public static final boolean USE_DEBUG_LOGGING = false;
public static final boolean USE_DEBUGING = false;
}
发布中的代码:
package config;
/**
* Environment configuration for Release
*/
public final class PlayerEnvConfig {
public static final boolean USE_REPORTING = true;
public static final boolean USE_ANALYTICS = true;
public static final boolean USE_LOGGING = false;
public static final boolean USE_DEBUG_LOGGING = false;
public static final boolean USE_DEBUGING = false;
}
问题是,对于主项目,我可以使用此构建类型为不同场景配置不同的应用程序,但我如何为库项目做同样的事情?
The problem is that for the main project I can use this Build types to configure differently the application for different scenarios, but how can I do the same for the Library Project?
因为目前我在 http://tools.android.com/tech-docs/new-build-system/user-guide 该库只会在测试时使用调试模式.
Because at the moment from what I read in http://tools.android.com/tech-docs/new-build-system/user-guide the library only will use the debug mode while testing.
有什么想法吗?
谢谢!
推荐答案
这是来自 谷歌代码问题,它对我有帮助:
It's a @bifmadei answer from google code issue and it helps for me:
过时:尝试在依赖项目中设置这个
Obsolete: Try setting this in the dependency project
android {
publishNonDefault true
...
}
更新:从 gradle 4.10.1 开始,publishNonDefault 默认为 true.因此,只需使用以下建议:
Update: Starting from gradle 4.10.1 publishNonDefault is true by default. So just use the recommendation below:
在使用它的项目中包含这个
Include this in the project that uses it
dependencies {
releaseCompile project(path: ':theotherproject', configuration: 'release')
debugCompile project(path: ':theotherproject', configuration: 'debug')
}
取自此处:https://code.google.com/p/android/issues/detail?id=66805
新方法:请注意,使用 implementation
指令分隔的 releaseCompile
和 debugCompile
已过时:https://stackoverflow.com/a/44364851/3379437
New approach:
Note that with implementation
instruction separated releaseCompile
and debugCompile
become obsolete: https://stackoverflow.com/a/44364851/3379437
但是这种方法仍然可以与自定义构建配置一起使用
But this approach still can be used with a custom build configuration
这篇关于在 Gradle 中为 Android 中的库项目构建变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!