AGP 7.0.0-alpha15之后如何修改版本号
How to change version code after AGP 7.0.0-alpha15
在 AGP 7.0.0-alpha15 之前,我可以像这样更改应用程序的版本代码
android {
defaultConfig {
applicationVariants.all {
if (buildType.name == "debug") {
outputs.forEach { output ->
(output as? com.android.build.gradle.internal.api.ApkVariantOutputImpl)?.versionCodeOverride = 1
}
}
}
}
}
applicationVariants
AGP 7.0.0-alpha15 后好像不见了,怎么改?
PS: plain gradle 好像还可以,上面是Kotlin
编辑
根据 的回答,我能够覆盖构建中的版本代码:
android {
androidComponents.onVariants { appVariant ->
if (appVariant.buildType == "release") {
appVariant.outputs.forEach {
it.versionCode.set(1)
}
}
}
}
变体 API 将在 AGP 7.0.0 中更改为惰性评估模型,Alpha 15 似乎已经为此删除了旧的 API。展望未来,您将需要使用 androidComponents
DSL,它可以访问变体。但是,我不确定您是否能够像这样重写版本代码。查看 beforeVariants
和 onVariants
了解更多信息:
android {
androidComponents.beforeVariants { variantBuilder ->
// Callback before variants are built. Can be modified, but doesn't allow access to outputs
}
androidComponents.onVariants { variant ->
// Callback after variants are built. Apparently it's read-only access at this point, but outputs are available here
println(variant.outputs)
}
}
在Java插件代码中,可以这样设置版本号和名称:
class SomePlugin implements Plugin<Project> {
@Override
@SuppressWarnings("UnstableApiUsage")
public void apply(@NotNull Project project) {
ApplicationAndroidComponentsExtension androidComponents = project.getExtensions()
.getByType(ApplicationAndroidComponentsExtension.class);
androidComponents.finalizeDsl(extension -> {
for (AndroidSourceSet sourceSet : extension.getSourceSets()) {
System.out.println(sourceSet.getName());
}
});
int versionCode = 1;
String versionName = "1.0.0";
VariantSelector selector = androidComponents.selector().all();
androidComponents.onVariants(selector, variant -> {
for (VariantOutput variantOutput : variant.getOutputs()) {
variantOutput.getVersionName().set( versionName );
variantOutput.getVersionCode().set( versionCode );
System.out.println(">>>> " +
variant.getName() + " " +
variantOutput.getVersionCode().get() + " / " +
variantOutput.getVersionName().get());
}
});
}
}
finalizeDsl
发生在 onVariants
.
之前
在 AGP 7.0.0-alpha15 之前,我可以像这样更改应用程序的版本代码
android {
defaultConfig {
applicationVariants.all {
if (buildType.name == "debug") {
outputs.forEach { output ->
(output as? com.android.build.gradle.internal.api.ApkVariantOutputImpl)?.versionCodeOverride = 1
}
}
}
}
}
applicationVariants
AGP 7.0.0-alpha15 后好像不见了,怎么改?
PS: plain gradle 好像还可以,上面是Kotlin
编辑
根据
android {
androidComponents.onVariants { appVariant ->
if (appVariant.buildType == "release") {
appVariant.outputs.forEach {
it.versionCode.set(1)
}
}
}
}
变体 API 将在 AGP 7.0.0 中更改为惰性评估模型,Alpha 15 似乎已经为此删除了旧的 API。展望未来,您将需要使用 androidComponents
DSL,它可以访问变体。但是,我不确定您是否能够像这样重写版本代码。查看 beforeVariants
和 onVariants
了解更多信息:
android {
androidComponents.beforeVariants { variantBuilder ->
// Callback before variants are built. Can be modified, but doesn't allow access to outputs
}
androidComponents.onVariants { variant ->
// Callback after variants are built. Apparently it's read-only access at this point, but outputs are available here
println(variant.outputs)
}
}
在Java插件代码中,可以这样设置版本号和名称:
class SomePlugin implements Plugin<Project> {
@Override
@SuppressWarnings("UnstableApiUsage")
public void apply(@NotNull Project project) {
ApplicationAndroidComponentsExtension androidComponents = project.getExtensions()
.getByType(ApplicationAndroidComponentsExtension.class);
androidComponents.finalizeDsl(extension -> {
for (AndroidSourceSet sourceSet : extension.getSourceSets()) {
System.out.println(sourceSet.getName());
}
});
int versionCode = 1;
String versionName = "1.0.0";
VariantSelector selector = androidComponents.selector().all();
androidComponents.onVariants(selector, variant -> {
for (VariantOutput variantOutput : variant.getOutputs()) {
variantOutput.getVersionName().set( versionName );
variantOutput.getVersionCode().set( versionCode );
System.out.println(">>>> " +
variant.getName() + " " +
variantOutput.getVersionCode().get() + " / " +
variantOutput.getVersionName().get());
}
});
}
}
finalizeDsl
发生在 onVariants
.