建立在摇篮变种图书馆项目的Andr​​oid [英] Build variants in Gradle for a Library Project in Android

查看:143
本文介绍了建立在摇篮变种图书馆项目的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置与摇篮里面包含一些外部库的项目。随着摇篮我可以设置不同的环境配置(用类中的配置文件)使用生成变异,所以我可以根据这个变量执行code中的主要应用。

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。在调试: 包配置;

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;
}

code的释放:

Code in release:

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?

由于在从我读<一个瞬间href="http://tools.android.com/tech-docs/new-build-system/user-guide">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.

任何想法?

谢谢!

推荐答案

不知道什么是错的配置,但你需要,我会做不同的看法。

Not sure what's wrong with your configuration but about your need, I would do it differently.

在摇篮构建文件,你可以使用 buildConfig 关键词到指定的行添加到 BuildConfig.java 生成的类。

In the gradle build file you can use the buildConfig keyword to add a specific line to the BuildConfig.java generated class.

所以,你可以添加做这样的事情,在你的 build.gradle

So you could add do something like that in your build.gradle :

    release {
        buildConfig "public static final String USE_REPORTING = true;"
    }
    debug {

        buildConfig "public static final String USE_REPORTING = false;"
    }

所以,只有一个 PlayerEnvConfig

public static final boolean USE_REPORTING = BuildConfig.USE_REPORTING;

甚至没有更多的 PlayerEnvConfig 和直接使用的 BuildConfig 类。

编辑自更新,语法发生了变化:

EDIT Since an update, the syntax has changed :

buildConfigField "<type>", "<name>", "<value>"

这篇关于建立在摇篮变种图书馆项目的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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