[英]Get gradle build type in product flavor
I need to create different app names depending on the product flavour used. 我需要根据使用的产品风格创建不同的应用程序名称。
While this was easy by simply setting a string resource, I can no longer do that because when the app is uploaded to hockeyapp the app name is set as '@string/app_name' instead of the value of app_name. 虽然通过简单地设置字符串资源很容易,但我不能再这样做,因为当应用程序上传到hockeyapp时,应用程序名称设置为“@ string / app_name”而不是app_name的值。
I have made some progress by setting the label in the manifest to be '${applicationName}' and setting the value with 通过将清单中的标签设置为“$ {applicationName}”并使用设置值,我取得了一些进展
manifestPlaceholders = [ applicationName : appName ];
in the product flavour block so that the value gets set at compile time. 在产品flavor块中,以便在编译时设置该值。
The problem comes when I try to append the build type to the application name. 当我尝试将构建类型附加到应用程序名称时出现问题。 I can't seem to find a way to know what build type is currently being used within the product flavour.
我似乎无法找到一种方法来了解当前在产品风格中使用的构建类型。
This is a stripped down version of the build for readability 这是用于可读性的构建的精简版本
android {
buildVersionName "1.0.0
buildTypes {
release {
... nothing special
}
uat {
signingConfig signingConfigs.debug
buildType = "uat"
applicationIdSuffix = "." + buildType
}
debug {
signingConfig signingConfigs.debug
buildType = "uat"
applicationIdSuffix = "." + buildType
}
}
productFlavors{
flavor1{
def appName = "app name " + buildType;
manifestPlaceholders = [ applicationName : appName ];
applicationId [id]
def clientIteration = [client iteration]
versionName buildVersionName + clientIteration
versionCode [version code]
}
flavor2{
... same as above with different app name
}
flavor3{
... same as above with different app name
}
}
}
This code works fine except the variable 'buildType' is always the last buildtype (in this case debug) which means the app name always has debug on the end. 此代码工作正常,但变量'buildType'始终是最后一个构建类型(在本例中为debug),这意味着应用程序名称总是在末尾进行调试。
Probably worth noting that I don't need to have anything appended on the end of the app name for releases. 可能值得注意的是,我不需要在应用程序名称的末尾附加任何附加内容。
I know I'm a bit late for the party but if you want different names based on the flavours, you should have something like this: 我知道我有点迟到了,但如果你想要根据口味不同的名字,你应该有这样的东西:
productFlavors{
flavour 1 {
applicationId "your_app_id"
resValue "string", "app_name", "Flavour 1 app name"
.......
}
flavour 2 {
applicationId "your_app_id"
resValue "string", "app_name", "Flavour 2 app name"
.......
}
}
and in your AndroidManifest.xml: 在AndroidManifest.xml中:
android:label="@string/app_name"
Hope this helps. 希望这可以帮助。
You can append the values like this 您可以附加这样的值
android {
productFlavors {
Foo {
applicationId "com.myexample.foo"
manifestPlaceholders = [ appName:"Foo"]
}
Bar {
applicationId "com.myexample.bar"
manifestPlaceholders = [ appName:"Bar"]
}
}
buildTypes {
release {
manifestPlaceholders = [ appNameSuffix:""]
}
debug {
manifestPlaceholders = [ appNameSuffix:".Debug"]
applicationIdSuffix ".debug"
}
}
}
and in the manifest 并在清单中
<application
android:label="${appName}${appNameSuffix}"
...
</application>
If you want to access different values based on build type you can do it like this 如果要根据构建类型访问不同的值,可以这样做
buildTypes {
debug{
buildConfigField "String", "Your_string_key", '"yourkeydebugvalue"'
buildConfigField "String", "SOCKET_URL", '"some text"'
buildConfigField "Boolean", "LOG", 'true'
}
release {
buildConfigField "String", "Your_string_key", '"yourkeyreleasevalue"'
buildConfigField "String", "SOCKET_URL", '"release text"'
buildConfigField "Boolean", "LOG", 'false'
}
}
And to access those values using build variants: 并使用构建变体访问这些值:
if(!BuildConfig.LOG)
// do something with the boolean value
Or 要么
view.setText(BuildConfig.yourkeyvalue);
This link http://inaka.net/blog/2014/12/22/create-separate-production-and-staging-builds-in-android/ may help you out. 这个链接http://inaka.net/blog/2014/12/22/create-separate-production-and-staging-builds-in-android/可能会帮到你。
If you have two productFlavors (Production and Staging, for instance) 如果您有两个productFlavors(例如,生产和暂存)
You should create two different resource folders: 您应该创建两个不同的资源文件夹:
project/app/src/production/res/values/strings.xml 项目/应用/ src目录/生产/ RES /价值/ strings.xml中
<resources>
<string name="root_url">http://production.service.com/api</string>
</resources>
project/app/src/staging/res/values/strings.xml 项目/应用/ src目录/分期/ RES /价值/ strings.xml中
<resources>
<string name="root_url">http://staging.service.com/api</string>
</resources>
You should add this following code inside the android {}: 您应该在android {}中添加以下代码:
productFlavors {
production {
applicationId "com.inaka.app.production"
}
staging {
applicationId "com.inaka.app.staging"
}
}
It's a good idea to have different icons for different productFlavors, just add the icon inside each different resource folder. 为不同的productFlavors设置不同的图标是个好主意,只需在每个不同的资源文件夹中添加图标即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.