Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/onehilltech/android-metadata

A library for reading meta-data from AndroidManifest.xml
https://github.com/onehilltech/android-metadata

android android-metadata androidmanifest annotations java metadata

Last synced: about 2 months ago
JSON representation

A library for reading meta-data from AndroidManifest.xml

Awesome Lists containing this project

README

        

android-metadata
==================

[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-android--metadata-green.svg?style=true)](https://android-arsenal.com/details/1/4045)
[![Download](https://jitpack.io/v/onehilltech/android-metadata.svg)](https://jitpack.io/#onehilltech/android-metadata)
[![Build Status](https://travis-ci.org/onehilltech/android-metadata.svg)](https://travis-ci.org/onehilltech/android-metadata)
[![codecov.io](http://codecov.io/github/onehilltech/android-metadata/coverage.svg?branch=master)](http://codecov.io/github/onehilltech/android-metadata?branch=master)

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.

```java
ManifestMetadata metadata = ManifestMetadata.get (context);

//
String value = metadata.getValue ("appid");
```

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

```java
ManifestMetadata metadata = ManifestMetadata.get (context);

//
Integer connTimeout = metadata.getValue ("conn.timeout", Integer.class);
```

You can even directly load a resource from the metadata:

```java
ManifestMetadata metadata = ManifestMetadata.get (context);

//
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:

```java
ManifestMetadata metadata = ManifestMetadata.get (context);

//
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:

```java
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:

```java
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:

```java
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:

```java
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.