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

构建Release APK遇mergeReleaseResources失败,OutputFile已废弃求重构

问题解决:重构废弃的OutputFile代码并修复构建错误

一、重构废弃的OutputFile代码

在Android Gradle Plugin(AGP)7.0+版本中,com.android.build.OutputFile已被废弃,需改用新API处理ABI拆分与版本码设置:

  1. 移除import com.android.build.OutputFile导入语句
  2. 替换原applicationVariants代码块为以下逻辑:
android {
    // 配置ABI拆分(若未配置则添加)
    splits {
        abi {
            enable true
            reset()
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
            universalApk false // 关闭通用APK生成,按需开启
        }
    }

    // 为不同ABI版本设置唯一版本码
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.abiFilter
            if (abi != null) {
                output.versionCodeOverride = defaultConfig.versionCode * 1000 + versionCodes[abi]
            }
        }
    }
}

说明:

  • 用output.abiFilter替代废弃的output.getFilter(OutputFile.ABI)
  • 用output.versionCodeOverride设置版本码,这是AGP官方推荐的新方式

二、修复mergeReleaseResources与releaseRuntimeClasspath解析错误

除重构废弃代码外,构建错误还关联以下问题,需同步修复:

  • 依赖重复冲突:删除dependencies中重复的de.undercouch:gradle-download-task依赖,保留新版本:
// 仅保留此行
implementation 'de.undercouch:gradle-download-task:5.3.0'
  • 错误的依赖声明:com.android.tools.build:gradle与org.jetbrains.kotlin:kotlin-gradle-plugin属于构建脚本依赖,不应放在app模块的implementation中,需移至项目根目录build.gradle的buildscript -> dependencies:
    根目录build.gradle示例片段:
buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:7.5.1'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20'
        classpath 'com.google.gms:google-services:4.3.14'
        classpath 'de.undercouch:gradle-download-task:5.3.0'
    }
}

同时删除app模块dependencies中的对应两行:

// 删除这两行
implementation 'com.android.tools.build:gradle:7.5.1'
implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20'
  • 清理构建缓存:执行以下命令后重新构建:
./gradlew clean
./gradlew bundleRelease

三、修正后的app模块build.gradle完整示例

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.gms.google-services'
    id 'de.undercouch.download'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "task"
        minSdk 22
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    splits {
        abi {
            enable true
            reset()
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
            universalApk false
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.abiFilter
            if (abi != null) {
                output.versionCodeOverride = defaultConfig.versionCode * 1000 + versionCodes[abi]
            }
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildToolsVersion '30.0.3'
    ndkVersion '21.4.7075529'
}

dependencies {
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'de.undercouch:gradle-download-task:5.3.0'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 05:05:19