Working on multiprocess Android apps is a complex undertaking. One of the biggest challenges is managing shared data between the multiple processes. Most solutions rely on one process to be available for another to read the data, which can be quite slow and could potentially lead to ANRs.
Harmony is a thread-safe, process-safe, full SharedPreferences
implementation. It can be used in place of SharedPreferences
everywhere.
SharedPreference
object, without requiring another process to startSharedPreferences
implementationOnSharedPreferenceChangeListener
emits changes made by other processesContentProvider
, Service
, BroadcastReceiver
, or AIDLSharedPreferences
The latest release is available on Maven Central.
implementation 'com.frybits.harmony:harmony:1.2.6'
// implementation 'com.frybits.harmony:harmony-crypto:1.2.6' // For Encrypted SharedPreferences
// Getting Harmony SharedPreferences
val prefs: SharedPreferences = context.getHarmonySharedPreferences("PREF_NAME")
// Getting Harmony SharedPreferences
SharedPreferences prefs = Harmony.getSharedPreferences(context, "PREF_NAME")
OR
harmony-crypto
library)// Getting Encrypted Harmony SharedPreferences
val prefs: SharedPreferences = context.getEncryptedHarmonySharedPreferences(
"PREF_NAME",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
// Getting Encrypted Harmony SharedPreferences
SharedPreferences prefs = EncryptedHarmony.getSharedPreferences(
context,
"PREF_NAME",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
Once you have this SharedPreferences
object, it can be used just like any other SharedPreferences
. The main difference with Harmony is that any change made to "PREF_NAME"
using apply()
or commit()
is reflected across all processes.
NOTE: Changes in Harmony do not reflect in Android SharedPreferences and vice-versa!
The following are comparison performance tests of some popular multiprocess preference libraries. Each test measures the time it takes to insert 1000 items individually into the preference library (Write), the time it takes to read each 1000 items individually (Read), and how long it took for each item to be available in an alternate process (IPC). Each test was run 10 times. All values in the table below are the average time for a single item to be inserted, read, and available in the alternate process.
Tests were broken into two separate categories:
Logic for tests can been seen in the TestRunner.kt
file.
Notes
Library | Read (avg) | Write (avg) | IPC (avg) 1 |
---|---|---|---|
SharedPreferences | 0.0004 ms | 0.029ms | N/A |
Harmony | 0.001 ms | 0.026 ms | 113.417 ms |
MMKV 2 | 0.012 ms | 0.051 ms | 152.887 ms 3 |
Tray 2 | 1.501 ms | 1.917 ms | 1.294 s |
Library | Read (avg) | Write (avg) | IPC (avg) 1 |
---|---|---|---|
SharedPreferences | 0.0002 ms | 0.429 ms | N/A |
Harmony | 0.002 ms | 6.466 ms | 11.692 ms |
MMKV 2 | 0.013 ms | 0.046 ms | 168.328 ms 3 |
Tray 2 | 1.589 ms | 1.956 ms | 1.384 s |
1 IPC is the time it took for the item to be available in a secondary process. SharedPreferences doesn't support IPC.
2 These libraries don't support asynchronous writes. All tests were synchronous writes by default.
3 MMKV doesn't support a change listener, so a while-loop in a separate thread was used to determine how soon the data was available in the separate process. See MMKVRemoteTestRunnerService.kt
for implementation details.
This section is to give a special thanks to individuals that helped with getting this project where it is today.
Finally, a very special thank you to @life360 for integrating this project and providing incredibly valuable feedback that I was able to use to improve Harmony.
Copyright 2020 Pablo Baxter
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.