{"id":16482146,"url":"https://github.com/bod/android-prefs","last_synced_at":"2025-06-28T02:39:12.554Z","repository":{"id":31017361,"uuid":"34575845","full_name":"BoD/android-prefs","owner":"BoD","description":"Android preferences for WINNERS!","archived":false,"fork":false,"pushed_at":"2018-12-23T17:13:51.000Z","size":427,"stargazers_count":182,"open_issues_count":3,"forks_count":23,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-02-27T12:14:16.985Z","etag":null,"topics":["android","sharedpreferences"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BoD.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-25T16:21:23.000Z","updated_at":"2023-10-29T21:35:12.000Z","dependencies_parsed_at":"2022-09-08T17:22:37.622Z","dependency_job_id":null,"html_url":"https://github.com/BoD/android-prefs","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2Fandroid-prefs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2Fandroid-prefs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2Fandroid-prefs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2Fandroid-prefs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BoD","download_url":"https://codeload.github.com/BoD/android-prefs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826783,"owners_count":20354220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","sharedpreferences"],"created_at":"2024-10-11T13:09:47.027Z","updated_at":"2025-03-16T18:31:39.378Z","avatar_url":"https://github.com/BoD.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"Prefs\n===\n\nAndroid preferences for WINNERS!\n\n![Be a winner!](/illus.jpg?raw=true \"Be a winner!\")\n\n\nThis little tool generates wrappers for your SharedPreferences, so you can benefit from compile time\nverification and code completion in your IDE.  You also get nice singletons for free.\n\n[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-android--prefs-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/1758)\n\nUsage\n---\n\n### 1/ Add the dependencies to your project\n\n```groovy\ndependencies {\n    /* ... */\n    annotationProcessor 'org.jraf:prefs-compiler:1.4.0' // or kapt if you use Kotlin\n    implementation 'org.jraf:prefs:1.4.0'\n}\n```\n\n\n### 2/ Define your preferences\n\nUse the `@Prefs` annotation on any plain old Java object.  All its (non static) fields will be considered a preference.\n\nFor instance:\n\n```java\n@Prefs\npublic class Main {\n    /**\n     * User login.\n     */\n    String login;\n\n    /**\n     * User password.\n     */\n    String password;\n\n    @DefaultBoolean(false)\n    Boolean isPremium;\n\n    @Name(\"PREF_AGE\")\n    Integer age;\n}\n```\n\nCurrently, the accepted types are:\n- Boolean\n- Float\n- Integer\n- Long\n- String\n- Set\\\u003cString\\\u003e\n\nOptionally, use `@DefaultXxx` and `@Name` annotations (the default default is `null`, and the default name is the name of your field).\n\nYou can pass a file name and mode (as per [Context.getSharedPreference()](http://developer.android.com/reference/android/content/Context.html#getSharedPreferences(java.lang.String, int))) like this:\n```java\n@Prefs(fileName = \"settings\", fileMode = Context.MODE_PRIVATE)\n```\n\nIf you don't, `PreferenceManager.getDefaultSharedPreferences(Context)` will be used.\n\n\n### 3/ Be a winner!\n\nA class named `\u003cYourClassName\u003ePrefs` will be generated in the same package (at compile time).  Use it like this:\n\n```java\n        MainPrefs mainPrefs = MainPrefs.get(this);\n\n        // Put a single value (apply() is automatically called)\n        mainPrefs.putAge(42);\n\n        // Put several values in one transaction\n        mainPrefs.edit().putLogin(\"john\").putPassword(\"p4Ssw0Rd\").apply();\n\n        // Check if a value is set\n        if (mainPrefs.containsLogin()) doSomething();\n\n        // Remove a value\n        mainPrefs.removeAge();\n        // Or (this has the same effect)\n        mainPrefs.putAge(null);\n\n        // Clear all values!\n        mainPrefs.clear();\n```\n\nBonus 1: in Kotlin you can directly use `=`:\n```kotlin\n        // Put a single value (apply() is automatically called)\n        mainPrefs.age = 42\n```\n\nBonus 2: you also get `LiveData`s to observe your preferences:\n```kotlin\n        mainPrefs.loginLiveData.observe(this, Observer {\n            Log.d(TAG, \"observed login=$it\")\n        })\n```\nNote: currently this is disabled by default (because maybe you don't use `LiveData`?) - add `generateGetLiveData = true`\nto your `@Prefs` annotation to enabled it.\n\n\nLicense\n---\n\n```\nCopyright (C) 2015-present Benoit 'BoD' Lubek (BoD@JRAF.org)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n\n__*Just to be absolutely clear, this license applies to this program itself,\nnot to the source it will generate!*__\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbod%2Fandroid-prefs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbod%2Fandroid-prefs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbod%2Fandroid-prefs/lists"}