只是一个带有“prod”和“mock”风格的普通项目
在app / build.gradle中:
sourceSets { main.java.srcDirs += "blabla bla" test.java.srcDirs += "blabla bla" androidTest.java.srcDirs += "blabla bla" prodDebug.java.srcDirs += "blabla bla" mockDebug.java.srcDirs += "blabla bla" prod.java.srcDirs += "blabla bla" testProd.java.srcDirs += "blabla bla" androidTestProd.java.srcDirs += "blabla bla" mock.java.srcDirs += "blabla bla" testMock.java.srcDirs += "blabla bla" androidTestMock.java.srcDirs += "blabla bla" }
点击Android Studio中的“运行”:
出了点问题:
Error:org.gradle.api.GradleException: The SourceSet 'mockDebug' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?
但是,我可以做“./gradlew clean build”或“./gradlew assembleProd installProdDebug”等.
但是,如果我在命令行中执行此操作,则无法完全构建应用程序,它可以在目标设备上安装应用程序,但应用程序将像“xxxx.dex”问题一样崩溃.
解决方法
将Gradle Plugin从3.0.1更新到3.1.0后遇到同样的问题.似乎Gradle现在只找到在Android Studio中选择的变体的源集.
现在修复它的解决方法是删除sourceSets {}块并添加它:
def customSourceSets = [ myFirstBuildVariant: 'src/myFirstBuildVariant',mySecondBuildVariant: 'src/mySecondBuildVariant',] android.applicationVariants.all { if (customSourceSets.containsKey(name)) { sourceSets.find { it.name == name }.setRoot(customSourceSets.get(name)) } }