根据newrelic文档,我必须将其API密钥保存在名为newrelic的文件中。属性,并在创建主类时使用它。如何避免将此密钥存储两次(newrelic.properties和Constants文件)?如何获取存储在newrelic中的api。属性文件?
使用New Relic API密钥的位置:
创建时的主要活动:
NewRelic.withApplicationToken("GENERATED_TOKEN").start(this.getApplication());
存档newrelic。项目名称/应用程序路径中的属性
com.newrelic.application_token=GENERATED_TOKEN
创建实用程序类AssetPropertyReader:
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
我找到的解决方案是基于这些主题:访问属性文件位于应用程序文件夹中的src文件在Android
Android-如何获得风味的应用程序
https://discuss.newrelic.com/t/multiple-flavors/12374/15
1/应用程序/构建。格拉德尔
在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/应用程序/构建。格拉德尔
productFlavors {
production {
dimension = "datatype"
buildConfigField "String", "NEW_RELIC_API_KEY", "\"${props.getProperty("com.newrelic.application_token")}\""
}
}
3/应用中的任何类。在我的情况下是主活动
if(isProductionFlavor()) {
NewRelic
.withApplicationToken(BuildConfig.NEW_RELIC_API_KEY)
.start(this.getApplication())
}