BuildHelper.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova;
  18. /*
  19. * This is a utility class that allows us to get the BuildConfig variable, which is required
  20. * for the use of different providers. This is not guaranteed to work, and it's better for this
  21. * to be set in the build step in config.xml
  22. *
  23. */
  24. import android.app.Activity;
  25. import android.content.Context;
  26. import java.lang.reflect.Field;
  27. public class BuildHelper {
  28. private static String TAG="BuildHelper";
  29. /*
  30. * This needs to be implemented if you wish to use the Camera Plugin or other plugins
  31. * that read the Build Configuration.
  32. *
  33. * Thanks to Phil@Medtronic and Graham Borland for finding the answer and posting it to
  34. * StackOverflow. This is annoying as hell! However, this method does not work with
  35. * ProGuard, and you should use the config.xml to define the application_id
  36. *
  37. */
  38. public static Object getBuildConfigValue(Context ctx, String key)
  39. {
  40. try
  41. {
  42. Class<?> clazz = Class.forName(ctx.getPackageName() + ".BuildConfig");
  43. Field field = clazz.getField(key);
  44. return field.get(null);
  45. } catch (ClassNotFoundException e) {
  46. LOG.d(TAG, "Unable to get the BuildConfig, is this built with ANT?");
  47. e.printStackTrace();
  48. } catch (NoSuchFieldException e) {
  49. LOG.d(TAG, key + " is not a valid field. Check your build.gradle");
  50. } catch (IllegalAccessException e) {
  51. LOG.d(TAG, "Illegal Access Exception: Let's print a stack trace.");
  52. e.printStackTrace();
  53. }
  54. return null;
  55. }
  56. }