Skip to content

Commit 016d5d8

Browse files
committed
增加 MMKV 拓展支持
1 parent 4d40472 commit 016d5d8

File tree

23 files changed

+359
-25
lines changed

23 files changed

+359
-25
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
使用 yaml 配置文件声明键值对的字段名、类型、默认值、是否加密、分组等,通过 Gradle 插件自动生成对象类,让键值对存储的使用变成get/set形式,让代码调用更加安全、优雅。
77

8-
- 支持对字段单独加密,避免敏感信息明文存储
8+
- 支持对字段单独加密,避免敏感信息明文存储
99
- 通过 yaml 配置存储字段信息,配置简单易懂,一目了然;
10-
- 支持基本类型、对象和列表数据,代替SharePreference及部分SQLite场景。
11-
- 支持自定义文件名,实现多用户之间的数据隔离。
10+
- 支持基本类型、对象和列表数据,代替SharePreference及部分SQLite场景;
11+
- 支持自定义文件名,实现多用户之间的数据隔离;
12+
- 支持自定义底层存储,提供 [MMKV拓展](#使用-mmkv-作为存储底层) 支持。
1213

1314
#### 推荐规范
1415

@@ -177,6 +178,32 @@ KVStorage.init(this, new KVStorage.Interceptor() {
177178

178179

179180

181+
### 使用 MMKV 作为存储底层
182+
183+
默认是使用 SharedPreferences 作为存储底层,开发者可以自定义存储底层,比如使用 [MMKV](https://github.com/Tencent/MMKV) 作为底层存储,从而获取更高的性能。
184+
185+
#### 引入 mmkv 拓展
186+
187+
```groovy
188+
implementation 'io.github.taoweiji.kvstorage:kvstorage-mmkv:+'
189+
```
190+
191+
#### 初始化
192+
193+
```java
194+
MMKV.initialize(this);
195+
196+
KVStorage.init(this, new KVStorage.Interceptor() {
197+
@Override
198+
public PreferencesAdapter createPreferencesAdapter(String fileName) {
199+
// 返回 mmkv 适配器
200+
return new MMKVPreferencesAdapter(fileName);
201+
}
202+
});
203+
```
204+
205+
这样底层存储就会改成mmkv,其它用法没有任何差异。
206+
180207

181208

182209

example/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import android.util.Base64
2+
import androidx.annotation.NonNull
3+
14
apply plugin: 'com.android.application'
25
apply plugin: 'kotlin-android'
36
apply plugin: 'kvstorage'
@@ -35,6 +38,7 @@ dependencies {
3538
implementation 'com.google.android.material:material:1.4.0'
3639
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
3740
implementation project(path: ':kvstorage')
41+
implementation project(path: ':kvstorage-mmkv')
3842
testImplementation 'junit:junit:4.+'
3943
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
4044
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
@@ -45,4 +49,4 @@ kvstorage {
4549
yamlFiles = "storage.yaml,fit_storage.yaml" // 配置文件,多个配置文件用","分割
4650
packageName = "com.taoweiji.kvstorage.example" // 生成代码的包名
4751
outputDir = "src/main/java" // 代码生成的目录
48-
}
52+
}

example/src/main/java/com/taoweiji/kvstorage/example/MyApplication.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import com.google.gson.Gson;
99
import com.google.gson.reflect.TypeToken;
1010
import com.taoweiji.kvstorage.KVStorage;
11+
import com.taoweiji.kvstorage.PreferencesAdapter;
12+
import com.taoweiji.kvstorage.mmkv.MMKVPreferencesAdapter;
13+
import com.tencent.mmkv.MMKV;
1114

1215
import java.lang.reflect.ParameterizedType;
1316
import java.lang.reflect.Type;
@@ -18,6 +21,7 @@ public class MyApplication extends Application {
1821
@Override
1922
public void onCreate() {
2023
super.onCreate();
24+
MMKV.initialize(this);
2125
KVStorage.init(this, new KVStorage.Interceptor() {
2226
@NonNull
2327
@Override
@@ -37,6 +41,12 @@ public String decryption(@NonNull String data) {
3741
public String encryption(@NonNull String data) {
3842
return Base64.encodeToString(data.getBytes(), Base64.DEFAULT);
3943
}
44+
45+
@Override
46+
public PreferencesAdapter createPreferencesAdapter(String fileName) {
47+
return new MMKVPreferencesAdapter(fileName);
48+
}
4049
});
50+
4151
}
4252
}

kvstorage-mmkv/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

kvstorage-mmkv/build.gradle

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
plugins {
2+
id 'com.android.library'
3+
}
4+
5+
android {
6+
compileSdkVersion 30
7+
8+
defaultConfig {
9+
minSdkVersion 16
10+
targetSdkVersion 30
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15+
consumerProguardFiles "consumer-rules.pro"
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
compileOptions {
25+
sourceCompatibility JavaVersion.VERSION_1_8
26+
targetCompatibility JavaVersion.VERSION_1_8
27+
}
28+
}
29+
30+
dependencies {
31+
implementation 'androidx.appcompat:appcompat:1.3.1'
32+
implementation 'com.google.android.material:material:1.4.0'
33+
testImplementation 'junit:junit:4.+'
34+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
35+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
36+
implementation project(path: ':kvstorage')
37+
api 'com.tencent:mmkv-static:1.2.10'
38+
}
39+
apply from: '../maven_public.gradle'

kvstorage-mmkv/consumer-rules.pro

Whitespace-only changes.

kvstorage-mmkv/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

0 commit comments

Comments
 (0)