我仅在应用程序的一种风格中使用 greenDAO,并且我想避免添加对不使用它的风格的依赖项,以便应用程序不会臃肿。
我遇到的问题是
apply plugin: 'org.greenrobot.greendao'
适用于两种风格,这会导致构建脚本对于不使用 greenDAO 的风格失败。
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
android {
...
productFlavors {
lite {
...
}
pro {
...
}
}
}
greendao {
schemaVersion 1
}
dependencies {
proCompile 'org.greenrobot:greendao:3.2.0'
}
我该如何解决这个问题,有什么想法吗?
编辑:
更具体地说,看起来 greenDAO 构建了所有标有 @Entity 的类,即使它们具有不同的风格,并将它们放入文件夹中
\build\generated\source\greendao\com\test\app\database
这会导致构建错误,因为找不到类(这当然不可能,因为它们以不同的风格定义)。
目前greenDao不支持此功能。与此相关的错误已在 GitHub 上的问题跟踪器上公开,我们希望它能尽快得到解决。
不要将此代码放在任何闭包中。将它们放在 /app/build.gradle 的末尾
不要将此代码放在任何闭包中。将它们放在 /app/build.gradle 的末尾
不要将此代码放在任何闭包中。将它们放在 /app/build.gradle 的末尾
核心功能:
def getCurrentFlavor(boolean firstUpper) {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern
if( tskReqStr.contains( "assemble" ) ) // to run ./gradlew assembleRelease to build APK
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else if( tskReqStr.contains( "bundle" ) ) // to run ./gradlew bundleRelease to build .aab
pattern = Pattern.compile("bundle(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
Matcher matcher = pattern.matcher( tskReqStr )
if( matcher.find() ) {
String flavor = matcher.group(1)
if (firstUpper) {
return flavor
} else {
return flavor.substring(0, 1).toLowerCase() + flavor.substring(1)
}
} else {
println "NO MATCH FOUND"
return ""
}
}
制作不同的 greendaoSchema 版本:
String currentFlavorDir = getCurrentFlavor(true)
if ("" != currentFlavorDir) {
apply from: "/${currentFlavorDir}/app/constant.gradle"
println "${currentFlavorDir} apply greenDaoSchemaVersion ${greendaoSchemaVersion}"
greendao {
schemaVersion greendaoSchemaVersion
}
}
来自风味目录的constant.gradle:
ext {
greendaoSchemaVersion = youFlavorVersionNumber
}
要解决在文件中生成两个或多个同名字段的问题,请输入以下代码:
核心处理器:
class Processor {
void processEntityAnnotation(boolean enable, File file) {
StringBuilder newFile = new StringBuilder()
try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("//@Entity")) {
if (enable) {
// replace with your own line
line = "@Entity(indexes = {@Index(value = \"createTime DESC\")})"
println ("processEntityAnnotation enable path: ${file.absolutePath}")
}
} else if (line.startsWith("@Entity")) {
if (!enable) {
// replace with your own line
line = "//@Entity(indexes = {@Index(value = \"createTime DESC\")})"
println ("processEntityAnnotation disable path: ${file.absolutePath}")
}
}
newFile.append(line)
newFile.append('\n')
}
reader.close();
}
try(FileWriter writer = new FileWriter(file)) {
writer.write(newFile.toString())
}
}
}
任务名为“greendaoBefore”,添加评论:
tasks.register('greendaoBefore') {
// Init this objects outside of "doLast" in order to run with "org.gradle.unsafe.configuration-cache=true"
String flavor = getCurrentFlavor(false)
Processor processer = new Processor()
File file0 = file("../flavor0/main/java/Entity.java")
File file1 = file("../flavor1/main/java/Entity.java")
File file2 = file("../flavor2/main/java/Entity.java")
doLast {
println "greendaoBefore"
println "greendaoBefore ${flavor}"
if (flavor == "flavor1") {
println("processEntityAnnotation path1")
processer.processEntityAnnotation(false, file0)
processer.processEntityAnnotation(true, file1)
processer.processEntityAnnotation(false, file2)
} else if (flavor == "flavor2") {
println("processEntityAnnotation path2")
processer.processEntityAnnotation(false, file0)
processer.processEntityAnnotation(false, file1)
processer.processEntityAnnotation(true, file2)
} else {
println("processEntityAnnotation path0")
processer.processEntityAnnotation(true, file0)
processer.processEntityAnnotation(false, file1)
processer.processEntityAnnotation(false, file2)
}
}
}
名为“greendaoAfter”的任务,如果需要取消 git 的评论:
tasks.register('greendaoAfter') {
// Init this objects outside of "doLast" in order to run with "org.gradle.unsafe.configuration-cache=true"
String flavor = getCurrentFlavor(false)
Processor processer = new Processor()
File file0 = file("../flavor0/main/java/Entity.java")
File file1 = file("../flavor1/main/java/Entity.java")
File file2 = file("../flavor2/main/java/Entity.java")
doLast {
println "greendaoAfter"
println "greendaoAfter ${flavor}"
if (flavor == "flavor1") {
processer.processEntityAnnotation(true, file0)
processer.processEntityAnnotation(true, file2)
} else if (flavor == "flavor2") {
processer.processEntityAnnotation(true, file0)
processer.processEntityAnnotation(true, file1)
} else {
processer.processEntityAnnotation(true, file1)
processer.processEntityAnnotation(true, file2)
}
}
}
注册任务:
tasks.configureEach { task ->
if (task.name == "greendaoPrepare") {
task.dependsOn('greendaoBefore')
}
if (task.name == "greendaoAfter") {
task.dependsOn('greendao')
}
//If you haven't use kotlin, you can replace with annotationProcessor task name
if (task.name.startsWith("kapt")) {
task.dependsOn('greendaoAfter')
}
}