javaandroidsqlitesqlcipher-android

different Database library with same methods in different flavours Best Practice?


I have two different falvours in my Androdi app that should use different SQL implementations. One use:

android.database.sqlite.SQLiteDatabase

and second:

net.sqlcipher.database.SQLiteDatabase

I have methods like that:

getAll(SQLiteDatabase conn)

How should I solve this situation to avoid copy&paste? What is the best pratcice? I have few ideas: first one (the worst with a lot of copypaste) is to provide different methods :

 getAll(android.database.sqlite.SQLiteDatabase conn)
 getAll(net.sqlcipher.database.SQLiteDatabase conn)

second is to wrap this class with some other in every flavour importing proper database(aggregation, composition as SQLiteDatabase is final):

import android.database.sqlite.SQLiteDatabase;
public class SQLliteDatabaseFlavoured   {
     SQLiteDatabase sqLiteDatabase;
}

With usage:

getAll(SQLliteDatabaseFlavoured.SQLiteDatabase conn)

Solution


ok I finally found solution based on @Jim's answer. Big Thanks!

It's not the cleanest but it wokrs. I create two tasks to copy files

task copyNoEncryption << { 
//copy to temp folder
copy {
    from("src/com/sql")
    into("src/temp/sql")
}
//copy back to correct folder and replace string's
copy {
    from("src/temp/sql")
    into("src/com/sql")

    filter {
 //you have to remember that first argument is REGEX and second is normal String
        String line ->
            line.replaceAll("before REGEX",
                    "after STRING")
    }
}
//delete temp folder
delete("src/temp")
}

and second task working analogously

task copyEncryption << {
//same body but reverse string swap 
//REMEMBER in replaceAll 1st arg is REGEX and second is String
}

now I'm adding execution this task before respectively flavor

android.buildTypes.all{ theBuildType ->
tasks.whenTaskAdded{ theTask ->
    if(theTask.name == "generateFlavorWithoutEncryptionr${theBuildType.name.capitalize()}Sources"){
        theTask.dependsOn "copyNoEncryption"
    }
    else if(theTask.name == "generateFlavourWithEncryption${theBuildType.name.capitalize()}Sources"){
        theTask.dependsOn "copyEncryption"
    }
}
}

Now every time when I'm building flavor I have correct libraries. Hope that someday this will help someone.