VS Code开发Flutter:如何配置Gradle的-Xlint:deprecation查看废弃API详情?
解决Flutter编译时废弃API警告无详细信息的问题
在VS Code中开发Flutter应用时,遇到如下编译警告:
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
尝试多种配置后,仍无法定位到具体是哪部分代码使用了废弃API。以下是当前android/app/build.gradle的配置代码:
plugins { id "com.android.application" id "kotlin-android" // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins. id "dev.flutter.flutter-gradle-plugin" id 'com.google.gms.google-services' } android { namespace = "testxxxx" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } allprojects { gradle.projectsEvaluated { tasks.withType(JavaCompile) { // options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" options.compilerArgs += ["-Xlint:deprecation"] } } } kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8 } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId = "com.test.testxxx" // You can update the following values to match your application needs. // For more information, see: https://flutter.dev/to/review-gradle-config. minSdkVersion = 23 // minSdk = flutter.minSdkVersion targetSdk = flutter.targetSdkVersion versionCode = flutter.versionCode versionName = flutter.versionName } 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 { // Import the Firebase BoM implementation platform('com.google.firebase:firebase-bom:33.2.0') // TODO: Add the dependencies for Firebase products you want to use // When using the BoM, don't specify versions in Firebase dependencies implementation 'com.google.firebase:firebase-analytics' // Add the dependencies for any other desired Firebase products // https://firebase.google.com/docs/android/setup#available-libraries }
解决方案
正确配置-Xlint:deprecation参数的方式是修改Flutter项目根目录下的android/build.gradle文件,添加以下内容:
allprojects { repositories { google() mavenCentral() } // 添加编译器参数 gradle.projectsEvaluated { tasks.withType(JavaCompile) { // "-Werror"会将所有警告(比如-Xlint:deprecation或-Xlint:unchecked生成的警告)视为错误,导致构建失败 options.compilerArgs += ["-Xlint:deprecation", "-Xlint:unchecked"] } // 目前不确定这部分是否生效,暂时保留 tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) { kotlinOptions { freeCompilerArgs += ["-Xlint:deprecation"] } } } }
内容的提问来源于stack exchange,提问作者cealex
相关产品推荐
相关产品推荐

