flutter添加webview功能之后报错 webview_flutter包 Dependency 'androidx.webkit:webkit:1.8.0' requires libraries and applications Dependency 'androidx.browser:browser:1.6.0' requires libraries and applications

在项目开发中需要添加webview,加载内置的html文件,代码写完后ios运行没有问题,运行安卓时报错,错误提示如下:


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
   > 2 issues were found when checking AAR metadata:

       1.  Dependency 'androidx.webkit:webkit:1.8.0' requires libraries and applications that
           depend on it to compile against version 34 or later of the
           Android APIs.

           :app is currently compiled against android-33.

           Also, the maximum recommended compile SDK version for Android Gradle
           plugin 7.3.0 is 33.

           Recommended action: Update this project's version of the Android Gradle
           plugin to one that supports 34, then update this project to use
           compileSdkVerion of at least 34.

           Note that updating a library or application's compileSdkVersion (which
           allows newer APIs to be used) can be done separately from updating
           targetSdkVersion (which opts the app in to new runtime behavior) and
           minSdkVersion (which determines which devices the app can be installed
           on).

       2.  Dependency 'androidx.browser:browser:1.6.0' requires libraries and applications that
           depend on it to compile against version 34 or later of the
           Android APIs.

           :app is currently compiled against android-33.

错误提示的关键字主要有:Gradle、minSdkVersion、compileSdkVerion、targetSdkVersion

Gradle:
借助 Gradle 版本目录,您能够以可扩容的方式添加和维护依赖项和插件。使用 Gradle 版本目录,您可以在拥有多个模块时更轻松地管理依赖项和插件。您不必对各个 build 文件中的依赖项名称和版本进行硬编码,也不必在每次需要升级依赖项时都更新每个条目,而是可以创建一个包含依赖项的中央版本目录,各种模块可在 Android Studio 协助下以类型安全的方式引用该目录。
这是安卓开发者网站的说明,我们可以理解为可以通过Gradle去管理不同模块的依赖项和插件。这个以后再说。

minSdkVersion:
应用程序支持的最低API版本.或者说应用运行所需最低 API 级别的整数。如果系统的 API 级别低于该属性中指定的值,Android 系统将阻止用户安装应用。

compileSdkVerion:
CompileSdkVersion 指定 Gradle 编译你的 APP 时使用的 Android API 版本,你的 App 可以使用该版本或者更低版本的 API 特性。简单来说,如果你的 APP 在开发中需要用到一些比较新的 API 特性,那么你的 compileSdkVersion 的数值就应该大于或等于相应的 API 级别。

targetSdkVersion:
TargetSdkVersion 按照字面意思理解就是目标 SdkVersion。如果没有设置,则默认值为minSdkVersion。TargetSdkVersion 是 Android 系统实现「前向兼容」的主要手段,当你设置了targetSdkVersion的时候,表示你已经充分测试过了你的App在该目标版本的运行情况(准确的说应该是从 minSdkVersion 到 targetSdkVersion 区间所有的系统版本),除非更新 targetSdkVersion,否则不改变应用的行为。

从文中我们可以看出需要我们把compileSdkVerion版本设置为 34,Gradle 目前的7.3.0只支持到33,所以Gradle版本也要修改
我们先修改compileSdkVerion同时把targetSdkVersion也修改到最新:
目录为:android/app/build.gradle


修改后记得保存。

然后修改gradle版本,
首先查看一下gradle对安卓的支持版本
https://docs.gradle.org/current/userguide/compatibility.html

最新的稳定版应该是8.2,我们设置8.2试一下
目录:
gradle/wrapper/gradle-wrapper.properties


保存,然后去运行安卓项目,此时vscode会去下载gradle,要等会,如果下载失败可以通过Andriod Studio来选择gradle版本,操作如下:



点击ok,Andriod Studio会自动下载gradle。

再次运行就可以了。

本人原来是ios开发,对安卓开发的配置可能存在一些问题,如果文章存在问题,请大家帮忙指出,谢谢。

官方文档:
https://developer.android.com/ndk/guides/sdk-versions?hl=zh-cn
https://developer.android.com/guide/topics/manifest/uses-sdk-element?hl=zh-cn
https://developer.android.com/build/migrate-to-catalogs?hl=zh-cn
https://docs.gradle.org/current/userguide/compatibility.html
部分内容引用:
https://blog.csdn.net/crazestone0614/article/details/127979666

posted @ 2024-02-20 10:33  hczhhm  阅读(411)  评论(0编辑  收藏  举报