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

Flutter Release APK构建失败,Debug与App Bundle可正常构建求助

解决Flutter构建Release APK时android:attr/lStar缺失问题

问题背景

执行flutter build apk构建Release版本APK时触发资源链接错误,提示android:attr/lStar not found,但flutter build apk --debug和flutter build appbundle可正常完成构建,flutter doctor -v无异常,已排查一整天未解决。

错误日志

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':unity_mediation:verifyReleaseResources'.
> A failure occurred while executing com.android.build.gradle.tasks.VerifyLibraryResourcesTask$Action
   > Android resource linking failed
     ERROR:/Users/bobbyjulian/.gradle/caches/transforms-3/a68f5ae822254b02ed9aba74a762d661/transformed/core-1.9.0/res/values/values.xml:104:5-113:25: AAPT: error: resource android:attr/lStar not found.

相关配置

/app/src/build.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 33

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        applicationId "com.thesimlife.thesimlife"
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}
apply plugin: 'com.google.gms.google-services'

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "com.unity3d.mediation:adcolony-adapter:1.0.0"
    implementation "com.unity3d.mediation:applovin-adapter:1.0.0"
    implementation "com.unity3d.mediation:vungle-adapter:1.0.1"
    implementation "com.unity3d.mediation:facebook-adapter:1.0.0"
    implementation "com.unity3d.mediation:admob-adapter:1.0.0"
}

项目根目录build.gradle

buildscript {
    ext.kotlin_version = '1.7.20'
    repositories {
        google()
        mavenCentral()
        maven {
            url 'https://unity3ddist.jfrog.io/artifactory/unity-mediation-mvn-prod-local/'
        }
    }

    dependencies {
        //classpath 'com.android.tools.build:gradle:4.1.0'
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath 'com.google.gms:google-services:4.3.10'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            url 'https://unity3ddist.jfrog.io/artifactory/unity-mediation-mvn-prod-local/'
        }
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

解决方案

1. 强制统一AndroidX Core库版本

android:attr/lStar是AndroidX Core库1.7.0及以上版本新增的属性,问题源于unity_mediation模块依赖的Core库版本与主项目存在冲突。在根目录build.gradle的subprojects块中添加依赖强制统一规则:

subprojects {
    project.configurations.all {
        resolutionStrategy {
            // 强制所有模块使用指定版本的Core库,与compileSdkVersion 33兼容
            force 'androidx.core:core-ktx:1.8.0'
        }
    }
    project.evaluationDependsOn(':app')
}

2. 明确指定targetSdkVersion

将/app/src/build.gradle中defaultConfig的targetSdkVersion改为与compileSdkVersion一致的33,避免Gradle自动选择的版本引发冲突:

defaultConfig {
    // ...其他配置
    targetSdkVersion 33
    // ...其他配置
}

3. 更新Unity Mediation适配器版本

当前使用的适配器版本较旧,可能存在依赖兼容问题,将dependencies中的适配器版本替换为最新稳定版:

dependencies {
    // ...其他依赖
    implementation "com.unity3d.mediation:adcolony-adapter:最新版本"
    implementation "com.unity3d.mediation:applovin-adapter:最新版本"
    implementation "com.unity3d.mediation:vungle-adapter:最新版本"
    implementation "com.unity3d.mediation:facebook-adapter:最新版本"
    implementation "com.unity3d.mediation:admob-adapter:最新版本"
}

4. 清理缓存并重新构建

执行以下命令清理缓存,再尝试构建:

flutter clean
rm -rf ~/.gradle/caches
flutter build apk

原因说明

Debug构建与Release构建的资源验证策略不同,App Bundle的资源打包逻辑也与APK存在差异,因此未触发该依赖冲突问题。通过统一Core库版本、明确SDK版本,可解决资源链接时的属性缺失错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 00:14:57