Last Updated on 2021-10-19 by Clay
問題描述
在 Flutter 如何使用照相機的官方教學文件中,提到若是在 Android 系統中透過 Flutter 調用照相機,其最低 SDK 版本(min-sdk-version)必須高於 21 才行。
否則的話,在調用照相機的過程中便會出現以下報錯(也就是我今天遇到的問題):
Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 21 declared in library [:camera] /Users/clay/Projects/FlutterProjects/flutter_medication_reminder/build/camera/intermediates/library_manifest/debug/AndroidManifest.xml as the library might be using APIs not available in 16
Suggestion: use a compatible library with a minSdk of at most 16,
or increase this project's minSdk version to at least 21,
or use tools:overrideLibrary="io.flutter.plugins.camera" to force usage (may lead to runtime failures)
解決方法
為了解決這個問題,我們必須修改 Flutter 專案中 Android 的最低 SDK 版本。
在 Flutter 專案中,我們要修改的文件路徑為 PROJECT_NAME/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 { compileSdkVersion 30 sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.clayatlas.flutter_project" minSdkVersion 16 targetSdkVersion 30 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
可以發現,目前在 defaultConfig 底下的 minSdkVersion 設定為 16,跟報錯訊息提供的一致。應將其修改為 21(或任何你需要的版本號)。
我在調整過最低版本號之後,便能成功執行在 Flutter 專案中調用照相機的程式。
References
- https://stackoverflow.com/questions/52060516/how-to-change-android-minsdkversion-in-flutter-project
- https://github.com/square/in-app-payments-flutter-plugin/issues/78