根据新文物docs,我必须将其API密钥保存在名为newrelic.properties
的文件中,并在主类的onCreate上使用它。如何避免两次存储此密钥(newrelic.properties和Constants文件)?如何获取存储在newrelic.properties文件中的api?
使用新的Relic API密钥的地方:
MainActivity onCreate:
NewRelic.withApplicationToken("GENERATED_TOKEN").start(this.getApplication());
在项目名称/应用路径中文件newrelic.properties
com.newrelic.application_token=GENERATED_TOKEN
答案 0 :(得分: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
答案 1 :(得分:0)
我发现的解决方案基于以下主题: 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())
}