manifest-config-processor

Annotation processor that generates classes to look up configuration from an android application manifest

License

License

"The Apache License, Version 2.0"
Categories

Categories

config Application Layer Libs Configuration
GroupId

GroupId

io.github.rakutentech.manifestconfig
ArtifactId

ArtifactId

manifest-config-processor
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

manifest-config-processor
Annotation processor that generates classes to look up configuration from an android application manifest
Project URL

Project URL

https://github.com/rakutentech/android-manifest-config
Source Code Management

Source Code Management

https://github.com/rakutentech/android-manifest-config.git

Download manifest-config-processor

Dependencies

runtime (4)

Group / Artifact Type Version
io.github.rakutentech.manifestconfig : manifest-config-annotations jar 0.2.0
org.jetbrains.kotlin : kotlin-stdlib-jdk7 jar 1.4.10
org.jetbrains.kotlin : kotlin-reflect jar 1.4.10
com.squareup : javapoet jar 1.10.0

Project Modules

There are no modules declared in this project.

Manifest Config

Maven Central CircleCI

Annotation processor that generates a class to lookup manifest metadata.

Example

  1. 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"
}
  1. Define the config interface
@ManifestConfig
public interface ApiClient {
  int retries();
  String apiEndpoint();
}
  1. 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>
  1. 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 generation
  • manifest-config-processor: Annotation processor that consumes the annotations and generates implementations
  • manifest-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.
io.github.rakutentech.manifestconfig

Rakuten, Inc.

Versions

Version
0.2.0