Reputation: 508
I am trying to copy a db file in assets based on build flavours, for this, I have created a task in build.gradle (app level)
flavorDimensions("default")
productFlavors {
dev {
applicationIdSuffix ".dev"
buildConfigField("String", "DB_FILE_DIR", '"stage/"')
delete"$rootProject.projectDir/app/src/main/assets/app_db.db"
}
stage {
applicationIdSuffix ".stage"
buildConfigField("String", "DB_FILE_DIR", '"stage/"')
delete "$rootProject.projectDir/app/src/main/assets/app_db.db"
}
production {
applicationIdSuffix ".production"
delete"$rootProject.projectDir/app/src/main/assets/app_db.db"
}
}
variantFilter { variant ->
if (variant.buildType.name.equals('release') || variant.buildType.name.equals('debug')) {
variant.setIgnore(true)
}
}
Task to copy the database files.
task copyProductionDB(type: Copy) {
from file("$rootProject.projectDir/production/app_db.db")
into "$rootProject.projectDir/app/src/main/assets/"
println("Production DB copied")
}
task copyStageDB(type: Copy) {
from file("$rootProject.projectDir/stage/app_db.db")
into "$rootProject.projectDir/app/src/main/assets/"
println("Stage DB copied")
}
And my problem is on switching from one flavour to another,I have to replace the db file to assets folder. How can I achieve this. Please help me. Thanks in advance.
Upvotes: 1
Views: 503
Reputation: 508
I solved the my problem with the following code.
tasks.whenTaskAdded { task ->
if (task.name ==~ /preDevMyAppBuild.*/) {
task.doFirst() {
copyDB("stage")
}
} else if (task.name ==~ /preStageMyAppBuild.*/) {
task.doFirst() {
copyDB("stage")
}
} else if (task.name ==~ /preProductionMyAppBuild.*/) {
task.doFirst() {
copyDB("production")
}
}
}
def copyDB(copyType) {
copy {
from file("$rootProject.projectDir/"+copyType+"/app_db.db")
into "$rootProject.projectDir/app/src/main/assets/"
println(copyType+" DB copied")
}
}
Upvotes: 3
Reputation: 1380
You can check application variant and get its direcotry:
android.applicationVariants.all { variant ->
// get directory
variant.dirName // it returns debug/dev or relese/dev , debug/stage or relese/stage
}
Upvotes: 3