在我的Android Studio
项目中有两个build configuration
,其中包含一些buildConfigField
:
buildTypes {
def SERVER_URL = "SERVER_URL"
def APP_VERSION = "APP_VERSION"
debug {
buildConfigField "String", SERVER_URL, "http://dev.myserver.com"
buildConfigField "String", APP_VERSION, "0.0.1"
}
release {
buildConfigField "String", SERVER_URL, "https://myserver.com"
buildConfigField "String", APP_VERSION, "0.0.1"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
我得到的错误如下:
/path/to/generated/BuildConfig.java
Error:(14, 47) error: ';' expected
Error:(15, 47) error: ';' expected
生成的BuildCofig.java如下:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.mycuteoffice.mcoapp";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Fields from build type: debug
public static final String APP_VERSION = 0.0.1;
public static final String SERVER_URL = http://dev.mycuteoffice.com;
}
我认为APP_VERSION
和SERVER_URL
没有正确生成,因为它们没有引号的字符串类型。
我不确定为什么会以这种方式生成它。请让我知道如何解决这个问题。
答案 0 :(得分:185)
字符串类型构建配置字段应该声明如下:
buildConfigField "String", "SERVER_URL", "\"http://dev.myserver.com\""
引号中的字段名称,另外还有转义引号中的字段值。
答案 1 :(得分:75)
为什么每个人都为逃避双引号而如此生气?看起来很难看!这是Groovy,伙计们,你可以混合单引号和双引号:
buildConfigField "String", 'SERVER_URL', '"http://dev.myserver.com"'
buildConfigField "String", 'APP_VERSION', '"0.0.1"'
答案 2 :(得分:21)
如果通过"解决问题"你的意思是没有必要加倍引用文字,我没有遇到任何东西,因为它似乎按设计工作。
我一直在尝试将文字移到" gradle.properties "作为一种解决方法,将潜在的多条难看的线条变成一条丑陋的线条。
像这样:
buildTypes {
def SERVER_URL = "SERVER_URL"
def APP_VERSION = "APP_VERSION"
def CONFIG = { k -> "\"${project.properties.get(k)}\"" }
debug {
buildConfigField "String", SERVER_URL, CONFIG("debug.server.url")
buildConfigField "String", APP_VERSION, CONFIG("version")
}
release {
buildConfigField "String", SERVER_URL, CONFIG("release.server.url")
buildConfigField "String", APP_VERSION, CONFIG("version")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
<强> gradle.properties 强>
version=0.1.1
...
debug.server.url=http://dev.myserver.com
...
release.server.url=http://myserver.com
...
进一步的想法:
def CONFIG = { b,k -> "\"${project.properties.get(b+'.'+k)}\"" }
def CONFIG_DEBUG = { k -> CONFIG('debug',k) }
def CONFIG_RELEASE = { k -> CONFIG('release',k) }
def CONFIG = { b,k -> "\"${project.properties.get(b+'.'+k)}\"" }
def CONFIG_INT = { b,k -> "${project.properties.get(b+'.'+k)}" }
...
答案 3 :(得分:9)
转义字符串引号:
buildConfigField "String", 'SERVER_URL', "\"http://dev.myserver.com\""
buildConfigField "String", 'APP_VERSION', "\"0.0.1\""
答案 4 :(得分:8)
我也很困惑。但是有一种感觉 - &#34; String&#34;定义字段的类型,而字段值不会自动引用,以便我们在这里使用表达式:
buildConfigField "String", "TEST", "new Integer(10).toString()"
否则,它是不可能的。
答案 5 :(得分:4)
答案 6 :(得分:1)
在应用程序build.gradle中
text = open("test.txt",'r').read()
results = re.findall(r'(bbduk_trimmed.*.fastq)\nSeq_132582_1: ATCCGAATTAGTGTAGGGGTTAACATAACTCT: \n(\d)\nSeq_483974_49238: TCCGAATTAGTGTAGGGGTTAACATAACTC: \n(\d*)',text)
然后在BuildConfig中
pd.DataFrame(results)
答案 7 :(得分:1)
我们应该对在Gradle属性或其他地方定义的Gradle常量进行替换:
@model List<MySite.Models.CustomerAddress>
<div class="input-group mb-3">
<button type="button" class="btn btn-primary" id="add-customer-address-submit" data-parent-id="Model.CustomerId">Add</button>
</div>
<table id="customerAddressList" class="child-grid">
<thead>
<tr>
...
</tr>
</thead>
@foreach (var address in Model)
{
<tr>
<td suppress-configuration-row-click data-configuration-id="@address.CustomerAddressId">
<input type="checkbox" id="address-selected" />
</td>
<td data-address="@address.Address">@address.Address</td>
<td data-city="@address.City">@address.City</td>
<td data-state="@address.State">@address.State</td>
<td data-zip="@address.Zip">@address.Zip</td>
</tr>
}
</table>
namespace MySite.Pages.Shared
{
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using UnlockKeyManagementWeb.DataAccess;
using UnlockKeyManagementWeb.Models;
public class AddressListPartialModel : PageModel
{
private readonly ICustomerAddressDataAccess customerAddressDataAccess;
public AddressListPartialModel (ICustomerAddressDataAccess customerAddressDataAccess)
{
this.customerAddressDataAccess = customerAddressDataAccess;
}
public List<CustomerAddress> CustomerAddresses{ get; set; }
public PartialViewResult OnGetUnlockKeyConfigurationsListPartial(int unlockKeyId)
{
this.CustomerAddresses = this.customerAddressDataAccess.GetCustomerAddresses(customerId);
return this.Partial("_AddressListPartial", this.CustomerAddresses);
}
}
}
在我们的buildConfigField "String", "CONSTANT_NAME", "\"${CONSTANT_VALUE}\""
或其他地方定义了CONSTANT_VALUE
的地方:
gradle.properties
从我们的环境中获取常量时,其应用方式相同:
CONSTANT_VALUE=string_goes_here
投票率最高的解决方案在我们只需要手动添加字符串的情况下有效,该解决方案又向前迈了一步。
答案 8 :(得分:0)
只有\“my stuff
\”为我工作。我在{{1}}中有各种各样奇怪的角色。
答案 9 :(得分:0)
我需要 buildConfigField 和 manifestPaceholder 中的变量。 为了解决这个问题,我做
def appAuthScheme= "appauth.myscheme"
buildConfigField 'String', 'APP_AUTH_SCHEME',"\"$appAuthScheme\""
manifestPlaceholders = [lowerApplicationId : applicationId.toLowerCase(),
appAuthRedirectScheme : appAuthScheme]
BuildConfig.APP_AUTH_SCHEME 是一个字符串!