Android studio Android Gradle 3.0.0-alpha2插件,无法设置只读属性的值';输出文件';

Android studio Android Gradle 3.0.0-alpha2插件,无法设置只读属性的值';输出文件';,android-studio,android-gradle-plugin,Android Studio,Android Gradle Plugin,我正在使用此代码 applicationVariants.all { variant -> variant.outputs.each { output -> def SEP = "_" def flavor = variant.productFlavors[0].name def buildType = variant.variantData.variantConfiguration.buildType.na

我正在使用此代码

applicationVariants.all { variant -> 
    variant.outputs.each { output ->
        def SEP = "_"
        def flavor = variant.productFlavors[0].name
        def buildType = 
        variant.variantData.variantConfiguration.buildType.name
        def version = variant.versionName
        def date = new Date()
        def formattedDate = date.format('ddMMyy_HHmm')
        def newApkName = PROJECT_NAME + SEP + flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"
        def file = new File(newApkName)
        output.outputFile = file
    }
}

在构建新apk时更改apk文件的名称,但由于我使用的是Android Studio 3.0 Canary 2,因此出现此错误:
无法设置只读属性“outputFile”的值。…

建议如下:

  • 使用
    all()
    而不是
    each()
  • 如果只更改文件名(这是您的情况),请使用
    outputFileName
    而不是
    output.outputFile
指南中的示例:

// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.
android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

下面的代码为我在android studio canary 3.0.0-alpha3上工作

android.applicationVariants.all {
    variant.outputs.all {
        def newApkName
        newApkName = "APPLICATION_NAME-" + defaultConfig.versionName + "-" + defaultConfig.versionCode".apk"
        outputFileName = newApkName;
    }
}
这将更改apk文件名,请参见以下内容:

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        def newApkName = applicationId + "-" + variant.versionName + "(" + variant.versionCode + ")" + ".apk";
        outputFileName = new File("${project.projectDir}/../outputs/apks/" + variant.name, newApkName);
    }
}

我发现gradle 3.0不再工作了

但是,涉及访问outputFile对象的更复杂任务不再有效。这是因为在配置阶段不再创建特定于变量的任务。这导致插件事先不知道其所有输出,但这也意味着更快的配置时间

然后我使用命令
gradlew
编译project.and
cp
将输出apk编译到我指定的路径

在ExecuteShell中,我将命令放在下面

./gradlew clean assembleDebug
cp $WORKSPACE/app/build/outputs/apk/debug/*.apk $WORKSPACE/JenkinsApk

在升级到Android Studio 3.0.0并使用新的gradle之后,现在,APK的输出将按口味名称和构建类型分布在目录中。

这是这个问题的完整示例

您只需在产品风味后粘贴gradle 3.0+

    android.applicationVariants.all { variant ->
    variant.outputs.all {

        def SEP = "_"
        def flavor = variant.productFlavors[0].name
        def buildType = variant.variantData.variantConfiguration.buildType.name
        def version = variant.versionName
        def versionCode = variant.versionCode
        def date = new Date();
        def formattedDate = date.format('ddMMyy_HHmm')

        outputFileName = "${flavor}${SEP}${buildType}${SEP}${version}${SEP}${versionCode}${SEP}${formattedDate}.apk"
    }
    }

我也有同样的问题。错误“无法设置只读属性“outputFile”的值…”

因此,我所做的是在项目结构窗口中将Android插件库的版本更改为2.3.3,现在可以运行了,错误消失了

稍后,对项目进行清理和重建,就这样


希望这会对你有所帮助。

这个问题已经问了一年半了,但也许这会帮助一些(像我一样)最先发现这篇文章的人。我相信更改文件名和目录的答案已经回答了


下面是坏消息的解释,显然一个新的api计划解决这个问题。滚动到链接页面的底部。这可以工作,但你不能更改输出目录我需要更改输出文件路径,而不仅仅是名称。获取错误:设置输出文件名时不支持绝对路径。你可以分享一下你是如何做到的吗?我知道了“我希望更改输出文件路径,而不仅仅是名称。@AmrutBidri,我认为最好问一个新问题(如果还没有在某个地方得到答案的话)。”。这个问题只针对更改文件名。是的,我的问题,是我自己的插件检查gradle版本的问题。你是否将其粘贴到gradle 3.0+和ProductFlavors之后?你可以输入特殊错误?无法获取com.android.build.gradle.internal.api.ApplicationVariatimpl类型对象的未知属性“variant”.
applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def relativeRootDir = output.packageApplication.outputDirectory.toPath()
                     .relativize(rootDir.toPath()).toFile()
            output.outputFileName = new File( "$relativeRootDir/release", newOutputName)
        }
    }