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

Flutter in_appwebview编译报错:找不到WebSettings相关方法

Flutter in_appwebview插件Android编译错误解决方案

问题描述

使用flutter_inappwebview 5.4.3+7版本时,Android编译失败,出现多个cannot find symbol错误,核心是找不到WebSettings类的setAppCachePath和setAppCacheEnabled方法。

完整报错信息

C:\src\flutter.pub-cache\hosted\pub.dartlang.org\flutter_inappwebview-5.4.3+7\android\src\main\java\com\pichillilorenzo\flutter_inappwebview\in_app_webview\InAppWebView.java:280:
error: cannot find symbol
settings.setAppCachePath(options.appCachePath);
^   symbol:   method setAppCachePath(String)   location: variable settings of type WebSettings
C:\src\flutter.pub-cache\hosted\pub.dartlang.org\flutter_inappwebview-5.4.3+7\android\src\main\java\com\pichillilorenzo\flutter_inappwebview\in_app_webview\InAppWebView.java:494:
error: cannot find symbol
settings.setAppCacheEnabled(false);
^   symbol:   method setAppCacheEnabled(boolean)   location: variable settings of type WebSettings
C:\src\flutter.pub-cache\hosted\pub.dartlang.org\flutter_inappwebview-5.4.3+7\android\src\main\java\com\pichillilorenzo\flutter_inappwebview\in_app_webview\InAppWebView.java:504:
error: cannot find symbol
settings.setAppCacheEnabled(true);
^   symbol:   method setAppCacheEnabled(boolean)   location: variable settings of type WebSettings
C:\src\flutter.pub-cache\hosted\pub.dartlang.org\flutter_inappwebview-5.4.3+7\android\src\main\java\com\pichillilorenzo\flutter_inappwebview\in_app_webview\InAppWebView.java:515:
error: cannot find symbol
settings.setAppCachePath(ctx.getCacheDir().getAbsolutePath());
^   symbol:   method setAppCachePath(String)   location: variable settings of type WebSettings
C:\src\flutter.pub-cache\hosted\pub.dartlang.org\flutter_inappwebview-5.4.3+7\android\src\main\java\com\pichillilorenzo\flutter_inappwebview\in_app_webview\InAppWebView.java:517:
error: cannot find symbol
settings.setAppCacheEnabled(true);
^   symbol:   method setAppCacheEnabled(boolean)   location: variable settings of type WebSettings
C:\src\flutter.pub-cache\hosted\pub.dartlang.org\flutter_inappwebview-5.4.3+7\android\src\main\java\com\pichillilorenzo\flutter_inappwebview\in_app_webview\InAppWebView.java:521:
error: cannot find symbol
settings.setAppCacheEnabled(false);
^   symbol:   method setAppCacheEnabled(boolean)   location: variable settings of type WebSettings
C:\src\flutter.pub-cache\hosted\pub.dartlang.org\flutter_inappwebview-5.4.3+7\android\src\main\java\com\pichillilorenzo\flutter_inappwebview\in_app_webview\InAppWebView.java:767:
error: cannot find symbol
settings.setAppCachePath(newOptions.appCachePath);
^   symbol:   method setAppCachePath(String)   location: variable settings of type WebSettings 
注意:部分输入文件使用或覆盖了已废弃的API。注意:使用 -Xlint:deprecation 重新编译以获取详细信息。注意:部分输入文件使用了未检查或不安全的操作。注意:使用 -Xlint:unchecked 重新编译以获取详细信息。共7个错误

FAILURE: 构建因异常失败。

- 出错原因:任务 ':flutter_inappwebview:compileDebugJavaWithJavac' 执行失败。

    编译失败;请查看编译器错误输出获取详细信息。

- 尝试方案:使用 --stacktrace 选项获取堆栈跟踪。使用 --info 或 --debug 选项获取更多日志输出。使用 --scan 选项获取完整分析信息。

- 如需更多帮助,请访问 https://help.gradle.org

构建耗时11秒失败,异常信息:Gradle任务assembleDebug执行失败,退出码为1

错误原因

Android API 31(Android 12)及以上版本中,WebSettings类的setAppCachePath和setAppCacheEnabled方法被移除,而你使用的flutter_inappwebview 5.4.3+7版本未适配这个API变更。

解决方案

  • 优先升级插件版本:这是最稳妥的方案,新版本已经修复了高API级别适配问题。执行以下命令升级:
    flutter pub upgrade flutter_inappwebview
    
  • 临时修改插件代码(不推荐):如果无法升级,找到报错的InAppWebView.java文件,给所有调用这两个方法的代码加上API版本判断:
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.R) {
        settings.setAppCachePath(options.appCachePath);
        settings.setAppCacheEnabled(true); // 替换为对应布尔值
    }
    
    注意:该修改会在执行flutter pub get时被覆盖,仅作为临时应急方案。
  • 降低Android编译SDK版本:在项目的android/app/build.gradle文件中,将compileSdkVersion和targetSdkVersion修改为30及以下。但这会影响应用对Android 12+新特性的支持,仅作为短期临时方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 05:09:35