为自定义源设置的Android的Gradle口味 – Gradle文件应该是什么样的?
我已经有一个旧的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抱怨无法find类,我试图从flavor1或flavor2 commonFilesinheritance。
从我在这里看到的其他各种主题,我看到其他人甚至不定义源集,而且我觉得我在其中做的事情可能太多了。
有没有人试过这个,知道如何正确configuration?
我认为你最好不要定义自定义的sourceSets
而是使用默认的gradleconfiguration。 我曾经做过自定义的源代码集,直到我意识到这些约定是很方便的。
你会想要这样的东西:
+ src + main // this is your common code + java + res + flavor1 + java + res + flavor2 + java + res
然后你可以继续,从你的build.gradle
删除源代码封闭
注:对于gradle
configuration,资源被合并或覆盖,而所有java
代码放在同一个类path上。 例如,每个flavor的AndroidManifest.xml
文件只需要和main
的manifest文件有所不同。 有一个资产,例如, ic_launcher
的味道覆盖ic_launcher
从主如果这样的文件存在。 但是, main
和味道有一个文件HomeActivity.java
是不可能的,并会给重复的文件错误。
如果您愿意,欢迎使用自定义sourceSets
和buildTypes
(或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'] flavor1.res.srcDirs = ['res-flavor1'] ... flavor2.java.srcDirs = ['src-flavor2'] flavor2.res.srcDirs = ['res-flavor2'] ... }
这是我的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