/android-manifest-config

Annotation processor that generates a class to lookup manifest metadata.

Primary LanguageJavaApache License 2.0Apache-2.0

Manifest Config

CircleCI

Annotation processor that generates a class to lookup manifest metadata.

Download

Example

  1. Add annotation processor to your project
repositories {
  jcenter()
}
dependencies {
  implementation        "com.rakuten.tech.mobile:manifest-config-annotations:$version"
  annotationProcessor   "com.rakuten.tech.mobile: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 <<<<<<< HEAD

=======

Publishing

Setup environment variables:

  • BINTRAY_USER
  • BINTRAY_KEY
  • BINTRAY_REPO

and run

./gradlew publish

publish: setup publishing to jcenter