You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle7.3.3与AGP7.2.2环境下修改APK文件名报错求助

解决AGP 7.x修改APK文件名的AgpDslLockedException错误

错误原因

AGP 7.0+版本后,构建DSL在配置阶段结束后会被锁定。你原来在buildTypes.release内部直接操作applicationVariants的写法,会触发DSL锁定异常——此时build types已经完成配置,不能再在这个上下文里修改变体输出。

修正方案

把修改APK文件名的逻辑移到android块下的finalizeDsl(AGP官方推荐的DSL完成后回调)或afterEvaluate闭包中,同时修正代码里的语法错误:

修改后的完整build.gradle代码

apply plugin: 'com.android.application'

android {
    compileSdkVersion 32

    defaultConfig {
        applicationId "..."
        minSdkVersion 19
        targetSdkVersion 32
        versionCode 1205
        versionName '1.2.05'
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    signingConfigs {
        config {
            keyAlias 'alias'
            keyPassword 'pass'
            storeFile file('mykeystore.jks') // 修正引号缺失问题
            storePassword '12345'
        }
    }

    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
            zipAlignEnabled true
            pseudoLocalesEnabled true
        }

        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            zipAlignEnabled true
            pseudoLocalesEnabled true
            debuggable true
            signingConfig signingConfigs.config
        }
    }

    productFlavors {
    }

    // 推荐使用finalizeDsl,在AGP DSL配置完成后执行
    finalizeDsl {
        project.ext.appName = 'myAppname'
        applicationVariants.all { variant ->
            variant.outputs.all { output ->
                def formattedDate = new Date().format('yyyyMMdd_HHmmss')
                // 修正原语法错误:baseName与versionName的拼接括号问题
                def newName = "${globalScope.project.name}-${output.baseName}-${variant.versionName}.apk"
                newName = newName.replace("app-", "${project.ext.appName}-")
                newName = newName.replace("-release", "-release-${formattedDate}_${variant.versionName.replace(".","")}")
                outputFileName = new File("../../../../release/" + newName)
            }
        }
    }

    // 若finalizeDsl不兼容,可替换为afterEvaluate
    // afterEvaluate {
    //     project.ext.appName = 'myAppname'
    //     applicationVariants.all { variant ->
    //         variant.outputs.all { output ->
    //             def formattedDate = new Date().format('yyyyMMdd_HHmmss')
    //             def newName = "${globalScope.project.name}-${output.baseName}-${variant.versionName}.apk"
    //             newName = newName.replace("app-", "${project.ext.appName}-")
    //             newName = newName.replace("-release", "-release-${formattedDate}_${variant.versionName.replace(".","")}")
    //             outputFileName = new File("../../../../release/" + newName)
    //         }
    //     }
    // }
}

关键改动点

  • 调整变体操作的位置:把applicationVariants.all从release块中移出,放到finalizeDsl或afterEvaluate中,避开DSL锁定阶段。
  • 修复语法问题:
    • 补全storeFile参数的开头单引号,修正为file('mykeystore.jks')。
    • 修正${output.baseName-${variant.versionName}}的括号嵌套错误,改为${output.baseName}-${variant.versionName}。
    • 把versionName.replace(".","")改为variant.versionName.replace(".",""),明确作用域避免歧义。
  • 优化变量定义:把appName的定义移到回调闭包中,避免重复定义。

内容的提问来源于stack exchange,提问作者Robert Angermayr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 11:36:17