Search code examples
fluttergradle

How to properly set -Xlint:deprecation to identify deprecated API usage in Gradle? (Developing with Flutter in VS Code)


Can someone help me correctly configure the setting for the following warning?

Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

I'm developing a Flutter app using VS Code, and I've tried several options but can't seem to configure it properly to see which part of the code is causing the "deprecated API" warning.

Here is my current 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
}

How can I properly configure the -Xlint:deprecation flag to show details about deprecated API usage?

The solution I found, with tips from Sameri11, was:

In the Flutter project’s android/build.gradle file,

allprojects {
    repositories {
        google()
        mavenCentral()
    }

    // Add compiler parameters
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {                        
            // "-Werror" treats any warning (such as those generated by -Xlint:deprecation or -Xlint:unchecked) 
            // as an error, causing the build to fail.
            options.compilerArgs += ["-Xlint:deprecation", "-Xlint:unchecked"]             
        }

        // Not entirely sure if this is working, but for now, I’m keeping it this way
        tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
            kotlinOptions {
                freeCompilerArgs += ["-Xlint:deprecation"]
            }
        }
    }
}

Solution

  • You may also need to add -Xlint:deprecation to Kotlin compile tasks, along with java. Like this:

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
                kotlinOptions {
                    freeCompilerArgs += ["-Xlint:deprecation"]
                }
            }
    

    There is also one great option to escalate all warnings to errors. May be handful to get attention to actual deprecations.

    For Java:

    options.compilerArgs += ["-Xlint:deprecation", "-Werror"]
    

    For Kotlin:

    kotlinOptions {
        allWarningsAsErrors = true
    }