带有自定义源集的 Android Gradle 风格 - gradle 文件应该是什么样的? [英] Gradle flavors for android with custom source sets - what should the gradle files look like?

查看:28
本文介绍了带有自定义源集的 Android Gradle 风格 - gradle 文件应该是什么样的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的 eclipse 项目,我已移入 android studio 并设置为使用风味.在我开始尝试在我的风格之间使用不同的 java 文件之前,它似乎工作正常.

I've got an old eclipse project I've moved into android studio and setup to use flavors. It seemed to be working fine till I started trying to use different java files between my flavors.

我的项目设置是这样的:

My project setup is this:

ProjectRoot
+- acitonbarsherlock
+- facebook
+- myLib1
+- myProject
   +- src
      +- commonFiles
         +- flavor1
         +- flavor2
   +- res
      +- flavor1
      +- flavor2

myProject gradle 文件 android 闭包的内部结构如下所示:

The innards of the myProject gradle file android closure looks like this:

android {
compileSdkVersion 17
buildToolsVersion "18.0.1"

signingConfigs {
     ...
}

productFlavors {
    flavor2 {
    }
    flavor1 {
    }
}

sourceSets{
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src/commonFiles/java']
        resources.srcDirs = ['src/commonFiles/java']
        aidl.srcDirs = ['src/commonFiles/java']
        renderscript.srcDirs = ['src/commonFiles/java']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }

    flavor2 {
        manifest.srcFile 'AndroidManifest-flavor2.xml'
        res.srcDirs = ['res-flavor2', 'res']
        java.srcDirs = ['src/flavor2/java','src/commonFiles/java']
        resources.srcDirs = ['src/flavor2/java','src/commonFiles/java']
        aidl.srcDirs = ['src/flavor2/java','src/commonFiles/java']
        renderscript.srcDirs = ['src/flavor2/java','src/commonFiles/java']
    }

    flavor1 {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src/flavor1/java','src/commonFiles/java']
        resources.srcDirs = ['src/flavor1/java','src/commonFiles/java']
        aidl.srcDirs = ['src/flavor1/java','src/commonFiles/java']
        renderscript.srcDirs = ['src/flavor1/java','src/commonFiles/java']
        res.srcDirs = ['res-flavor1','res']
        assets.srcDirs = ['assets']
    }

    // Move the tests to tests/java, tests/res, etc...
    instrumentTest.setRoot('tests')

    // Move the build types to build-types/<type>
    // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
    // This moves them out of them default location under src/<type>/... which would
    // conflict with src/ being used by the main source set.
    // Adding new build types or product flavors should be accompanied
    // by a similar customization.
    debug.setRoot('build-types/debug')
    release.setRoot('build-types/release')

}

buildTypes {
    release {
        signingConfig signingConfigs.release
    }
}

}

使用我这样的设置,gradle 抱怨无法找到我试图从风味 1 或风味 2 中的 commonFiles 继承的类.

With my setup like this gradle complains about not being able to find classes I'm trying to inherit from commonFiles in flavor1 or flavor2.

从我在这里看到的各种其他主题中,我看到其他人甚至没有定义源集,而且我觉得我在这些主题中所做的可能太多了.

From the various other topics I've looked at on here I see others not even defining source sets, and I feel like what I'm doing in them is perhaps too much.

有没有人以前尝试过这个并且知道应该如何正确配置它?

Has anyone experimented with this before and know how this should properly be configured?

推荐答案

我认为最好不要定义自定义 sourceSets,而是使用默认的 gradle 配置.我曾经做过自定义源集,直到我意识到这些约定非常方便.

I think you'd be better off not defining custom sourceSets but using the default gradle configuration. I used to do custom sourcesets until I realized the conventions are, well, convenient.

你会想要这样的:

+ src
    + main // this is your common code
        + java 
        + res
    + flavor1
        + java
        + res
    + flavor2
        + java
        + res

然后你可以继续从你的 build.gradle

Then you can just go ahead and remove the sourcesets closure from your build.gradle

注意:对于 gradle 配置,资源被合并或覆盖,而所有 java 代码都放在相同的类路径上.例如,每种风格的 AndroidManifest.xml 文件只需要与 main 的清单文件有所不同.拥有一个资产,例如,ic_launcher 会覆盖 main 中的 ic_launcher,如果这样的文件存在的话.但是,在 main 和风味中都有一个文件 HomeActivity.java 是不可能的,并且会产生重复文件错误.

NOTE: For the gradle configuration, resources are merged or overridden whereas all java code is put on the same class-path. For example, the AndroidManifest.xml files for each flavor need only have the differences from main's manifest file. Having an asset, for example, ic_launcher in a flavor overrides the ic_launcher from main if such file exists. However, having a file HomeActivity.java in both main and the flavor is not possible and will give a duplicate file error.

这篇关于带有自定义源集的 Android Gradle 风格 - gradle 文件应该是什么样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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