问题描述
今天导入一个新项目时报错,报错内容如下:
uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:wakelock_plus]
完整报错如下:
D:\WORK\myLearningWork\development\storetest\android\app\src\debug\AndroidManifest.xml Error:
uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:wakelock_plus] D:\WORK\myLearningWork\development\storetest\build\wakelock_plus\intermediates\merged_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 19,
or use tools:overrideLibrary="dev.fluttercommunity.plus.wakelock" to force usage (may lead to runtime failures)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:wakelock_plus] D:\WORK\myLearningWork\development\storetest\build\wakelock_plus\intermediates\merged_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 19,
or use tools:overrideLibrary="dev.fluttercommunity.plus.wakelock" to force usage (may lead to runtime failures)
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 26s
如图:
问题原因
SDK版本太低导致的。
解决方法
修改build.gradle文件的配置
D:\WORK\myLearningWork\development\storetest\android\app\build.gradle
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.storetest"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
将minSdkVersion 改为19即可。
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.storetest"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion 19
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
gradle文件介绍
在Android开发中,build.gradle是一个重要的配置文件,用于定义和管理项目的构建过程。每个Android项目都包含一个或多个build.gradle文件,其中最重要的是项目级别的build.gradle和模块级别的build.gradle。
项目级别的build.gradle
项目级别的build.gradle文件位于项目的根目录下,它用于配置整个项目的构建设置和依赖项。以下是一个典型的项目级别的build.gradle文件的示例:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
// 构建脚本的版本
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2' // Gradle 插件版本
}
}
// 应用插件
apply plugin: 'com.android.application'
android {
compileSdkVersion 30 // 编译目标版本
buildToolsVersion "30.0.3" // 构建工具版本
defaultConfig {
applicationId "com.example.myapp" // 应用程序ID
minSdkVersion 21 // 最低支持的Android版本
targetSdkVersion 30 // 目标Android版本
versionCode 1 // 版本代码
versionName "1.0" // 版本名称
}
buildTypes {
release {
// 发布版本的构建配置
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// 依赖项配置
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
}
}
上述示例中的build.gradle文件包含了以下几个重要部分:
- buildscript:用于定义构建脚本的依赖项。在这个块中,你可以声明Gradle插件的版本以及其他需要的仓库位置。
- apply plugin:应用插件,这里使用的是com.android.application插件,表示这是一个Android应用项目。
- android:Android构建配置的主要部分,用于定义编译版本、构建工具版本、应用程序ID、最低支持的Android版本、目标Android版本等。
- defaultConfig:默认的构建配置,包括应用程序ID、版本代码、版本名称等。
- buildTypes:定义不同构建类型的配置,例如release和debug。可以在这里配置构建选项,如是否启用代码混淆等。
- dependencies:定义项目的依赖项,包括本地库(如jar文件)和远程库(如AndroidX库)。
模块级别的build.gradle
模块级别的build.gradle文件位于每个模块的目录下,用于配置该模块的构建设置和依赖项。以下是一个典型的模块级别的build.gradle文件的示例:
plugins {
id 'com.android.library'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// 依赖项配置
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
}
}
模块级别的build.gradle文件与项目级别的build.gradle文件相似,但通常不包含buildscript块。它包含了与项目级别的build.gradle文件相同的配置项,如android和dependencies块,用于配置模块的构建设置和依赖项。
总结
总结来说,build.gradle文件是Android开发中用于定义和管理项目的构建过程的关键配置文件。项目级别的build.gradle文件用于配置整个项目的构建设置和依赖项,而模块级别的build.gradle文件用于配置每个模块的构建设置和依赖项。通过修改这些文件,开发人员可以自定义和控制Android应用的构建过程。