{"id":19602711,"url":"https://github.com/qiwi/featuretoggle","last_synced_at":"2025-04-27T17:32:21.939Z","repository":{"id":38313341,"uuid":"417423516","full_name":"qiwi/FeatureToggle","owner":"qiwi","description":"Feature Toggle library for Android","archived":false,"fork":false,"pushed_at":"2022-09-28T09:05:02.000Z","size":220,"stargazers_count":21,"open_issues_count":0,"forks_count":1,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-04-05T01:51:14.590Z","etag":null,"topics":["android","android-library","feature-flag","feature-flags","feature-manager","feature-toggle","feature-toggles","kotlin"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qiwi.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":"2021-10-15T08:24:24.000Z","updated_at":"2025-03-27T06:27:23.000Z","dependencies_parsed_at":"2022-08-17T15:55:34.217Z","dependency_job_id":null,"html_url":"https://github.com/qiwi/FeatureToggle","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qiwi%2FFeatureToggle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qiwi%2FFeatureToggle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qiwi%2FFeatureToggle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qiwi%2FFeatureToggle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qiwi","download_url":"https://codeload.github.com/qiwi/FeatureToggle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251178042,"owners_count":21548152,"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","android-library","feature-flag","feature-flags","feature-manager","feature-toggle","feature-toggles","kotlin"],"created_at":"2024-11-11T09:26:00.120Z","updated_at":"2025-04-27T17:32:20.858Z","avatar_url":"https://github.com/qiwi.png","language":"Kotlin","readme":"# FeatureToggle\n\nThe Feature Toggle library for Android.\n\n## Overview\n\nThe `FeatureToggle` library allows you to configure features of an Android application at runtime using feature flags. \nHere are common usecases:\n- [Trunk-Based Developement](https://trunkbaseddevelopment.com).\n- Safe release with a new implementation of the app’s critical part. If the new implementation causes a critical problem, developers can switch back to the old implementation using a feature flag.\n- A/B testing, when feature flags are used to switch between multiple feature implementations.\n\nWhen you use `FeatureToggle` library, each dynamic feature in an Android application must be represented as a separate class or an interface with multiple implementations. \nEach dynamic feature has a `Feature Flag` with unique key and a `FeatureFactory`.\n\n`Feature Flag` is Kotlin class that contains one or more fields that describe feature configuration.\n\n`Feature Flag`s can be loaded from multiple `FeatureFlagDataSource`s. `FeatureFlagDataSource` has a priority value, which helps to decide which `FeatureFlagDataSource` should be used to apply a certain `Feature Flag`.\n\n`Feature Flag`s are stored in JSON format, and at runtime are represented as Koltin objects. \n\nA `FeatureFactory` is responsible for creating feature objects, using the provided `Feature Flag` objects to decide how to create feature objects.\n\nThe `FeatureToggle` library automatically generates registries of current application feature flags and factories using annotation processors.\n\n## Quick Start\n\n1. Add a feature manager and compiler:\n\n```kotlin\nimplementation(\"com.qiwi.featuretoggle:featuretoggle-feature-manager:0.1.3\")\n// To use kapt\nkapt(\"com.qiwi.featuretoggle:featuretoggle-compiler:0.1.3\")\n// To use ksp\nksp(\"com.qiwi.featuretoggle:featuretoggle-compiler:0.1.3\")\n```\n\n2. Add a converter that will be used to convert feature flags from Json into Kotlin objects. Two converters are available:\n[Jackson](https://github.com/FasterXML/jackson-module-kotlin) and [Gson](https://github.com/google/gson):\n\n```kotlin\nimplementation(\"com.qiwi.featuretoggle:featuretoggle-converter-jackson:0.1.3\")\n//or\nimplementation(\"com.qiwi.featuretoggle:featuretoggle-converter-gson:0.1.3\")\n```\n\n3. Add a feature flag with an unique key and factory for every feature:\n\n```kotlin\nclass SampleFeature {\n    //code\n}\n\n@FeatureFlag(\"feature_key\")\nclass SampleFeatureFlag(\n    //fields\n)\n\n@Factory\nclass SampleFeatureFeatureFactory : FeatureFactory\u003cSampleFeature, SampleFeatureFlag\u003e() {\n\n    override fun createFeature(flag: SampleFeatureFlag): SampleFeature {\n        //construct feature using flag\n    }\n\n    override fun createDefault(): AndroidInfoFeature {\n        //construct default feature implementation\n    }\n}\n```\n4. Create an instance of `FeatureManager` using `FeatureManager.Builder`. Provide a converter, necessary data sources and generated registries. It is recommended to fetch feature flags immediately after creating an instance of `FeatureManager`:\n\n```kotlin\nval featureManager = FeatureManager.Builder(context)\n    .converter(JacksonFeatureFlagConverter()) //or GsonFeatureFlagConverter()\n    .logger(DebugLogger()) //optional logger\n    .addDataSource(AssetsDataSource(\"feature_flags.json\", context)) //also available additional data sources: FirebaseDataSource, AgConnectDataSource, RemoteDataSource\n    .flagRegistry(FeatureFlagRegistryGenerated()) //set generated flag registry\n    .factoryRegistry(FeatureFactoryRegistryGenerated()) //set generated factory registry\n    .build()\n\nfeatureManager.fetchFlags()\n```\n\n5. Provide an instance of `FeatureManager` using your favourite DI framework, or you can use the `FeatureToggle` singleton:\n\n```kotlin\nimplementation(\"com.qiwi.featuretoggle:featuretoggle-feature-manager-singleton:0.1.3\")\n```\n```kotlin\nFeatureToggle.setFeatureManager(...)\n\nFeatureToggle.featureManager().getFeature(...)\n```\n\n6. Get a feature from `FeatureManager`:\n\n```kotlin\nval feature = featureManager.getFeature\u003cSampleFeature\u003e()\n```\n\nIt is recommended to wait for the feature flags to get loaded, for example on a splash screen:\n```kotlin\nclass SplashScreenActivity : AppCompatActivity() {\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        ...\n        lifecycleScope.launchWhenCreated {\n            runCatching {\n                withTimeout(TIMEOUT_MILLIS) {\n                    FeatureToggle.featureManager().awaitFullLoading()\n                }\n            }\n            openMainActivity()\n        }\n    }\n\n    ...\n}\n```\n## Assets DataSource\n\n`AssetsDataSource` loads feature flags from a JSON file from `assets` folder. It is used to include default feature flag values into apk or app bundle. Default `AssetsDataSource` priority value is `1`.\n\n## Cache\n\nFeature flags when loaded from remote data sources are cached and used on the next fetch. You can also set a priority of cached feature flags in a `FeatureManager.Builder`:\n```kotlin\nFeatureManager.Builder(context)\n    ...\n    .cachedFlagsPriority(2)\n```\nDefault cached flags priority value is `2`.\n\n## Remote Config DataSource\n\n`FeatureToggle` supports [Firebase Remote Config](https://firebase.google.com/docs/remote-config)\nand [AppGallery Connect Remote Configuration](https://developer.huawei.com/consumer/en/agconnect/remote-configuration/).\n\nAdd a remote config value with its feature flag key for every feature. Remote config value must be stored as a Json string. Sample:\n\n```json\n{\n    \"versionName\": \"12\",\n    \"apiLevel\": 31\n}\n```\n\nDefault remote config data sources priority value is `4`.\n\n### `FirebaseDataSource`:\n\n1. [Add Firebase to your Android project](https://firebase.google.com/docs/android/setup).\n2. If you need to use Google Analytics with Remote Config, add the analytics dependency:\n\n```kotlin\nimplementation(\"com.google.firebase:firebase-analytics:${version}\")\n```\n\n3. Add `FirebaseDataSource`:\n\n```kotlin\nimplementation(\"com.qiwi.featuretoggle:featuretoggle-datasource-firebase:0.1.3\")\n```\n```kotlin\nFeatureManager.Builder(context)\n    ...\n    .addDataSource(FirebaseDataSource())\n```\n4. Add feature flags into the **Engage \u003e Remote Config** section in the [Firebase console](https://console.firebase.google.com). Sample:\n\n\u003cimg src=\"https://user-images.githubusercontent.com/27818051/137912355-0ef1634d-75d5-4e57-88e9-cdaeeca8d753.png\" width=\"400\" height=\"300\"\u003e\n\nFor more details about Firebase Remote Config, look at the [official docs](https://firebase.google.com/docs/remote-config).\n\n### `AgConnectDataSource`:\n\n1. [Integrate the AppGallery Connect SDK](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-get-started-android-0000001058210705#section1552914317248).\n2. If you need to use HUAWEI Analytics with remote configuration, add the analytics dependency:\n\n```kotlin\nimplementation(\"com.huawei.hms:hianalytics:${version}\")\n```\n3. Add `AgConnectDataSource`:\n\n```kotlin\nimplementation(\"com.qiwi.featuretoggle:featuretoggle-datasource-agconnect:0.1.3\")\n```\n```kotlin\nFeatureManager.Builder(context)\n    ...\n    .addDataSource(AgConnectDataSource())\n```\n4. Add feature flags into the **Grow \u003e Remote Configuration** section in [AppGallery Connect](https://developer.huawei.com/consumer/en/service/josp/agc/index.html). Sample:\n\n\u003cimg src=\"https://user-images.githubusercontent.com/27818051/137916454-35613ba8-fe58-4198-8d71-ec52cb5db030.png\" width=\"700\" height=\"100\"\u003e\n\u003cimg src=\"https://user-images.githubusercontent.com/27818051/137917223-e1daafa8-3d05-4f81-9f95-b0caa4a417c5.png\" width=\"400\" height=\"300\"\u003e\n\nFor more details about AppGallery Connect Remote Configuration, look at the [official docs](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-remoteconfig-android-getstarted-0000001056347165).\n\n## Remote DataSource\n\nThe `FeatureToggle` library also has `RemoteDataSource` that can download feature flags from JSON REST API using the [OkHttp](https://github.com/square/okhttp) library.\nJSON response must be in the following format:\n```json\n[\n    {\n      \"feature\": \"android_info\",\n      \"versionName\": \"12\",\n      \"apiLevel\": 31\n    }\n]\n```\n\nUsage:\n\n```kotlin\nimplementation(\"com.qiwi.featuretoggle:featuretoggle-datasource-remote:0.1.3\")\n```\n```kotlin\nFeatureManager.Builder(context)\n    ...\n    .addDataSource(RemoteDataSource(\"url\"))\n```\n\nDefault `RemoteDataSource` priority value is `3`.\n\n## Debug DataSource\n\nIf you need to update feature flags manually (for debug purposes), use `DebugDataSource`:\n\n```kotlin\nimplementation(\"com.qiwi.featuretoggle:featuretoggle-datasource-debug:0.1.3\")\n```\n```kotlin\nval debugDataSource = DebugDataSource()\n\nFeatureManager.Builder(context)\n    ...\n    .addDataSource(debugDataSource)\n\n...\n\ndebugDataSource.updateFeatureFlagsFromJsonString(...)\n```\n\nDefault `DebugDataSource` priority value is `100`.\n\n## Custom DataSource\n\nYou can extend the `FeatureToggle` library with custom data source:\n\n```kotlin\nclass CustomDataSource : FeatureFlagDataSource {\n\n    override val sourceType: FeatureFlagsSourceType ...\n\n    override val key: String ...\n\n    override val priority: Int ...\n\n    override fun getFlags(\n        registry: FeatureFlagRegistry,\n        converter: FeatureFlagConverter,\n        logger: Logger\n    ): Flow\u003cMap\u003cString, Any\u003e\u003e = flow {\n        ...\n    }\n\n}\n```\n\n## Testing\n\nIf you need to use `FeatureManager` in unit tests, use `FakeFeatureManager`. It doesn’t load flags from data sources – but uses mocked feature flags instead.\nUsage example:\n\n```kotlin\ntestImplementation(\"com.qiwi.featuretoggle:featuretoggle-feature-manager-test:0.1.3\")\n```\n```kotlin\nval fakeFeatureManager = FakeFeatureManager.create(FeatureFlagRegistryGenerated(), FeatureFactoryRegistryGenerated())\n\n...\n\nfakeFeatureManager.overrideFlag(...)\n```\n\n## R8/Proguard\n\n`FeatureToggle` library modules have bundled proguard rules for its classes. However, you need to add a proguard rule for every feature flag class:\n\n```kotlin\n-keep class com.example.SampleFeatureFlag { *; }\n```\n\n## License\n\n    MIT License\n\n    Copyright (c) 2021 QIWI\n\n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n\n    The above copyright notice and this permission notice shall be included in all\n    copies or substantial portions of the Software.\n\n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n    SOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqiwi%2Ffeaturetoggle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqiwi%2Ffeaturetoggle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqiwi%2Ffeaturetoggle/lists"}