Skip to content

Commit a714605

Browse files
committed
fix: MMKV can't decode bug
feat: add SdkTest.kt for test (sdk-ktx)'s ability
1 parent efc60bc commit a714605

File tree

8 files changed

+108
-31
lines changed

8 files changed

+108
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dependencyResolutionManagement {
2727
```groovy
2828
dependencies {
2929
// add sdk-ktx to your dependencies
30-
implementation 'com.github.pslilysm:sdk-ktx:2.0.1'
30+
implementation 'com.github.pslilysm:sdk-ktx:2.0.2'
3131
}
3232
```
3333

app/build.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ android {
3232
dependencies {
3333

3434
implementation 'androidx.core:core-ktx:1.8.0'
35-
implementation 'androidx.appcompat:appcompat:1.4.2'
35+
implementation 'androidx.appcompat:appcompat:1.5.0'
3636
implementation 'com.google.android.material:material:1.6.1'
3737
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
38-
testImplementation 'junit:junit:4.13.2'
39-
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
40-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
38+
39+
implementation 'com.tencent:mmkv:1.2.13'
40+
41+
implementation project(':sdk-library')
42+
43+
implementation 'com.google.code.gson:gson:2.9.1'
4144
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package perx.pslilysm.sdk_ktx
22

3-
import androidx.appcompat.app.AppCompatActivity
43
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
55

66
class MainActivity : AppCompatActivity() {
77
override fun onCreate(savedInstanceState: Bundle?) {
88
super.onCreate(savedInstanceState)
99
setContentView(R.layout.activity_main)
10+
SdkTest.test()
1011
}
1112
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package perx.pslilysm.sdk_ktx
2+
3+
import android.util.Log
4+
import com.tencent.mmkv.MMKV
5+
import pers.pslilysm.sdk_library.AppHolder
6+
import pers.pslilysm.sdk_library.util.*
7+
import pers.pslilysm.sdk_library.util.MMKVExtensions.decode
8+
import java.util.*
9+
10+
/**
11+
*
12+
*
13+
* @author cxd
14+
* Created on 2022/8/16 10:25
15+
*/
16+
object SdkTest {
17+
18+
private const val TAG = "CXD-SdkTest"
19+
20+
init {
21+
MMKV.initialize(AppHolder.get())
22+
}
23+
24+
fun test() {
25+
var value: Any = AesUtil.encrypt("pslilysm")
26+
Log.i(TAG, "test: $value")
27+
value = AesUtil.decrypt(value as String)
28+
Log.i(TAG, "test: $value")
29+
val json = "{\n" +
30+
" \"psl\": 24,\n" +
31+
" \"cxd\": 23,\n" +
32+
" \"fyh\": 23\n" +
33+
"}"
34+
value = GsonUtil.jsonToMap(json, Int::class.java)
35+
(value as Map<String, Int>).forEach { _key: String, _value: Int ->
36+
Log.i(TAG, "test: key -> $_key, value -> $_value")
37+
}
38+
value = GsonUtil.objToJson(value, true)
39+
Log.i(TAG, "test: $value")
40+
MMKV.defaultMMKV().encode("key", 12)
41+
value = MMKV.defaultMMKV().decode("key", 123)
42+
Log.i(TAG, "test: $value")
43+
MMKV.defaultMMKV().encode("key", "abc")
44+
value = MMKV.defaultMMKV().decode("key", "aa")
45+
Log.i(TAG, "test: $value")
46+
MMKV.defaultMMKV().encode("key", 3F)
47+
value = MMKV.defaultMMKV().decode("key", 4F)
48+
Log.i(TAG, "test: $value")
49+
MMKV.defaultMMKV().encode("key", 4L)
50+
value = MMKV.defaultMMKV().decode("key", 5L)
51+
Log.i(TAG, "test: $value")
52+
MMKV.defaultMMKV().encode("key", true)
53+
value = MMKV.defaultMMKV().decode("key", false)
54+
Log.i(TAG, "test: $value")
55+
val set = mutableSetOf<String>()
56+
set.add("1")
57+
set.add("2")
58+
MMKV.defaultMMKV().encode("key", set)
59+
value = MMKV.defaultMMKV().decode("key", HashSet<String>())
60+
Log.i(TAG, "test: $value")
61+
Log.i(TAG, "test: " + FormatterUtil.getyyyyMMddHHmmssFormatter().format(Date()))
62+
Log.i(TAG, "test: " + FormatterUtil.get_0dot00Formatter().format(36.24444))
63+
GlobalExecutors.io().execute {
64+
Log.i(TAG, "test: run")
65+
}
66+
ThreadUtil.checkIsMainThread()
67+
ToastUtil.showShort("sdk test")
68+
}
69+
70+
}

sdk-library/src/main/java/pers/pslilysm/sdk_library/util/AesUtil.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ object AesUtil {
9797
* @return 加密好的字符串,最后会BASE64编码一下
9898
*/
9999
@kotlin.jvm.JvmStatic
100-
fun encrypt(str: String, aesMode: String?, aesKey: String, ivKey: String): String {
100+
fun encrypt(str: String, aesMode: String, aesKey: String, ivKey: String): String {
101101
return try {
102102
val cipher = Cipher.getInstance(aesMode)
103103
cipher.init(
@@ -122,7 +122,7 @@ object AesUtil {
122122
* @return 解密好的字符串
123123
*/
124124
@kotlin.jvm.JvmStatic
125-
fun decrypt(str: String?, aesMode: String?, aesKey: String, ivKey: String): String {
125+
fun decrypt(str: String, aesMode: String, aesKey: String, ivKey: String): String {
126126
return try {
127127
val cipher = Cipher.getInstance(aesMode)
128128
cipher.init(

sdk-library/src/main/java/pers/pslilysm/sdk_library/util/GsonUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ object GsonUtil {
102102
jsonObject,
103103
"members"
104104
)
105-
members.forEach { (s: String, jsonElement: JsonElement?) ->
105+
members!!.forEach { (s: String, jsonElement: JsonElement?) ->
106106
result[s] = sGson.fromJson(jsonElement, vClass)
107107
}
108108
} catch (e: ReflectiveOperationException) {

sdk-library/src/main/java/pers/pslilysm/sdk_library/util/MMKVExtensions.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ object MMKVExtensions {
6464
*
6565
* @param key mapped value
6666
* @param defaultValue will be return if not find the key
67-
* @return saved value by key
67+
* @return defaultValue if not found the key
6868
*/
6969
@kotlin.jvm.JvmStatic
70-
fun <T> MMKV.decode(key: String, defaultValue: T): T? {
71-
return decode<T>(key, defaultValue, null)
70+
fun <T> MMKV.decode(key: String, defaultValue: T): T {
71+
return decode<T>(key, defaultValue, null)!!
7272
}
7373

7474
/**
@@ -89,35 +89,35 @@ object MMKVExtensions {
8989
} else {
9090
valueClass!!
9191
}
92-
return if (clazz == Int::class.java) {
92+
return if (clazz == Int::class.javaPrimitiveType || clazz == Int::class.javaObjectType) {
9393
Integer.valueOf(
9494
this.decodeInt(
9595
key,
9696
(if (defaultValue == null) 0 else defaultValue as Int?)!!
9797
)
9898
) as T
99-
} else (if (clazz == Float::class.java) {
99+
} else (if (clazz == Float::class.javaPrimitiveType || clazz == Float::class.javaObjectType) {
100100
java.lang.Float.valueOf(
101101
this.decodeFloat(
102102
key,
103103
(if (defaultValue == null) 0 else defaultValue as Float?) as Float
104104
)
105105
) as T
106-
} else if (clazz == Double::class.java) {
106+
} else if (clazz == Double::class.javaPrimitiveType || clazz == Double::class.javaObjectType) {
107107
java.lang.Double.valueOf(
108108
this.decodeDouble(
109109
key,
110110
(if (defaultValue == null) 0 else defaultValue as Double?) as Double
111111
)
112112
) as T
113-
} else if (clazz == Long::class.java) {
113+
} else if (clazz == Long::class.javaPrimitiveType || clazz == Long::class.javaObjectType) {
114114
java.lang.Long.valueOf(
115115
this.decodeLong(
116116
key,
117117
(if (defaultValue == null) 0 else defaultValue as Long?)!!
118118
)
119119
) as T
120-
} else if (clazz == Boolean::class.java) {
120+
} else if (clazz == Boolean::class.javaPrimitiveType || clazz == Boolean::class.javaObjectType) {
121121
java.lang.Boolean.valueOf(
122122
this.decodeBool(
123123
key,
@@ -146,7 +146,6 @@ object MMKVExtensions {
146146
* @param tClass value's class
147147
* @param <T> the type of the value and must extends [Parcelable]
148148
* @return a parcelable value by the key
149-
* @see .decodeParcelable
150149
*/
151150
@kotlin.jvm.JvmStatic
152151
fun <T : Parcelable?> MMKV.decodeParcelable(key: String?, tClass: Class<T?>?): T? {
@@ -167,8 +166,8 @@ object MMKVExtensions {
167166
key: String?,
168167
tClass: Class<T>?,
169168
defaultValue: T
170-
): T? {
171-
return this.decodeParcelable(key, tClass, defaultValue)
169+
): T {
170+
return this.decodeParcelable(key, tClass, defaultValue)!!
172171
}
173172

174173
}

sdk-library/src/main/java/pers/pslilysm/sdk_library/util/reflection/ReflectionUtil.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ object ReflectionUtil {
238238

239239
@kotlin.jvm.JvmStatic
240240
@Throws(ReflectiveOperationException::class)
241-
fun <T> getFieldValue(`object`: Any?, fieldName: String): T {
241+
fun <T> getFieldValue(`object`: Any?, fieldName: String): T? {
242242
return findOrCreateField(`object`!!.javaClass, fieldName)[`object`] as T
243243
}
244244

@@ -250,13 +250,17 @@ object ReflectionUtil {
250250

251251
@kotlin.jvm.JvmStatic
252252
@Throws(ReflectiveOperationException::class)
253-
fun <T> invokeMethod(`object`: Any, methodName: String): T {
253+
fun <T> invokeMethod(`object`: Any, methodName: String): T? {
254254
return invokeMethod(`object`, methodName, *sEmptyParameterTypesAndArgs)
255255
}
256256

257257
@kotlin.jvm.JvmStatic
258258
@Throws(ReflectiveOperationException::class)
259-
fun <T> invokeMethod(`object`: Any, methodName: String, vararg parameterTypesAndArgs: Any?): T {
259+
fun <T> invokeMethod(
260+
`object`: Any,
261+
methodName: String,
262+
vararg parameterTypesAndArgs: Any?
263+
): T? {
260264
val clazz: Class<*> = `object`.javaClass
261265
val classLoader =
262266
if (clazz.classLoader == null) ClassLoader.getSystemClassLoader() else clazz.classLoader
@@ -275,7 +279,7 @@ object ReflectionUtil {
275279

276280
@kotlin.jvm.JvmStatic
277281
@Throws(ReflectiveOperationException::class)
278-
fun <T> getStaticFieldValue(className: String, fieldName: String): T {
282+
fun <T> getStaticFieldValue(className: String, fieldName: String): T? {
279283
return getStaticFieldValue(Class.forName(className), fieldName)
280284
}
281285

@@ -285,13 +289,13 @@ object ReflectionUtil {
285289
className: String,
286290
classLoader: ClassLoader,
287291
fieldName: String
288-
): T {
292+
): T? {
289293
return getStaticFieldValue(classLoader.loadClass(className), fieldName)
290294
}
291295

292296
@kotlin.jvm.JvmStatic
293297
@Throws(ReflectiveOperationException::class)
294-
fun <T> getStaticFieldValue(clazz: Class<*>, fieldName: String): T {
298+
fun <T> getStaticFieldValue(clazz: Class<*>, fieldName: String): T? {
295299
return findOrCreateField(clazz, fieldName)[null] as T
296300
}
297301

@@ -320,7 +324,7 @@ object ReflectionUtil {
320324

321325
@kotlin.jvm.JvmStatic
322326
@Throws(ReflectiveOperationException::class)
323-
fun <T : Any> invokeStaticMethod(className: String, methodName: String): T {
327+
fun <T : Any> invokeStaticMethod(className: String, methodName: String): T? {
324328
return invokeStaticMethod(
325329
ClassLoader.getSystemClassLoader().loadClass(className),
326330
methodName,
@@ -334,7 +338,7 @@ object ReflectionUtil {
334338
className: String,
335339
classLoader: ClassLoader,
336340
methodName: String
337-
): T {
341+
): T? {
338342
return invokeStaticMethod(
339343
classLoader.loadClass(className),
340344
methodName,
@@ -344,7 +348,7 @@ object ReflectionUtil {
344348

345349
@kotlin.jvm.JvmStatic
346350
@Throws(ReflectiveOperationException::class)
347-
fun <T> invokeStaticMethod(clazz: Class<*>, methodName: String): T {
351+
fun <T> invokeStaticMethod(clazz: Class<*>, methodName: String): T? {
348352
return invokeStaticMethod(clazz, methodName, *sEmptyParameterTypesAndArgs)
349353
}
350354

@@ -355,7 +359,7 @@ object ReflectionUtil {
355359
classLoader: ClassLoader,
356360
methodName: String,
357361
vararg parameterTypesAndArgs: Any?
358-
): T {
362+
): T? {
359363
return invokeStaticMethod(
360364
classLoader.loadClass(className),
361365
methodName,
@@ -369,7 +373,7 @@ object ReflectionUtil {
369373
clazz: Class<*>,
370374
methodName: String,
371375
vararg parameterTypesAndArgs: Any?
372-
): T {
376+
): T? {
373377
val classLoader =
374378
if (clazz.classLoader == null) ClassLoader.getSystemClassLoader() else clazz.classLoader
375379
val splitParameterTypesAndArgs =

0 commit comments

Comments
 (0)