AirCon
Remote config management Android library powered by annotation processing and code generation.
- Manage your app's remote configs using simple annotations, use generated providers classes to obtain config values.
- Supports adding any config source (FireBase support included in library).
- Supports custom validation and adaptation of config values.
- Inject remotely configured colors/Strings directly to XML layouts.
Usage
Initializing the SDK
The AirCon SDK should be initialized in the Application.onCreate() method using a AirConConfiguration
object.
Supported config types
Primitives
@BooleanConfig
@IntConfig
@LongConfig
@FloatConfig
@StringConfig
Collection
@StringSetConfig
Json
@JsonConfig
Enums
@StringEnumConfig
@IntEnumConfig
Special
@ColorConfig
@TimeConfig
@TextConfig
@UrlConfig
Mutable configs
All configs are read-only by default.
If the custom source supports overriding local values (e.g the built-in Firebase config source) use the @Mutable
annotation
on a config field to generate a setter for that config.
Remote config sources
AirCon supports any source of remote config.
For the common case of using FireBase as a remote config source, the library is packaged with a FireBaseConfigSource
.
Config sources can either be added in the SDK init phase:
Or at a later stage:
Multiple remote config sources for the same app are supported.
Each config is a assigned a source using the @Source
annotation.
Either define a source for the entire feature:
Or define a specific source for each config:
If a source is defined on both the feature interface and the config field, the config field source will be used.
Default value
A default value for a config can be defined in several ways (only one way can be used for the same config):
- Define the
defaultValue
attribute of the config annotation: - Define a resource as the default value:
- Define another config value as a default value:
- Define a custom default value provider method:
Custom value validation
Some config types provide inherit validation.
For example, the @UrlConfig
verifies that the configured URL is a valid URL, otherwise the default value is returned.
If custom validation is needed for a config the then a validation predicate method can be defined:
Custom adapters
Some config types provide implementation for common adaptation needs.
For example, the @JsonConfig
converts a remotely configured json string to an object.
In some cases extra processing is needed on the returned config value, which can be achieved be defining an adapter method:
An adapter method can return a different type than the original config type:
Enum configs
A common use case is converting a remotely configured int/String to an Enum:
Usage:
@StringEnumConfig
is also supported for converting strings to enum consts.
Config groups
Multiple configs can be groups together.
A POJO class containing all the config values will be generated and can be passed around to methods.
Groups can be used in 2 ways:
- For every feature annotated with
@FeatureRemoteConfig
a group containing all configs is generated.
Usage:
- Creating a custom group and defining the contained configs.
The value of the field defines the group name.
Usage:
Mock values
Mocking remotely configured values is useful for testing purposes.
To mock a config value a mock method should be defined:
A good practice for preventing mocks in release builds is defining all the config mock methods in the same class and ignore it in git.
In addition, a custom lint check will throw an error if a config mock is defined in a release build.
Custom config types
AirCon supports defining custom config type annotations in cases where the built-in types are not sufficient
or when common validation and processing is used across multiple config keys.
To define a custom config type:
- Define a config type resolver
The resolver defines:
- The custom config annotation (defined in step 2) to which it defines the processing logic.
- How a configured value is processed for the custom type.
- The primitive type of the config (i.e the type that will be used remotely).
- The processed type of the config, which can be whatever you like.
- Define a config type annotation and annotate it with
@ConfigType
and bind it to the resolver you've created via
the parameter to the annotation
The annotation can contain any number of attributes but must contain a defaultValue
attribute.
Note that the annotation must be retained in runtime and be targeted only for fields.
- Register the custom config type when initializing the SDK
- Use the custom config
The new config type acts exactly the same as the build-in config annotations and therefore
can be used in conjunction with all other AirCon features such as mocks, custom adapters, custom validators and so on.
XML injection
Aircon is also equipped with XML injection capabilities.
Injection for attributes of type String, color and color state list are supported.
To use XML injection:
- Enable XML injection by providing the app R.attr class and a config source:
Or provide a custom attribute resolver:
- Define attributes corresponding to config keys:
- Define default values in the app theme:
- Use attributes in XML:
- Extend either
AirConAppCompatActivity
orAirConFragmentActivity
. - Behold the magic.
Lint
AirCon library is bundled with various lint checks for verifying correct usage of the library.
No extra integration is needed from the app side.
Download
For using Firebase config source:
For using Gson
to parse @JsonConfig
:
If you are using Kotlin, replace annotationProcessor
with kapt
.