2

I'm creating a main android project wich uses a class library with some utility classes. This class library will be accessed by other projects, so there will be some configuration information that needs to be passed from the project to the class library. This information will be the same for the whole project, so I`d like to pass it only once, instead of passing as a parameter each time i call a function.

How can i pass this global information to the class library?

This question (Android main project with library project - how to pass setting between projects) suggests creating a setter method to pass the information, but that requires an instance of the library active all the time. I thought about creating a static property, but I heard somewhere that in some cases (when the app is swapped to another app and then swapped back), the classes could be recreated and the static values would be lost.

Community
  • 1
  • 1
Marlon
  • 1,719
  • 3
  • 20
  • 42
  • I remember reading once that you could inject variables into the app from Gradle. If you just have configuration parameters, then using the build tool would be better than hard-coded into the app – OneCricketeer Oct 12 '16 at 12:55

1 Answers1

2

You could use the manifest file, when the sdk need a api key this is stored in this place in general so, you can use the same idea.

In your manifest:

<meta-data android:name="my_test_metagadata" android:value="testValue" />

In your java file:

ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("my_test_metagadata");
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84