10

I saw this error when trying to use sqlcipher in my project. I looked it up and found several people resolved it by adding SQLiteDatabase.loadLibs(); However, it says it's expecting an @NotNull Context context and I wasn't sure what it means. Has anyone resolved this issue? This and this are two of the sources I used.

The dependency in my gradle.build is compile 'net.zetetic:android-database-sqlcipher:3.3.1-1@aar' and since I have this, it means I don't have to manually move any files to my libs directory, right?

@Override
public void onCreate(SQLiteDatabase db) {
    SQLiteDatabase.loadLibs();

    db.execSQL(CREATE_SCRIPT);
}

Apologies in advance if these are basic questions.

Community
  • 1
  • 1
Spider
  • 431
  • 7
  • 21

2 Answers2

17

However, it says it's expecting an @NotNull Context context and I wasn't sure what it means.

It means that loadLibs() needs a Context as a parameter.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am just not sure where the Context would come from, since I can't just pass a Context context as a parameter > > – Spider Oct 18 '15 at 17:57
  • @Spider: `Context` is a Java class. Subclasses of `Context` include `Activity` and `Service`. Somewhere, an `Activity` or a `Service` is what is triggering your work with the database. For example, in [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Database/ConstantsSecure-AndroidStudio), I am using an `Activity` to set up my `SQLiteOpenHelper`, and `onCreate()` on `SQLiteOpenHelper` is passed a `Context` as a parameter. – CommonsWare Oct 18 '15 at 18:06
  • Ohhh okay, is adding a pw to sqlite only available for paid version? Because right now, I can only getWritableDatabase with an empty string. Also, does it mean it's not encrypted? How can I look at the db I've created? – Spider Oct 18 '15 at 18:50
  • @Spider: "Because right now, I can only getWritableDatabase with an empty string" -- SQLCipher for Android works with a non-empty passphrase, as [my sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Database/ConstantsSecure-AndroidStudio) demonstrates. "Also, does it mean it's not encrypted?" -- yes, that's the meaning of an empty database in SQLCipher. – CommonsWare Oct 18 '15 at 19:05
  • I guess what's confusing me is it looks like you just hardcoded yours (and I see it says not recommended), but is that the pw you created for your db? Can you explain to me how that works? How can I create a pw it can start using? Or is this not possible for a pre-existing database? – Spider Oct 18 '15 at 21:12
  • @Spider: "you just hardcoded yours" -- it's a book example, so I try to keep 'em simple. "is that the pw you created for your db?" -- yes. "Can you explain to me how that works?" -- um, it's a `String`. I pass it to `getReadableDatabase()` and kin to open (and lazy-create, if needed) the database. The first time the app is run, attempting to open the database creates it, courtesy of `SQLiteOpenHelper`. "Or is this not possible for a pre-existing database?" -- I am uncertain what you mean by "pre-existing" here. – CommonsWare Oct 18 '15 at 21:19
  • @Spider: While my book is up to Version 6.8, Version 5.1 is [available](https://commonsware.com/Android/4-2-free) under a Creative Commons license, and it has a chapter on SQLCipher. It's for older versions of the sample (and SQLCipher for Android itself), but it may be useful to you. – CommonsWare Oct 18 '15 at 21:23
  • Oh awesome, the book I bought only covers sqlite db, but no encryption. I'll also look in there, thank you What I mean by pre-existing is that I had an app with a functioning sqlite db that was holding data, now I'm realising I should probably encrypt it and I'm trying to add that encryption to it. – Spider Oct 18 '15 at 21:34
  • @Spider: Ah. Yes, that's a bit of a pain. I think that version of the book has a section on doing this. You basically have to temporarily have two databases: your original unencrypted one and a new encrypted one. You connect SQLCipher to both and run a few commands to slurp the data out of the unencrypted database into the encrypted one. – CommonsWare Oct 18 '15 at 21:36
  • Actually I think I found it, it seems on page 1440 you handle this scenario, although the github link no longer works. So I'm going to try that out, thank you for all your help! – Spider Oct 18 '15 at 21:38
  • @Spider: "the github link no longer works" -- replace `master` with `v5.1` in the URL, or just [browse the repo for the `v5.1` tag](https://github.com/commonsguy/cw-omnibus/tree/v5.1). – CommonsWare Oct 18 '15 at 22:35
2

Please confirm if use

SQLiteDatabase.loadLibs(CONTEXT);

where CONTEXT is the Android context

if using Fragment use getContext method else in Activity use applicationContext

JABI
  • 21
  • 1