Android Hawk database github open-source project
Android Hawk database github open-source project
Hawk is a very convenient database. You only need a line of code to operate the database and can store any data type.
Hawk is a simple key-value database.
It uses:
AES Encryption
Can select SharedPreferences or SQLite
Gson parsing (the fastJson version is replaced in Simple below the article)
Provided:
Secure Data Persistence
Can store any type
Let's take a look at the example to meet the needs of the project.
Storage example:
Hawk.put("key", "something"); // Save stringHawk.put("key", true); // save booleanHawk.put("key", new Foo()); // save an objectHawk.put("key", List
); // save listHawk.put("key", List
); // save listHawk.put("key", Map
); // save mapHawk.put("key", Set
); // save setHawk.put("key", 1234); // save numbers
Example:
String value = Hawk.get(key);int value = Hawk.get(key);Foo value = Hawk.get(key);boolean value = Hawk.get(key);List
value = Hawk.get(key);List
value = Hawk.get(key);Map
value = Hawk.get(key);Set
value = Hawk.get(key);
(1) Add to project
Configure android studio in gradle
repositories { // ... maven { url "https://jitpack.io" }}dependencies { compile 'com.github.orhanobut:hawk:1.21'}
For eclipse users, please download and import the project
You can refer to the Simple
(2) initialize Hawk
You only need to initialize it once and put it in the activity you first executed or the application.
Hawk.init(this) .setEncryptionMethod(HawkBuilder.EncryptionMethod.MEDIUM) .setStorage(HawkBuilder.newSqliteStorage(this)) .setLogLevel(LogLevel.FULL) .build();
High-Security Initialization may take 36-400 ms and requires a password.
Hawk.init(this) .setEncryptionMethod(HawkBuilder.EncryptionMethod.HIGHEST) .setStorage(HawkBuilder.newSqliteStorage(this)) .setLogLevel(LogLevel.FULL) .build();
You can select SQLite or SharePreference for initialization.
.setStorage(HawkBuilder.newSqliteStorage(this))
Or
.setStorage(HawkBuilder.newSharedPrefStorage(this))
(3) store data
Hawk.put(key, T); // Returns the result as boolean
You can also use the chain function to store multiple projects. Remember to use commit () at the end ().
// Returns the result as booleanHawk.chain() .put(KEY_LIST, List
) .put(KEY_ANOTHER,"test") .commit();
(4) Data Retrieval
T result = Hawk.get(key);
Or
Set the default value when it is null.
T result = Hawk.get(key, T);
(5) Remove Data
Hawk.remove(key); // Returns the result as boolean
Or
Remove multiple
Hawk.remove(KEY_LIST, KEY_NAME); // Returns the result as boolean
(6) determine whether a key is included
boolean contains = Hawk.contains(key);
(7) set the password
Hawk.init(this) .setEncryptionMethod(HawkBuilder.EncryptionMethod.HIGHEST) .setPassword("password") .setStorage(HawkBuilder.newSqliteStorage(this)) .setLogLevel(LogLevel.FULL) .setCallback(new HawkBuilder.Callback() { @Override public void onSuccess() { } @Override public void onFail(Exception e) { } }) .build();
(8) Considerations
Hawk log output:
Hawk.init(context,PASSWORD, LogLevel.FULL); // as default it is NONE
Hawk supports the recently popular RxJava
If you use it, Please import it. If you do not need the Rx function, removing the corresponding source code will not be affected.
Obfuscation:
#Gson-keep class com.google.gson.** { *; }-keepattributes Signature
(Final) Problems Encountered
Hawk uses Json for data storage
The Gson library provided by google is used, but FastJson is already available in my project.
To control the package size, replace Gson parsing with the eclipse code written in FastJson for your reference.
Github address: https://github.com/orhanobut/hawk
Eclipse Demo
: Http://download.csdn.net/detail/aaawqqq/9367130
Eclipse modification project used in Demo,
The Demo contains four parts:
Hawk_main,
Hawk_gson-lib, // gson package as resolution method remove rx Function
Hawk_fastJson-lib, // fastJson package as Parsing Method remove rx Function
Hawk_lib // The gson package has the rx function as the parsing method, but you need to add the rx package yourself.
----------
Hawk_main is the main Demo.
Select one of the other three lib libraries.
Lib modified the parsing method into gson and fastjson versions.
Hawk_lib is the source code. You must add rx to use the rx function.
// Please wait until then // zookeeper // zookeeper without any bugs! // Zookeeper has been completed before // zookeeper has been executed before being executed ┛ ┗ ┻ ┛
Hope to be useful to everyone