具有自定义源集的Android的Gradle风格 - gradle文件应该是什么样的?

时间:2013-10-19 01:01:45

标签: android gradle android-studio source-sets

我有一个旧的eclipse项目我已经进入android studio并设置使用flavour。它似乎工作正常,直到我开始尝试在我的口味之间使用不同的java文件。

我的项目设置如下:

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

myProject gradle文件android闭包的内部看起来像这样:

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抱怨无法找到我试图从flavor1或flavor2中的commonFiles继承的类。

从我在这里看到的各种其他主题,我看到其他人甚至没有定义源集,我觉得我在他们身上做的事情可能太多了。

之前是否有人对此进行过实验并知道如何正确配置?

5 个答案:

答案 0 :(得分:54)

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

你会想要这样的东西:

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

然后,您可以继续从build.gradle

中删除源集关闭

注意:对于gradle配置,资源被合并或覆盖,而所有java代码都放在同一个类路径上。例如,每个flavor的AndroidManifest.xml文件只需要与main的清单文件有所不同。例如,如果存在此类文件,则拥有资源(例如,ic_launcher会覆盖{main}中的ic_launcher。但是,在HomeActivity.javamain中都有文件{{1}},并且不会出现重复的文件错误。

答案 1 :(得分:39)

如果您愿意,欢迎使用自定义sourceSetsflavours(或buildTypes)。

例如,您可以按如下方式在Gradle文件中设置它们: -

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']
    }
  flavor1 {
       java.srcDirs = ['src-flavor1'] 
       res.srcDirs = ['res-flavor1']
       ...
    }

}

答案 2 :(得分:5)

以下是我的Gradle的样子:

MyApplication
    - res
    - libs
    - jni 
    - src
         -com/akshat/
    - src_qa
         - com/akshat/util/FlavorConst.java
    - src_normal
         - com/akshat/util/FlavorConst.java

以下是我的目录结构:

do

答案 3 :(得分:2)

这种方式对我有用。享受

sourceSets {
        main {
            manifest.srcFile 'src/AndroidManifest.xml'
            java.srcDirs = ['src/java']
            resources.srcDirs = ['srs/others']
            res.srcDirs = ['src/res']
            assets.srcDirs = ['src/assets']
            jniLibs.srcDirs = ['jniLibs']
        }
        development{
            res.srcDirs += ['development/src/res']
        }
        standford{
            res.srcDirs += ['standford/src/res']
        }

        commercial{
            res.srcDirs += ['commercial/src/res']
        }

    }
    productFlavors {
        development{
            flavorDimensions "default"
        }
        standford{
            flavorDimensions "default"
        }
        commercial{
            flavorDimensions "default"
        }
    }

答案 4 :(得分:0)

您可以像下面的示例一样编码

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        res.srcDirs = ['res']
        java.srcDirs = ['src', '../3party/Alohalytics/src/android/java', 'ttsearch/jni/out']
        assets.srcDirs = ['assets', 'flavors/mwm-ttf-assets']
        jniLibs.srcDirs = ['libs', 'jniLibs', 'ttsearch/libs']
    }
    flavor {
        manifest.srcFile 'flavor'
        assets.srcDirs = ['flavor/assets']
        res.srcDirs = ['flavor/res']
        res.srcDirs = ['flavor/res']
        ....
    }
}