Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android configuration in meta-data or in properties file?

I'm writing a library that is used by developers and I have a configuration items like apiKey, environment and others. I want to developer to set these values, my first thought was using Java Properties file that I load in the Application class, but I've seen Google Play and Google Map SDK ask the developers to add their apiKey in meta-data tag in Android Manifest. What is the recommended way?

like image 958
Jimmy Avatar asked Feb 13 '15 15:02

Jimmy


People also ask

What is metadata file in Android?

description: A name-value pair for an item of additional, arbitrary data that can be supplied to the parent component.

What is the main configuration file for Android applications?

The config file must be available at the "/sdcard/config. txt" of the android hardware.

What is Android config?

The Android Device Configuration Service collects information from Android devices, including: Device and account identifiers. Device attributes. Software and security software versions. Network connectivity and performance data.

What is meta service in Android?

Meta Data Services was an object-oriented repository technology that could be integrated with enterprise information systems or with applications that process metadata.


1 Answers

The recommended android way to let the user define values for your library from the application module is through meta-tags

it is as easy as using this code on the android-manifest

<meta-data android:name="com.yourproject.apikey" android:value="apikey"/>

and you can get these values from your library like this

try {
    ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
    Bundle bundle = applicationInfo.metaData;
    String apiKey = bundle.getString("com.yourproject.apikey");
} catch (NameNotFoundException e) {
    //Resolve error for not existing meta-tag, inform the developer about adding his api key
}

The main benefits of this approach are

1) That you can let the user define different values per device type e.g. phone, tablet or sdk-version (this may not be applicable for your case but it's a good point for sure)

2) You can add complex data as the value in your meta-tag, and you can use this if you want to add a string-array with all the value that you want to declare.

3) Since meta-tags are basically a bundle it is easier for you to

  • Read these values on your library code.

  • Have default values if the user has not declared the required meta-tags.

(I believe that parsing properties from assets requires more code without any great advantage)

4) You can also add configuration meta-tags for activities,providers and broadcastreceiver on your AndroidManifest.

5) Also I suppose that it is easier to request for the consumers of your library to add a few meta info on his Android Manifest, than adding a properties file on their assets folder.And i believe that this is the main reason Many known libraries use this approach like google-play-service, google-maps, crash reporting libraries and many libraries that provide user analytic.

Good Luck

like image 101
Manolis Proimakis Avatar answered Oct 22 '22 06:10

Manolis Proimakis