manifest-config-annotations

Annotations for manifest-config-annotation processor to configure code generation

Лицензия

Лицензия

"The Apache License, Version 2.0"
Категории

Категории

config Библиотеки уровня приложения Configuration
Группа

Группа

io.github.rakutentech.manifestconfig
Идентификатор

Идентификатор

manifest-config-annotations
Последняя версия

Последняя версия

0.2.0
Дата

Дата

Тип

Тип

pom.sha512
Описание

Описание

manifest-config-annotations
Annotations for manifest-config-annotation processor to configure code generation
Ссылка на сайт

Ссылка на сайт

https://github.com/rakutentech/android-manifest-config
Система контроля версий

Система контроля версий

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

Скачать manifest-config-annotations

Зависимости

Библиотека не имеет зависимостей. Это самодостаточное приложение, которое не зависит ни от каких других библиотек.

Модули Проекта

Данный проект не имеет модулей.

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.

Версии библиотеки

Версия
0.2.0