android-metadata

Additional

Language
Java
Version
1.5.1 (Aug 5, 2016)
Created
Feb 13, 2014
Updated
Aug 31, 2017
Owner
One Hill Technologies, LLC (onehilltech)
Contributors
Vladislav Bauer (vbauer)
hilljh82
2
Activity
Badge
Generate
Download
Source code

Advertisement

android-metadata

A utility library for Android designed to simplify reading meta-data values from AndroidManifest.xml.

  • Quickly access a meta-data values from anywhere with few lines of code.
  • Read individual meta-data values into type-specific variables.
  • Instantiate objects from meta-data values.
  • Read one or more meta-data values into annotated Java classes.
  • Use meta-data values to pass configuration parameters to third-party libraries.

Installation

Gradle

buildscript {
  repositories {
    maven { url "https://jitpack.io" }
  }
}

dependencies {
  compile com.github.onehilltech:android-metadata:x.y.z
}

Getting Started

Here is the quickest and easiest way to load the metadata from AndroidManifest.xml and get a value. The value, by default, is a String value type.

ManifestMetadata metadata = ManifestMetadata.get (context);

// <meta-data android:name="appid" android:value="32ba65ae723940" />
String value = metadata.getValue ("appid");

If the value is not a String type, then you can provide a hint:

ManifestMetadata metadata = ManifestMetadata.get (context);

// <meta-data android:name="conn.timeout" android:value="60" />
Integer connTimeout = metadata.getValue ("conn.timeout", Integer.class);

You can even directly load a resource from the metadata:

ManifestMetadata metadata = ManifestMetadata.get (context);

// <meta-data android:name="appname" android:resource="@string/app_name" />
String appName = metadata.getValue ("appname", true, String.class);

In some cases, you may need to provide additional information about the resource type since different resources types can have the same Java type:

ManifestMetadata metadata = ManifestMetadata.get (context);

// <meta-data android:name="bgcolor" android:resource="@color/background" />
Integer bgColor = metadata.getValue ("bgcolor", true, Integer.class, ResourceType.Color);

Using Annotations to Load Metadata

Here is the simplest example of using an annotation to define what meta-data value in AndroidManifest.xml it should be initialized with:

public class MyData {
  private String appid_;
  
  @MetadataProperty (name="my.message")
  public String message;
  
  @MetadataMethod (name="appid")
  public void setAppId (String appid) {
    this.appid_ = appid;
  }
}

In the example above, the field message will be initialized with the value of meta-data tag named my.message. You initialize all values with the @Metadata annotation using a single line of code:

MyData myData = new MyData ();
ManifestMetadata.get (context).initFromMetadata (myData);

This method will auto-detect the target type, and then assign the value. If the field is not assignable using the meta-data's value, then an exception will be thrown.

Reading from a Resource

In some cases, you will want to read the value from a resource (i.e., you use android:resource in the meta-data tag). You can use the @Metadata annotation to read resource values as well:

public class MyData {
  private String appid_;
  
  @MetadataProperty (name="my.message", fromResource=true)
  public String message;

  @MetadataMethod (name="appid", fromResource=true)
  public void setAppId (String appid) {
    this.appid_ = appid;
  }
}

Giving Resource Type Hints

There are some resources that have the same field type, such as integer and color. This makes it hard to auto-detect the resources type. We can therefore provide a hint as follows:

public class MyData {
  @MetadataProperty (name="my.message", fromResource=true, resourceType=ResourceType.Color)
  public int backgroundColor;
}

In the example above, the value for backgroundColor will be loaded from resources and interpreted as a color.