1

It's a highly discussed problem on SO, but I couldn't find an up-to-date solution after hours of searching. Sadly I can't add comments anywhere.

I'm setting the versionName and versionCode in my build.gradle using the gradle-android-git-version Plugin. I now want to alter the name of the build outputfile from my-sdk-release.aar to my-sdk-release-versionName.aar or similar.

However, any solutions presented did not work, because properties were not found. The documentation on that matter seems to be outdated as well, OutputFile is long deprecated.

Going with the newest one I could find:

afterEvaluate {
    android.libraryVariants.all { variant ->
        variant.variantData.outputFactory.apkDataList.each { apkData ->
            if (apkData.outputFileName.endsWith('.aar')) {
                apkData.outputFileName = "${project.name}-${buildType.name}-anything-you-want.aar"
            }
        }
    }
}

which is giving me the error No such property: outputFactory for class: com.android.build.gradle.internal.variant.LibraryVariantData on Gradle 7.3.3

My build.gradle goes as follows:

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
    id 'com.gladed.androidgitversion' version '0.4.14'
}

android {
    compileSdk 33

    defaultConfig {
        minSdk 24
        targetSdk 33
        versionName androidGitVersion.name()
        versionCode androidGitVersion.code()

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    kotlinOptions {
        freeCompilerArgs += [
                '-opt-in=kotlin.RequiresOptIn',
                '-Xexplicit-api=strict'
        ]
    }

    afterEvaluate {
        android.libraryVariants.all { variant ->
            variant.variantData.outputFactory.apkDataList.each { apkData ->
                if (apkData.outputFileName.endsWith('.aar')) {
                    apkData.outputFileName = "${project.name}-${buildType.name}-anything-you-want.aar"
                }
            }
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'no.nordicsemi.android:ble-ktx:2.5.1'
    implementation 'no.nordicsemi.android.support.v18:scanner:1.5.1'
    implementation 'commons-codec:commons-codec:1.13'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2'
    testImplementation 'junit:junit:4.13.2'
}

What did I do wrong?

1 Answers1

0

apkDataList, as name implies it is likely to use building apk.

android {
    compileSdk 33

    defaultConfig {
        minSdk 24
        targetSdk 33
        versionName androidGitVersion.name()
        versionCode androidGitVersion.code()

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    /* your options... */

    android.libraryVariants.all { variant ->
        variant.outputs.all { file ->
            if (file.outputFileName.endsWith('.aar')) {
                file.outputFileName = "${project.name}-${variant.buildType.name}-${android.defaultConfig.versionName}.aar"
            }
        }
    }
}
  • Is there a single line we can edit or setting in the IDE we can change? Seems kind of absurd to have to use programming logic just to set the export name of the file. – default123 Jul 17 '23 at 22:42