有条件的依赖与摇篮,这可能吗? [英] Conditional dependencies with Gradle, is it possible?

查看:321
本文介绍了有条件的依赖与摇篮,这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由不同模块的Andr​​oid项目(已经移植到Android Studio和使用摇篮)。

I have an Android project (already ported to Android Studio and using Gradle) that is made up of different modules.

该项目实际上是用来创建两个不同的应用程序,其中code是pretty大同小异,除了一些资源。

The project is actually used to create two different apps, where the code is pretty much the same, except for some resources.

因此​​这些资源已被分成两个不同的模块。

Thus the resources have been split into two different modules.

这个项目中使用Eclipse中工作,并切换资源模块的原作者列入依赖关系的基础上,他想建立的应用程式。而且他还用手工包的名字在AndroidManifest.xml更改

The original author of this project used to work in Eclipse and switch the resource modules included in the dependencies based on which app he wanted to build. And he also used to change by hand the package name in AndroidManifest.xml

我想这一切自动完成,仍然有一个code基,但与每个目标特定的模块两个构建目标。是可行的与摇篮?

I would like to automate all of this and still have a single code base, but have two build targets with specific modules for each target. Is that doable with Gradle?

更新:

为了使事情变得更加困难,我的项目都具有层次是pretty多少以下内容:

To make things even harder, my project has a hierarchy that is pretty much the following:

--+--MainProject
  +--LibData
  +--LibBase
  +--LibResA
  +--LibResB

其中:


  • MainProject取决于LibBase和LibData。

  • LibData取决于LibBase

  • LibBase要么取决于LibResA或LibResB根据最终的APK,我需要建立。

的建议,我试着用口味实施本加在MainProject的build.gradle以下内容:

As suggested, I've tried implementing this with flavors by adding in the MainProject build.gradle the following:

productFlavors {
    producta {
    }
    productb {
    }
}

然后在LibBase我添加以下到它的build.gradle:

And then in LibBase I've added the following to its build.gradle:

dependencies {
    productaCompile project(':LibResA')
    productbCompile project(':LibResB')
}

但是,当我生成项目,LibData无法找到LibBase继承的类和资源。所以我现在仍坚持这一错误。对我来说,它看起来像LibBase没有被复制到LibData的中间体。 LibData这样不能解决类LibBase,但它只是我的假设。

But then, when I build the project, LibData can't find the classes and resources inherited from LibBase. So now I'm stuck with this error. To me it looks like LibBase isn't being copied to the intermediates of LibData. That way LibData can't resolve the classes in LibBase, but it's just my assumption.

更新2:

我一直在调查这个问题,现在我已经改变了我的build.gradle文件看起来像这样:

I kept investigating this issue and now I've changed my build.gradle files to look like this:

主项目的build.gradle:

defaultPublishConfig "productaRelease"
publishNonDefault true

productFlavors {
    producta {
        applicationId "com.producta"
    }

    productb {
        applicationId "com.productb"
    }
}

dependencies {
    compile project(':LibData')
}

LibData的build.gradle(没有产品的口味,只是依赖):

dependencies {
    compile project(':LibBase')
}

LibBase的build.gradle:

defaultPublishConfig "productaRelease"
publishNonDefault true

productFlavors {
    producta {
    }

    productb {
    }
}

dependencies {
    productaCompile project(path: ':LibResA')
    productbCompile project(path: ':LibResB')
}

这种方式我没有得到任何错误,这样做通常的 gradle这个干净的构建的时候,但我可以看到,包括资源总是那些LibResA的就像defaultPublishConfig是唯一一个在任何时候使用。

This way I get no errors when doing the usual gradle clean build but I can see that the resources included are always those of LibResA just like the defaultPublishConfig is the only one used at all times.

如果我在Android的工作室(0.8.1 ATM)打开此项目的结果是,如果我尝试将其切换LibBase模块的构建变量并设置为productbRelease,正在显示以下错误:错误:模块' LibBase'有变种productbRelease选中,但模块'LibData'取决于变种productaRelease。

If I open this project in Android Studio (0.8.1 atm) the result is that if I try to switch the build variant of the LibBase module and set it to productbRelease, the following error is being shown: Error:Module 'LibBase' has variant 'productbRelease' selected, but the module ''LibData'' depends on variant 'productaRelease'.

我跑出来的想法。

推荐答案

是的,是的。基于摇篮新的Andr​​oid版本系统支持你的使用情况,其产品口味的概念。 http://tool​​s.android.com/tech-docs/new -build系统/用户指南

Yes, it is. New Android build system based on Gradle supports your use case with its concept of product flavors. http://tools.android.com/tech-docs/new-build-system/user-guide

请注意,你可能会想,当你做移植到摇篮,从Eclipse中切换到Android工作室打造。

Note that you will likely want to switch from Eclipse to Android Studio when you do migration to Gradle build.

这篇关于有条件的依赖与摇篮,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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