57

我有一个旧的 eclipse 项目,我已经搬到了 android studio 并设置为使用风味。在我开始尝试在我的风格之间使用不同的 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 继承的类。

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

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

4

6 回答 6

67

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

你会想要这样的东西:

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

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

注意:对于gradle配置,资源被合并或覆盖,而所有java代码都放在同一个类路径上。例如,AndroidManifest.xml每个风味的文件只需要与main清单文件有差异。例如,如果存在此类文件,则ic_launcher在风味中拥有资产会覆盖from main。ic_launcher但是,HomeActivity.java在这两种情况main下都有一个文件是不可能的,并且会出现重复文件错误。

于 2013-12-03T08:01:16.590 回答
44

如果您愿意,欢迎您使用自定义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']
       ...
    }

}
于 2014-02-10T13:46:51.433 回答
7

这是我的 Gradle 的样子:

   productFlavors {
    // Still keeping the productFlavors closure in case we decide to add flavors later
    normal {
        applicationId 'com.akshat'
    }
    qa {
        applicationId 'com.akshat.qa'
    }
}

 sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        aidl.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
        jni.srcDirs = [] // Set empty since we use our own ndkBuild task
        jniLibs.srcDirs = ['libs']
    }

    normal {
        java.srcDirs = ['src_normal']
    }
    qa{
        java.srcDirs = ['src_qa']
    }

这是我的目录结构:

MyApplication
    - res
    - libs
    - jni 
    - src
         -com/akshat/
    - src_qa
         - com/akshat/util/FlavorConst.java
    - src_normal
         - com/akshat/util/FlavorConst.java
于 2016-05-21T05:57:23.270 回答
6

这种方式对我有用。享受

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"
        }
    }
于 2018-12-31T07:16:30.727 回答
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']
        ....
    }
}
于 2017-11-23T04:15:55.907 回答
0

带有自定义源集的变体有问题。例如,如果我们只想为某些风格更改几行,我们有结构 xxx/res-paid/values/strings.xml 有两个字符串,在清单文件中使用。在这种情况下,我们有清单文件的 android 链接失败错误。因此,必须使用自定义来源的变体不起作用。也许我会向 google 和 gradle 报告有关它的问题。

于 2021-11-19T01:04:18.497 回答