[英]How to create a two or three different build using Gradle build system, each build pointing to different server?
I have to create a two or three different build (Android build) using Gradle build system, each build pointing to different server and api key? 我必须使用Gradle构建系统创建两个或三个不同的构建(Android构建),每个构建都指向不同的服务器和api密钥? At present these server url and api key is hard coded in java file.
目前,这些服务器的url和api密钥已硬编码在java文件中。 I could build debug and release build properly, but need to generate different build with set of different server url and api key?
我可以构建调试并正确发布构建,但是需要使用一组不同的服务器URL和api密钥生成不同的构建吗?
Thanks in advance! 提前致谢!
(If I understand) I would use an external executable that prepare a batch file for each build. (如果我理解的话)我将使用一个外部可执行文件,该文件为每个版本准备一个批处理文件。
The .bat file calls gradle passing a variable (conaining the key) to it: .bat文件调用gradle将变量(包含键)传递给它:
gradle assemble -Pkey=%key%
Now, inside the gradle script you can get the value of the variable: 现在,在gradle脚本中,您可以获取变量的值:
def varKey = "$key"
and you can use it to sign the apk: 您可以使用它来签名apk:
signingConfigs {
release {
storeFile = file('../key.keystroke')
storePassword = varKey
keyAlias = "My key"
keyPassword = varKey
}
}
EDIT: Ok, the previous text is useful to sign the apk file with different keys. 编辑:好的,前面的文本对于用不同的密钥签名apk文件很有用。
To edit the server url using gradle, first you need to store the url and api key into a resource string and use from inside the java class instead of the hard coded strings: 要使用gradle编辑服务器url,首先需要将url和api键存储到资源字符串中,并从java类内部而不是硬编码字符串中使用:
<resources>
<string name="server_url">you.server.com</string>
<string name="api_key">your_api_key</string>
</resources>
now consider the .bat that calls gradle: 现在考虑调用gradle的.bat:
gradle assemble -Papi_key=%api_key% -Pserver_url=%server_url%
inside gradle, you get the variable content: 在gradle中,您将获得变量内容:
def varApiKey = "$api_key"
def varServerUrl = "$server_url"
Now you need to replace the strings in your resources. 现在,您需要替换资源中的字符串。 I never done it but (for example) you can look at:
我从未做过,但是(例如)您可以看一下:
How to replace strings resources with Android Gradle 如何使用Android Gradle替换字符串资源
I resolved my requirement by creating different build type and configuring different resource for that build type. 我通过创建不同的构建类型并为该构建类型配置不同的资源来解决了我的要求。 For example my build.xml look like below:
例如,我的build.xml如下所示:
buildTypes { buildTypes {
release {
signingConfig signingConfigs.release
runProguard true
proguardFile ('proguard-android-optimize-app.txt')
}
server1.initWith(buildTypes.debug)
server1 {
packageNameSuffix ".server1"
}
} }
sourceSets {
main {
manifest.srcFile 'app/AndroidManifest.xml'
java.srcDirs = ['app/src']
resources.srcDirs = ['app/src']
renderscript.srcDirs = ['app/src']
res.srcDirs = ['app/res']
assets.srcDirs = ['app/assets']
}
server1 {
res.srcDirs = ['app/res/server1']
}
instrumentTest.setRoot('tests')
}
Thanks for the support! 感谢您的支持!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.