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

如何解决Flutter中image_picker的Android资源链接错误?

问题:image_picker 1.0.4版本导致Android资源链接失败

使用image_picker:1.0.4构建Android Release版本时,出现以下错误:

任务':image_picker_android:verifyReleaseResources'执行失败。
执行com.android.build.gradle.tasks.VerifyLibraryResourcesTask$Action时发生失败
Android资源链接失败

当前Gradle配置如下:

android/build.gradle配置

ext.kotlin_version = '1.8.0'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.14'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

android/app/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"

android {
    namespace "com.example.health"
    compileSdkVersion 34
    ndkVersion flutter.ndkVersion

    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.example.health"
        minSdkVersion 21
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        missingDimensionStrategy 'react-native-camera', 'general'
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }

}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'com.google.android.material:material:1.2.0'

}
configurations.all {
    resolutionStrategy {
    force 'com.android.support:support-v4:27.1.0'
    }
}

解决方案

1. 删除冲突的强制依赖

移除configurations.all块中强制的com.android.support:support-v4:27.1.0,该依赖与image_picker_android使用的AndroidX资源冲突:

// 删除这段代码
configurations.all {
    resolutionStrategy {
    force 'com.android.support:support-v4:27.1.0'
    }
}

2. 升级AndroidX依赖版本

当前AndroidX依赖版本与compileSdk 34不兼容,升级到适配版本:
修改dependencies块内容:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'com.google.android.material:material:1.11.0'
}

3. 统一子项目编译配置

在android/build.gradle的subprojects块中添加配置,确保所有子模块(包括image_picker_android)使用与主项目一致的compileSdk和targetSdk:

subprojects {
    project.evaluationDependsOn(':app')
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                compileSdkVersion 34
                defaultConfig {
                    targetSdkVersion 34
                }
            }
        }
    }
}

4. 清理缓存并重新构建

执行以下命令完成修复:

flutter clean
flutter pub get
flutter build apk --release

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 09:46:03