android自定义properties,如何配置额外的属性加载android应用程序初始化?(How to configure extra properties to load in a andro...

如何配置额外的属性加载android应用程序初始化?(How to configure extra properties to load in a android app initialization?)

我想设置一个变量SERVER_URL ,它将在环境生产 , 测试 , 开发之间切换。

我想做的事:

@Override

protected void onCreate(Bundle bundle) {

R.urls.SERVER_URL; // is it a valid approach using resources?

}

有没有办法改变环境(dev,prod,test)而不用更改代码?

实施此行为的最佳方法是什么?

有没有办法在playstore中配置我的变量(SERVER_URL),还是只能在代码中实现?

I want to setup a variable SERVER_URL and it'll be switched between environments production, test, development.

What I want to do:

@Override

protected void onCreate(Bundle bundle) {

R.urls.SERVER_URL; // is it a valid approach using resources?

}

Is there a way to switch environments(dev, prod, test) without change the code?

What's the best approach to implement this behavior?

Is there a way to configure it in the playstore my variable(SERVER_URL) or must I implement only in code?

原文:https://stackoverflow.com/questions/50256035

更新时间:2020-01-07 09:12

最满意答案

有两种方法可以做到这一点:

1 /按string资源喜欢你想要的

添加名为secret_keys.xml的资源文件或任何名称以将其与其他资源文件分开。 把你的钥匙,API端点作为普通的字符串资源,如果你不想搞乱本地化,记得加上translatable="false" 。

将该文件放在app/debug/res/values/ 。 创建一个新的debug文件夹,如果它不存在。 对staging或release做同样的事情时,Android会自动使用与构建类型同名的文件夹。

2 /通过properties文件

创建3个.properties文件并将你的密钥放在里面:

2070384ccbfcf7d6dd14f45578288dab.png

HOST="http://api.blablabla.com"

CLIENT_ID="hahaha"

CLIENT_SECRET="hehehe"

将它绑定到您的应用程序build.gradle BuildConfig变量,对其他生成类型执行相同操作:

def getPropertiesFile = { path ->

Properties properties = new Properties()

properties.load(new FileInputStream(file(path)))

return properties

}

android {

...

buildTypes {

debug {

...

getPropertiesFile('./config/development.properties').each { p ->

buildConfigField 'String', p.key, p.value

}

}

...

}

}

在你的应用程序中调用BuildConfig.HOST来获取你想要的字符串

UPDATE

忽略.gitignore这些配置文件:

app/config

secret_keys.xml

There are 2 ways you can do it:

1/ By string resource like you want

Add a resource file called secret_keys.xml or whatever name to separate it from other resources file. Put your keys, api endpoints here as the normal string resource, remember to add translatable="false" if you don't want to mess with localization.

Place that file in app/debug/res/values/. Create a new debug folder if it doesn't exist. Do the same for staging or release, Android will automatically use the folder with the same name as the build type.

2/ By properties files

Create 3 .properties files and put your keys inside:

2070384ccbfcf7d6dd14f45578288dab.png

HOST="http://api.blablabla.com"

CLIENT_ID="hahaha"

CLIENT_SECRET="hehehe"

Bind it to BuildConfig variable in your app build.gradle, do the same for other build types:

def getPropertiesFile = { path ->

Properties properties = new Properties()

properties.load(new FileInputStream(file(path)))

return properties

}

android {

...

buildTypes {

debug {

...

getPropertiesFile('./config/development.properties').each { p ->

buildConfigField 'String', p.key, p.value

}

}

...

}

}

In your app just call BuildConfig.HOST to get the string you want

UPDATE

Ignore these config files in .gitignore:

app/config

secret_keys.xml

2018-05-10

相关问答

一种方法是获取Android的源代码,并像Octavian写的那样编写自己的Android自定义ROM。 另一种选择是编写一个自定义的家庭应用程序,该应用程序并不是真正的家庭应用程序(但会取代默认的家庭应用程序意图)。 请参阅SlideScreen作为非传统“家庭”应用程序的示例。 One method is to get the source of Android and write your own custom ROM of Android as Octavian wrote. Anothe

...

在你的Config.groovy你可以做一个基本的技巧 Properties legacyProps = // whatever you need to do to load the legacy properties

for(String propName in legacyProps.stringPropertyNames()) {

setProperty(propName, legacyProps.getProperty(propName))

}

这适用于不包含任何点的属性名称。 包含

...

继续我回答我自己的深奥问题的传统,似乎一个可行的替代方案是在引擎的环境/ * .rb中包含一个保护条款和类似这样的初始化器: if defined? CuteEngine::Application

CuteEngine::Application.configure do

config.whatever = something

end

end

这解决了以相对较低的成本获得两个Rails :: Application对象的问题。 对此不是很满意,但我会活下去。 Continuing

...

在修改清单文件之后弄明白了。 添加了意图过滤器“category.PREFERENCE”并在服务标记内移动了BIND_WALLPAPER权限。 这是新的清单文件:

package="com.android.RrD"

android:versionCode="1"

android:versionName="1.0">

...

最简单的方法是在调用fs.readFileSync()之前使用app.listen() 。 例如: var lang = fs.readFileSync('lang.json');

app.get('/lang', function(req, res) {

res.setHeader('Content-Type', 'application/json');

res.send(lang);

});

// ...

请注意,如果lang.json的内容在节点进程的生命周期内在磁盘上发生更改

...

有两种方法可以做到这一点: 1 /按string资源喜欢你想要的 添加名为secret_keys.xml的资源文件或任何名称以将其与其他资源文件分开。 把你的钥匙,API端点作为普通的字符串资源,如果你不想搞乱本地化,记得加上translatable="false" 。 将该文件放在app/debug/res/values/ 。 创建一个新的debug文件夹,如果它不存在。 对staging或release做同样的事情时,Android会自动使用与构建类型同名的文件夹。 2 /通过properti

...

您可以通过禁用“ 即时运行”功能来解决此问题。 Instant Run需要添加一些文件才能工作,所以它往往会添加我们的apk大小。 阅读有关Instant Run的更多信息。 You could solve it by disabling Instant Run feature. Instant Run need to add some files to work, so it tends to add our apk size. Read More about Instant Run.

你使用过Android NDK吗? 因为我认为OpenCV是本机C / C ++代码所以您需要NDK才能将库容纳到您的项目中。 I finally found the solution to my problem. I just had to put the .so files under src/main/jniLibs and they are added to the apk when bulding. Easier than expected :)

首先,当我使用它时,npm版本在android上有点小错误。 尝试删除它并从Git添加它。 cordova plugin add https://github.com/j3k0/cordova-plugin-purchase.git --variable BILLING_KEY="MIIB...AQAB"

其次,看起来你可能正在使用一些较旧的语法。 这个插件的doco真的没有很好的版本控制。 整个网络都有不同版本的doco。 我认为这是最新版本。 这是我的初始化代码。 看看它是否也适合你。

...

另外,我想问你我的项目结构是否正确。 不,这实际上是导致你的问题。 即使src/main/config在Eclipse中被标记为项目目录,maven也不会以任何特殊方式处理它,特别是它不会在CLASSPATH上包含该文件夹。 查看您构建的WAR文件,它甚至不包含这些文件! 只需将所有*.properties文件移动到src/main/resources您的应用程序就可以构建并运行正常。 Also, i would like to ask you if my project structure is

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值