Подтвердить что ты не робот

Невозможно установить значение свойства read-only 'outputFile' для ApkVariantOutputImpl_Decorated

Я использовал следующий код в своем gradle script для переименования apks, сгенерированного с помощью AndroidStudio:

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(output.outputFile.parent, defaultConfig.versionCode + "_" + output.outputFile.name)
    }
}

Таким образом, он генерировал apks с именами, такими как: 345-app-release.apk, где 345 - версияCode.

Но после обновления до AndroidStudio 3.0 он возвращает следующую ошибку:

Невозможно установить значение свойства read-only 'outputFile' для ApkVariantOutputImpl_Decorated {apkData = Main {type = MAIN, fullName = debug, filters = []}} типа com.android.build.gradle.internal.api.ApkVariantOutputImpl.

Как я могу добиться аналогичного переименования с помощью новых инструментов сборки.

4b9b3361

Ответ 1

Используйте output.outputFileName вместо output.outputFile

Ответ 2

2019 - Простое решение для Gradle 3. 0+ ** и 3.1.0

Чтобы изменить имя APK в Android

 android { //add inside the android {}
   ......
   applicationVariants.all { variant ->
       variant.outputs.all {
           def flavor = variant.name
           def versionName = variant.versionName
           outputFileName = "prefix_${flavor}_${versionName}.apk"
       }
   }
}

prefix_release_1.0.1.apk

Ответ 3

Попробуйте этот код:

buildTypes {

       applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def name = "myapp_v${variant.versionName}(${variant.versionCode}).apk"
                output.outputFileName = name
            }
        }

    }

Ответ 4

In or After gradle 3.1.0

попробуйте ниже код

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = new File(
                    "release_build", // here you can change the name
                    output.outputFile.name)
        }
}

Ответ 5

В моем случае я решил эту проблему, просто отказавшись обновить Gradle 4.4 (в моем случае). Поэтому, когда Studio просит (когда вы открываете свой проект в первый раз), обновить Gradle для поддержки мгновенного запуска и т.д., Просто откажитесь, и вы все должно быть в порядке.

Ответ 6

измените свой код на: -

    applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(output.outputFile.parent, "${variant.applicationId}-${variant.versionName}.apk")
    }
}

Ответ 7

То, что работало для меня, было 1. измените каждый на "все" 2. измените output.outputFile на "outputFileName" 3. Запустите $./Gradlew clean build в терминале

Например, если ваши настройки artifacts.gradle были такими:

android.applicationVariants.all { variant ->

variant.outputs.each { output ->
    def finalVersionCode = 10000 + versionCode
    output.versionCodeOverride = finalVersionCode
    output.outputFile = new File(
         output.outputFile.parent,       output.outputFile.name.replace(".apk","-${finalVersion}.apk"))
}

}

Тогда вы бы хотели изменить это на:

android.applicationVariants.all { variant ->

variant.outputs.all { output ->
    def finalVersionCode = 10000 + versionCode
    output.versionCodeOverride = finalVersionCode
    outputFileName = new File(
         output.outputFile.parent,
         outputFileName.replace(".apk", "-${finalVersionCode}.apk"))
}

}