摇篮:如何在Android库使用BuildConfig与获取应用程序设置的标志 [英] Gradle : how to use BuildConfig in an android-library with a flag that gets set in an app

查看:1080
本文介绍了摇篮:如何在Android库使用BuildConfig与获取应用程序设置的标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的(1.10的gradle的gradle和0.8的插件)为基础的Andr​​oid项目由一个大的Andr​​oid库,3种不同的Andr​​oid的应用程序依赖的

My (gradle 1.10 and gradle plugin 0.8)-based android project consists of a big android-library that is a dependency for 3 different android-apps

在我的图书馆,我很想能够使用这样的结构

In my library, I would love to be able to use a structure like this

if (BuildConfig.SOME_FLAG) {
    callToBigLibraries()
}

如proguard的将能够减少产生的apk的大小,是根据SOME_FLAG的最终值

as proguard would be able to reduce the size of the produced apk, based on the final value of SOME_FLAG

但我找不出如何与作为的gradle做到这一点:

But I can't figure how to do it with gradle as :

* the BuildConfig produced by the library doesn't have the same package name than the app
* I have to import the BuildConfig with the library package in the library
* The apk of an apps includes the BuildConfig with the package of the app but not the one with the package of the library.

我尝试没有成功与BuildTypes和东西玩像

I tried without success to play with BuildTypes and stuff like

release {
    // packageNameSuffix "library"
    buildConfigField "boolean", "SOME_FLAG", "true"
}
debug {
    //packageNameSuffix "library"
    buildConfigField "boolean", "SOME_FLAG", "true"
}

什么是建立对于我的图书馆共享BuildConfig和我的应用程序,其标志将在生成的应用程序被重写的正确方法?

What is the right way to builds a shared BuildConfig for my library and my apps whose flags will be overriden at build in the apps ?

提前许多感谢任何见解。

Many thanks in advance for any insight

推荐答案

您不能做你想做的,因为 BuildConfig.SOME_FLAG 是不会得到传播妥善到您的图书馆;构建类型本身不会传播到图书馆 - 他们总是建成释放。这是错误的https://$c$c.google.com/p/android/issues/detail?id=52962

You can't do what you want, because BuildConfig.SOME_FLAG isn't going to get propagated properly to your library; build types themselves aren't propagated to libraries -- they're always built as RELEASE. This is bug https://code.google.com/p/android/issues/detail?id=52962

要解决它:如果你有超过所有的库模块的控制,可以确保所有code。通过感动callToBigLibraries()是类和包,你可以使用ProGuard干净切割掉,然后使用反射,这样,如果存在的话,你可以访问他们,如果他们不降低正常。你基本上做同样的事情,但你让在运行时,而不是编译时检查,这是一个有点困难。

To work around it: if you have control over all of the library modules, you could make sure that all the code touched by callToBigLibraries() is in classes and packages that you can cleave off cleanly with ProGuard, then use reflection so that you can access them if they exist and degrade gracefully if they don't. You're essentially doing the same thing, but you're making the check at runtime instead of compile time, and it's a little harder.

让我知道如果你遇到了麻烦搞清楚如何做到这一点;如果你需要它,我可以提供一个样本。

Let me know if you're having trouble figuring out how to do this; I could provide a sample if you need it.

这篇关于摇篮:如何在Android库使用BuildConfig与获取应用程序设置的标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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