更新到 'com.android.tools.build:gradle:4.1.0' 后缺少 BuildConfig.VERSION_CODE
BuildConfig.VERSION_CODE is missing after update to 'com.android.tools.build:gradle:4.1.0'
我有一个多模块应用程序,出于某种原因 VERSION_CODE 生成的字段已从除根模块之外的所有模块中消失,我已经在另一个项目上对其进行了测试,它的行为相同。现在我只是降级到 4.0.1,但这只是一种解决方法。
我需要使用 gradle 工具 4.1.0
在所有模块中恢复 BuildConfig.VERSION_CODE
非常感谢任何帮助。
默认配置示例:
buildFeatures {
buildConfig = true
}
defaultConfig {
minSdkVersion global["androidMinSdkVersion"]
targetSdkVersion global["androidTargetSdkVersion"]
versionCode global["versionString"]
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath true
}
}
}
4.0.1上的BuildConfig代码来了
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "app";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "flavour";
public static final int VERSION_CODE = 107;
public static final String VERSION_NAME = "6.0.0";
// Field from build type: debug
public static final String AMQP_URL = "url";
// Field from build type: debug
public static final String API_VERSION_SUFFIX = "V_04_04_09";
// Field from build type: debug
public static final String BASE_URL = "url";
// Field from product flavor: flavour
public static final String BUILD_FLAVOR = "flavour";
// Field from build type: debug
public static final int DB_VERSION = 53;
}
这里是 4.1.0
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "app";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "flavour";
public static final String VERSION_NAME = "6.0.0";
// Field from build type: debug
public static final String AMQP_URL = "url";
// Field from build type: debug
public static final String API_VERSION_SUFFIX = "V_04_04_09";
// Field from build type: debug
public static final String BASE_URL = "url";
// Field from product flavor: flavour
public static final String BUILD_FLAVOR = "flavour";
// Field from build type: debug
public static final int DB_VERSION = 53;
}
I have a multi modular app and for some reason VERSION_CODE generated field has disappeared from all modules except the root one
这似乎与 this reported issue. In that case, it was VERSION_NAME
that was missing from library modules. See this specific comment 的基本原理有关。 显然,他们修复了 VERSION_NAME
但没有修复 VERSION_CODE
。 当我测试这个时,我没有得到 VERSION_NAME
或 VERSION_CODE
,所以我无法解释您的模块如何获得 VERSION_NAME
.
您应该可以切换到 using buildConfigField
以提供您的版本代码:
buildConfigField "long", "VERSION_CODE", global["versionString"]
无法找到干净的解决方案,因此添加了解决方法:
buildConfigField "int", "VERSION_CODE1", String.valueOf(global["versionString"])
你可以使用这里的其他答案作为解决方法,但这个答案只是为了通知你来自处理这个错误的人的官方回复。这不会被修复,因为这是预期的行为。
Google 工程师是这样说的:
Status: Won't Fix (Intended Behavior) Hi all,
Yes, we should have deprecated this first. Since this happened
(several months ago), we have put in place better policies for
deprecation/removal of properties. This happened kind of late for 4.1
though, and this particular issue fell through the cracks so we did
not get a chance to decide whether to revert this change (and we even
missed putting this change in the release notes at first). We
apologize for this.
Some of the motivation behind this were to reduce/remove the confusion
around 2 points:
Users accessing the BuildConfig fields but not setting them in the
DSL. This would definitively be broken and not do what one would
expect (ie these are static values, and won't magically reflect the
version of the app the library is embedded in) The library
AndroidManifest.xml file contains the values set in the DSL, but these
values are completely ignored by the consuming app(s) during manifest
merging (they are also overridden) If you want to keep injecting the
values from a common place into all your library project's BuildConfig
fields, you could continue to do this manually with your own custom
fields. These fields will only exist if you set them via the DSL
(therefore preventing problem #1 above), and will not impact the
library manifest (problem #2 above).
However, if you have a large setup with many library projects, this
pattern will generate a lot of duplicates. It would make more sense to
have a single module that generates a similar class, and have all your
library projects (or at least the ones that need the info) depend on
it.
我们也有一个 multi-module 设置,我们能够通过将以下行添加到每个模块的 build.gradle
来以最小的更改解决问题
buildConfigField 'int', 'VERSION_CODE', "${rootProject.appVersionCode}"
buildConfigField 'String', 'VERSION_NAME', "\"${rootProject.appVersionName}\""
将 buildConfigField
添加到模块 build.gradle 文件中的 buildTypes
buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
例子,
buildTypes {
debug{
buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
//..
}
release {
buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
//...
}
}
defaultConfig.versionCode
和defaultConfig.versionName
的值是
defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
//..
}
在你的模块中 build.gradle
您可以通过在模块 build.gradle 中编写 buildConfigField
方法在 BuildConfig
中创建字段。该方法第一个参数是创建字段类型,第二个参数是字段名,第三个参数是值。
Android Studio 4.1
您可以通过包名+BuildConfig+变量访问变量。
示例:com.heregoesyourpackagename.BuildConfig.VERSION_CODE
这将 return 放置在 Build gradle
中的值
我有一个多模块应用程序,出于某种原因 VERSION_CODE 生成的字段已从除根模块之外的所有模块中消失,我已经在另一个项目上对其进行了测试,它的行为相同。现在我只是降级到 4.0.1,但这只是一种解决方法。
我需要使用 gradle 工具 4.1.0
在所有模块中恢复 BuildConfig.VERSION_CODE非常感谢任何帮助。
默认配置示例:
buildFeatures {
buildConfig = true
}
defaultConfig {
minSdkVersion global["androidMinSdkVersion"]
targetSdkVersion global["androidTargetSdkVersion"]
versionCode global["versionString"]
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath true
}
}
}
4.0.1上的BuildConfig代码来了
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "app";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "flavour";
public static final int VERSION_CODE = 107;
public static final String VERSION_NAME = "6.0.0";
// Field from build type: debug
public static final String AMQP_URL = "url";
// Field from build type: debug
public static final String API_VERSION_SUFFIX = "V_04_04_09";
// Field from build type: debug
public static final String BASE_URL = "url";
// Field from product flavor: flavour
public static final String BUILD_FLAVOR = "flavour";
// Field from build type: debug
public static final int DB_VERSION = 53;
}
这里是 4.1.0
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "app";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "flavour";
public static final String VERSION_NAME = "6.0.0";
// Field from build type: debug
public static final String AMQP_URL = "url";
// Field from build type: debug
public static final String API_VERSION_SUFFIX = "V_04_04_09";
// Field from build type: debug
public static final String BASE_URL = "url";
// Field from product flavor: flavour
public static final String BUILD_FLAVOR = "flavour";
// Field from build type: debug
public static final int DB_VERSION = 53;
}
I have a multi modular app and for some reason VERSION_CODE generated field has disappeared from all modules except the root one
这似乎与 this reported issue. In that case, it was VERSION_NAME
that was missing from library modules. See this specific comment 的基本原理有关。 显然,他们修复了 当我测试这个时,我没有得到 VERSION_NAME
但没有修复 VERSION_CODE
。VERSION_NAME
或 VERSION_CODE
,所以我无法解释您的模块如何获得 VERSION_NAME
.
您应该可以切换到 using buildConfigField
以提供您的版本代码:
buildConfigField "long", "VERSION_CODE", global["versionString"]
无法找到干净的解决方案,因此添加了解决方法:
buildConfigField "int", "VERSION_CODE1", String.valueOf(global["versionString"])
你可以使用这里的其他答案作为解决方法,但这个答案只是为了通知你来自处理这个错误的人的官方回复。这不会被修复,因为这是预期的行为。
Google 工程师是这样说的:
Status: Won't Fix (Intended Behavior) Hi all,
Yes, we should have deprecated this first. Since this happened (several months ago), we have put in place better policies for deprecation/removal of properties. This happened kind of late for 4.1 though, and this particular issue fell through the cracks so we did not get a chance to decide whether to revert this change (and we even missed putting this change in the release notes at first). We apologize for this.
Some of the motivation behind this were to reduce/remove the confusion around 2 points:
Users accessing the BuildConfig fields but not setting them in the DSL. This would definitively be broken and not do what one would expect (ie these are static values, and won't magically reflect the version of the app the library is embedded in) The library AndroidManifest.xml file contains the values set in the DSL, but these values are completely ignored by the consuming app(s) during manifest merging (they are also overridden) If you want to keep injecting the values from a common place into all your library project's BuildConfig fields, you could continue to do this manually with your own custom fields. These fields will only exist if you set them via the DSL (therefore preventing problem #1 above), and will not impact the library manifest (problem #2 above).
However, if you have a large setup with many library projects, this pattern will generate a lot of duplicates. It would make more sense to have a single module that generates a similar class, and have all your library projects (or at least the ones that need the info) depend on it.
我们也有一个 multi-module 设置,我们能够通过将以下行添加到每个模块的 build.gradle
buildConfigField 'int', 'VERSION_CODE', "${rootProject.appVersionCode}"
buildConfigField 'String', 'VERSION_NAME', "\"${rootProject.appVersionName}\""
将 buildConfigField
添加到模块 build.gradle 文件中的 buildTypes
buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
例子,
buildTypes {
debug{
buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
//..
}
release {
buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
//...
}
}
defaultConfig.versionCode
和defaultConfig.versionName
的值是
defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
//..
}
在你的模块中 build.gradle
您可以通过在模块 build.gradle 中编写 buildConfigField
方法在 BuildConfig
中创建字段。该方法第一个参数是创建字段类型,第二个参数是字段名,第三个参数是值。
Android Studio 4.1
您可以通过包名+BuildConfig+变量访问变量。
示例:com.heregoesyourpackagename.BuildConfig.VERSION_CODE
这将 return 放置在 Build gradle
中的值