Manifest Config
Annotation processor that generates a class to lookup manifest metadata.
Example
- Add annotation processor to your project
repositories {
mavenCentral()
}
dependencies {
implementation "io.github.rakutentech.manifestconfig:manifest-config-annotations:$version"
annotationProcessor "io.github.rakutentech.manifestconfig:manifest-config-processor:$version"
}
- Define the config interface
@ManifestConfig
public interface ApiClient {
int retries();
String apiEndpoint();
}
- Configure manifest meta-data
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<application>
<meta-data
android:name="Retries"
android:value="1"/>
<meta-data
android:name="ApiEndpoint"
android:value="https://example.com/"/>
</application>
</manifest>
- Read config from generated class
ApiClientManifestConfig
(which looks up"Retries"
and"ApiEndpoint"
from the manifest meta data).
ApiClient config = new ApiClientManifestConfig(context);
config.retries();
config.apiEndpoint();
Why?: We use manifest metadata to configure many of our libraries with static values. To reduce duplication in both source and test code we generate that repetitive code.
Advanced Features
For more customization regarding meta keys and fallback values you can use the @MetaData
annotation on interface methods, e.g.
@ManifestConfig
public interface ApiClient {
@MetaData(key = "my.package.prefix.Retries", value = "4")
int retries();
@MetaData(key = "my.package.prefix.ApiEndpoint", value = "https://example.com")
String apiEndpoint();
}
// Will generate lookup calls like this:
public interface ApiClientManifestconfig {
// ... boilerplate
@Override
public int retries() {
return metaData.getInt("my.package.prefix.Retries", 4);
}
@Override
public String apiEndpoint() {
return metaData.getString("my.package.prefix.ApiEndpoint", "https://example.com");
}
}
Supported types & fallback value parsing
type | default value | parsing of MetaData.value() |
---|---|---|
int , java.lang.Integer |
-1 |
Kotlin's String.toInt |
float , java.lang.Float |
-1.0f |
Kotlin's String.toFloat |
boolean , java.lang.Boolean |
false |
only "true" and "false" convert (ignoring case) |
java.lang.String |
`""`` | taken as is |
Contributions
Found a bug? Please file an issue or send a pull request
Project structure
manifest-config-annotations
: Java annotations to mark source code for generationmanifest-config-processor
: Annotation processor that consumes the annotations and generates implementationsmanifest-config-sample
: Example project
Changelog
0.2.0 (2021-03-09)
- Changed Maven Group ID to
io.github.rakutentech.manifestconfig
. You must upudate your dependency declarations to the following:io.github.rakutentech.manifestconfig:manifest-config-annotations:0.2.0
io.github.rakutentech.manifestconfig:manifest-config-processor:0.2.0
- Migrated publishing to Maven Central due to Bintray/JCenter being shutdown. You must add
mavenCentral()
to your `repositories``.
0.1.0 (2018-08-14)
- Initial release.