You need to enable JavaScript to run this app. Android10无法创建Excel 添加requestLegacyExternalStorage后资源链接失败
优惠活动
大模型
产品
解决方案
定价
更多

Android10无法创建Excel 添加requestLegacyExternalStorage后资源链接失败

安卓项目添加requestLegacyExternalStorage属性报错解决方案

  • 检查compileSdkVersion配置
    android:requestLegacyExternalStorage是Android 10(API等级29)新增的系统属性,若模块级build.gradle中compileSdkVersion设置低于29,编译器无法识别该属性会触发资源链接失败错误。需将compileSdkVersion调整为29或更高版本,配置示例:
    android {
        compileSdk 29 // 也可设置为30、33、34等更高版本,向下兼容
        defaultConfig {
            // 原有配置无需修改
        }
    }
    
  • 检查属性放置位置
    该属性必须添加在AndroidManifest.xml的<application>节点下,放置在<manifest>根节点、<activity>等组件节点下都会触发报错,正确配置示例:
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="你的包名">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/你的主题"
            android:requestLegacyExternalStorage="true">
            
            <!-- 你的四大组件配置 -->
        </application>
    </manifest>
    
  • 检查Android Gradle插件(AGP)版本
    若你使用的AGP版本低于3.6.0,旧版本插件对API 29新增属性的解析存在兼容性问题,建议升级AGP到3.6.0或更高版本,项目级build.gradle配置参考:
    buildscript {
        dependencies {
            classpath "com.android.tools.build:gradle:4.2.2"
            // 其余依赖无需修改
        }
    }
    
  • 适配说明
    若你的项目targetSdkVersion已升级到30及以上,requestLegacyExternalStorage属性会被系统自动忽略,此时生成Excel文件需使用MediaStore API写入公共文档目录,或调用系统存储访问框架(SAF)实现文件创建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 23:06:08