【问题标题】:Proper way to save and use New Relic API Key保存和使用 New Relic API 密钥的正确方法
【发布时间】:2019-03-21 02:49:41
【问题描述】:

根据 New Relic docs,我必须将它的 API 密钥保存在一个名为 newrelic.properties 的文件中,并在主类的 onCreate 中使用它。如何避免将此密钥存储两次(newrelic.properties 和常量文件)?如何获取存储在 newrelic.properties 文件中的 api?

New Relic API KEY使用场所:

MainActivity onCreate:

NewRelic.withApplicationToken("GENERATED_TOKEN").start(this.getApplication());

在项目名称/应用路径中的文件 newrelic.properties

com.newrelic.application_token=GENERATED_TOKEN

【问题讨论】:

    标签: android newrelic api-key


    【解决方案1】:

    创建一个实用程序类 AssetsPropertyReader:

    public class AssetsPropertyReader {
       private Context context;
       private Properties properties;
    
       public AssetsPropertyReader(Context context) {
              this.context = context;
              /**
               * Constructs a new Properties object.
               */
              properties = new Properties();
       }
    
       public Properties getProperties(String FileName) {
    
              try {
                     /**
                      * getAssets() Return an AssetManager instance for your
                      * application's package. AssetManager Provides access to an
                      * application's raw asset files;
                      */
                     AssetManager assetManager = context.getAssets();
                     /**
                      * Open an asset using ACCESS_STREAMING mode. This
                      */
                     InputStream inputStream = assetManager.open(FileName);
                     /**
                      * Loads properties from the specified InputStream,
                      */
                     properties.load(inputStream);
    
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     Log.e("AssetsPropertyReader",e.toString());
              }
              return properties;
    
       }
    
    }
    

    然后像这样使用它:

    AssetsPropertyReader assetsPropertyReader = new AssetsPropertyReader(context);
    Properties p = assetsPropertyReader.getProperties("MyStringsFile.properties"); //here 'MyStringsFile.properties' is the name of the file
    
    
    Toast.makeText(context, p.getProperty("MyBlog"), 1).show(); //'MyBlog' is the key we want value for
    

    取自这里:http://khurramitdeveloper.blogspot.com/2013/07/properties-file-in-android.html

    【讨论】:

    • 这个文件不在 /assets 文件夹中,而是在 projectname/app 路径中,所以只用上面的代码是找不到的,但我会尝试类似的方法
    【解决方案2】:

    我找到的解决方案基于以下主题: access Properties file located in app folder in src file in android

    Android - How To Get Flavor's ApplicationId

    https://discuss.newrelic.com/t/multiple-flavors/12374/15

    1/app/build.gradle

    在 android { ... } 插件之前,添加:

    // Check if new relic file exists
    Properties props = new Properties()
    try {
      props.load(file('newrelic.properties').newDataInputStream())
    } catch (Exception ex) {
      throw new GradleException("Missing newrelic.properties file on /app path.")
    }
    

    2/app/build.gradle

     productFlavors {
        production {
          dimension = "datatype"
          buildConfigField "String", "NEW_RELIC_API_KEY", "\"${props.getProperty("com.newrelic.application_token")}\""
        }
    }
    

    3/您应用中的任何类。在我的情况下是 MainActivity

    if(isProductionFlavor()) {
        NewRelic
         .withApplicationToken(BuildConfig.NEW_RELIC_API_KEY)
         .start(this.getApplication())
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 2018-11-06
      • 2020-04-11
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多