This is a wrapper library for SharedPreferences
for Android, based on Lombok (with @Data
annotation), which allows you to manage SharedPreferences
more elegantly.
-
Install the
Lombok
plugin for your Android Studio according to the method provided in this repository: sgpublic/lombok-plugin-action: A repository for Lombok plugin incompatibility issues with Android Studio. (github.com) -
Add dependencies in
build.gradle
.-
For Java project
dependencies { implementation "io.github.sgpublic:exsp-runtime:$latest" annotationProcessor "io.github.sgpublic:exsp-compiler:$latest" def lombok_ver = "1.18.24" compileOnly "org.projectlombok:lombok:$lombok_ver" annotationProcessor "org.projectlombok:lombok:$lombok_ver" }
-
For Kotlin project, see Lombok compiler plugin | Kotlin (kotlinlang.org) for more details.
plugins { id 'org.jetbrains.kotlin.plugin.lombok' version '1.7.10' id 'io.freefair.lombok' version '5.3.0' id 'kotlin-kapt' } kapt { keepJavacAnnotationProcessors = true } dependencies { implementation "io.github.sgpublic:exsp-runtime:$latest" kapt "io.github.sgpublic:exsp-compiler:$latest" def lombok_ver = "1.18.24" compileOnly "org.projectlombok:lombok:$lombok_ver" annotationProcessor "org.projectlombok:lombok:$lombok_ver" }
-
-
Create a new class for managing
SharedPreferences
, and add@ExSharedPreference
and@Data
annotations.PS: Whether your project uses
Java
orKotlin
, this class must beJava
!@Data @ExSharedPreference(name = "test") public class TestPreference { }
-
Add member variables to this class and add the
@ExValue
annotation to set the default value.@Data @ExSharedPreference(name = "name_of_shared_preference") public class TestPreference { @ExValue(defVal = "test") private String testString; @ExValue(defVal = "0") private float testFloat; @ExValue(defVal = "0") private int testInt; @ExValue(defVal = "0") private long testLong; @ExValue(defVal = "false") private boolean testBool; @ExValue(defVal = "TYPE_A") private Type testEnum; public enum Type { TYPE_A, TYPE_B; } }
-
Use
ExPreference.init(Context context)
method inApplication
to initializeContext
.class App: Application() { override fun onCreate() { super.onCreate() ExPreference.init(this) } }
-
Now you can manage
SharedPreferences
directly usinggetters
/setters
provided byLombok
, enjoy it!-
Kotlin
val test: TestPreference = ExPreference.get() test.testString = "new string" Log.d("TestPreference#testString", test.testString)
-
Java
TestPreference test = ExPreference.get(TestPreference.class); test.setTestString("new string"); Log.d("TestPreference#testString", test.getTestString());
-
-
If you set
isMinifyEnabled = true
in your project, you should add toproguard-rules.pro
:-keepclassmembers class io.github.sgpublic.exsp.ExPrefs { public static *** get(***); }
ExSharedPreference
allows you to save custom types into SharedPreferences, but since SharedPreferences only supports a limited number of types, we use the conversion mechanism to complete this function.
PS: We've added special support for enum types, so you needn't to add converters for enum types.
-
Add the required custom types to the class directly.
Note: The
defVal
needs to fill in the string of the original type value, not your custom type!@Data @ExSharedPreference(name = "name_of_shared_preference") public class TestPreference { ... @ExValue(defVal = "-1") private Date testDate; ... }
-
Create a class that implements the Converter interface and add
@ExConverter
annotation.@ExConverter class DateConverter: Converter<Date, Long> { override fun toPreference(origin: Date): Long { return origin.time } override fun fromPreference(target: Long): Date { return Date(target) } }
The first parameter of the
Converter
generic parameter is your custom type, and the second parameter is the type actually stored inSharedPreferences
.
We have a demo using Kotlin
to demonstrate how ExSharedPreference
used: demo-kotlin.
This annotation is used to create a SharedPreferences
whose parameters correspond to the Context#getSharedPreferences(String name, int mode)
method.
That is, the following annotation:
@ExSharedPreference(name = "name_of_shared_preference", mode = Context.MODE_PRIVATE)
means the following statement:
SharedPreferences sharedPreference = context.getSharedPreference("name_of_shared_preference", Context.MODE_PRIVATE);
-
name
:String
(Required) Desired preferences file.
-
mode
:int
/Intefer
(Optional) Operating mode, default is
Context.MODE_PRIVATE
.
This annotation is used to mark a SharedPreference
key whose parameters correspond to the getXxxx
methods of class android.content.SharedPreferences
.
That is, the following annotation:
@ExValue(name = "test_string", defVal = "default value")
private String testString;
means the following statement:
// getter
sharedPreference.getString("test_string", "default value");
// setter
sharedPreference.editor()
.putString("test_string", "new value")
.apply();
-
name
:String
(Optional) The name of the preference to retrieve, default is the variable name with a capital letter.
-
defVal
:String
(Required) Value to return if this preference does not exist.
This annotation is used to mark a custom type converter for ExSharedPreference
processing.