摇篮口味为Android定制源集 - 我应该在摇篮文件是什么样子?摇篮、是什么样子、口味、文件

2023-09-04 06:16:43 作者:最美的风景在路上

我有一个旧的Eclipse项目,我搬进了机器人工作室,并设置为使用香精。它似乎是罚款工作到我开始尝试用我的口味之间不同的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 ---- RES-flavor2

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

在myProject的摇篮文件的Andr​​oid封闭的内部结构是这样的:

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

}

使用我的设置是这样摇篮抱怨不能够找到类,我试图从commonFiles继承flavor1或flavor2。

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 ,但使用默认的摇篮配置。我以前做定制sourcesets直到我意识到的惯例是,好,方便。

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

然后,你可以继续前进,从取出sourcesets关闭你的 build.gradle

注意:作为摇篮配置,资源合并,而所有 Java的覆盖 code被放在同一类路径上。例如,的Andr​​oidManifest.xml 文件每味只需要具有从的差异主要的清单文件。拥有的资产,例如, ic_launcher 的香味覆盖 ic_launcher 从主如果这样的文件存在。然而,有 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.