How to change version code after AGP 7.0.0-alpha15
Asked Answered
E

3

8

Before AGP 7.0.0-alpha15 I could change version code of an app for example like that

android {
  defaultConfig {
    applicationVariants.all {
      if (buildType.name == "debug") {
        outputs.forEach { output ->
          (output as? com.android.build.gradle.internal.api.ApkVariantOutputImpl)?.versionCodeOverride = 1
        }
      }
    }
  }
}

applicationVariants seems missing after AGP 7.0.0-alpha15, how to change it?

PS: It seems ok in plain gradle, above is Kotlin

Edit

With answer from below I was able to override version code in build:

android {
  androidComponents.onVariants { appVariant ->
    if (appVariant.buildType == "release") {
      appVariant.outputs.forEach {
        it.versionCode.set(1)
      }
    }
  }
}
Efren answered 5/5, 2021 at 7:3 Comment(2)
ApplicationVariant still exists and has List<VariantOutput>.Evangelia
In plain gradle yes, but not in Kotlin DSLEfren
S
6

The Variant API is going to change to a lazily-evaluated model in AGP 7.0.0, and it seems like Alpha 15 has removed the old APIs for this now. Going forward, you will need to use the androidComponents DSL, which gives access to variants. I am not sure if you'll be able to rewrite the version code like this, however. Check out beforeVariants and onVariants for more info:

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)
  }
}
Sides answered 6/5, 2021 at 7:31 Comment(0)
E
1

In Java plugin code, one can set the version code & name alike this:

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 happens before onVariants.

Evangelia answered 10/3, 2022 at 7:4 Comment(0)
C
0
android {
    // ..
    splits {
        abi {
            enable true
            reset()
            include 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a'
        }
    }
    // ..
}

androidComponents {
    onVariants(selector().all()) { appVariant ->
        for (VariantOutput variantOutput : appVariant.getOutputs()) {
            def versionCodes = ['[armeabi]'       : 100000,
                                '[armeabi-v7a]'   : 200000,
                                '[x86]'           : 300000,
                                '[arm64-v8a]'     : 400000,
                                '[x86_64]'        : 500000]
            String abi = variantOutput.getFilters().identifier;
            Integer version = versionCodes.get(abi) + versionCode
            if (appVariant.getName().contains("Debug")) {
                version += 10000
            }
            variantOutput.getVersionCode().set(version);
        }
    }
}

dependencies {
    // ...
}
Carburize answered 20/10, 2022 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.