skip to Main Content

I am a newbie to flutter and trying to build the apk that can be installed on my android device. I am able to build the release apk using the following command:

flutter build apk

To debug certain features of my app, I need to build the debug apk:

flutter build apk –debug

This fails with the following error on command prompt:

ERROR:<<myProjectDir>>buildpath_provider_androidintermediatesruntime_library_classes_jardebugclasses.jar: D8: java.lang.NullPointerException: 
Cannot invoke "String.length()" because "<parameter1>" is null
ERROR:<<myProjectDir>>buildflutter_secure_storageintermediatesruntime_library_classes_jardebugclasses.jar: D8: java.lang.NullPointerException: 
Cannot invoke "String.length()" because "<parameter1>" is null
ERROR:<<myProjectDir>>buildfile_pickerintermediatesruntime_library_classes_jardebugclasses.jar: D8: java.lang.NullPointerException: 
Cannot invoke "String.length()" because "<parameter1>" is null

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeLibDexDebug'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Failed to transform classes.jar (project :file_picker) to match attributes {artifactType=android-dex, asm-transformed-variant=NONE, 
com.android.build.api.attributes.AgpVersionAttr=7.3.0, com.android.build.api.attributes.BuildTypeAttr=debug, c
om.android.build.gradle.internal.attributes.VariantAttr=debug, dexing-enable-desugaring=true, dexing-enable-jacoco-instrumentation=false, 
dexing-is-debuggable=true, dexing-min-sdk=21, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for DexingWithClasspathTransform: <<myProjectDir>>buildfile_pickerintermediatesruntime_library_classes_jardebugclasses.jar.
         > Error while dexing.
   > Failed to transform classes.jar (project :flutter_secure_storage) to match attributes {artifactType=android-dex, asm-transformed-variant=NONE, 
com.android.build.api.attributes.AgpVersionAttr=7.3.0, com.android.build.api.attributes.BuildTypeAttr=debug, com.android.build.gradle.internal.attributes.VariantAttr=debug, dexing-enable-desugaring=true, dexing-enable-jacoco-instrumentation=false, dexing-is-debuggable=true, dexing-min-sdk=21, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for DexingWithClasspathTransform: <<myProjectDir>>buildflutter_secure_storageintermediatesruntime_library_classes_jardebugclasses.jar.
         > Error while dexing.
   > Failed to transform classes.jar (project :path_provider_android) to match attributes {artifactType=android-dex, asm-transformed-variant=NONE, 
com.android.build.api.attributes.AgpVersionAttr=7.3.0, com.android.build.api.attributes.BuildTypeAttr=debug, com.android.build.gradle.internal.attributes.VariantAttr=debug, dexing-enable-desugaring=true, dexing-enable-jacoco-instrumentation=false, dexing-is-debuggable=true, dexing-min-sdk=21, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for DexingWithClasspathTransform: <<myProjectDir>>buildpath_provider_androidintermediatesruntime_library_classes_jardebugclasses.jar.
         > Error while dexing.

* 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 22s
Running Gradle task 'assembleDebug'...                             24.3s
Gradle task assembleDebug failed with exit code 1

flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.22.2, on Microsoft Windows [Version 10.0.19045.4529], locale en-IN)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.10.0)
[!] Android Studio (not installed)
[√] IntelliJ IDEA Community Edition (version 2024.1)
[√] Connected device (4 available)
[√] Network resources

! Doctor found issues in 1 category.

My 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"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader("UTF-8") { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
    flutterVersionCode = "1"
}

def flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
    flutterVersionName = "1.0"
}

android {
    namespace = "com.example.untitled"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.example.untitled"
        // 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.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = flutterVersionCode.toInteger()
        versionName = flutterVersionName
      //  multiDexEnabled true

    }

    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 = "../.."
}

My android/build.gradle:

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = "../build"
//rootProject.buildDir = "E:\build\backEnd"

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(":app")
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

//configurations.all {
  //  resolutionStrategy {
    //    force("androidx.core:core-ktx:1.9.0")
   // }
//}

gradle.properties:

    org.gradle.jvmargs=-Xmx4G -XX:+HeapDumpOnOutOfMemoryError
    android.useAndroidX=true
    android.enableJetifier=true

gradle-wrapper.properties:

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https://services.gradle.org/distributions/gradle-8.7-all.zip

settings.gradle:

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

include ":app"

local.properties:
flutter.sdk=e:\myApp\flutter
sdk.dir=E:\myApp\android
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1

JDK Version:

openjdk version "21.0.3" 2024-04-16 LTS OpenJDK Runtime Environment
Corretto-21.0.3.9.1 (build 21.0.3+9-LTS) OpenJDK 64-Bit Server VM
Corretto-21.0.3.9.1 (build 21.0.3+9-LTS, mixed mode, sharing)

Gradle Version:


Gradle 8.7

Build time: 2024-03-22 15:52:46 UTC Revision:
650af14d7653aa949fce5e886e685efc9cf97c10

Kotlin: 1.9.22 Groovy: 3.0.17 Ant: Apache Ant(TM)
version 1.10.13 compiled on January 4 2023 JVM: 21.0.3
(Amazon.com Inc. 21.0.3+9-LTS) OS: Windows 10 10.0 amd64

Things that I have tried so far:

  • Clearing gradle cache

  • Following code in both app and andriod build.gradle (one by one)

    configurations.all {
    resolutionStrategy {
    force("androidx.core:core-ktx:1.9.0")
    }
    }

My hunch is this problem is either due to incompatibility (of what) or some missing configuration.

PS: The Web Build works fine

2

Answers


  1. Chosen as BEST ANSWER

    With inputs from the SO community plus trial and error helped me to resolve the problem by downgrading Java version to 19:

    flutter config --jdk-dir=<<path to java>>jdk-19.0.2
    

    How did I figure this out:

    Running out of options, I decided to create a new Flutter Project and then apply my changes one by one to figure out the issue. However the clue (marked as bold and italics) was in the output of flutter create:

    Creating project test_flutter... Resolving dependencies in test_flutter... (1.2s) Downloading packages... Got dependencies in test_flutter. Wrote 129 files.

    All done! You can find general documentation for Flutter at: https://docs.flutter.dev/ Detailed API documentation is available at: https://api.flutter.dev/ If you prefer video documentation, consider: https://www.youtube.com/c/flutterdev

    In order to run your application, type:

    $ cd test_flutter $ flutter run

    Your application code is in test_flutterlibmain.dart.

    Newer than known valid Java version (21.0.3), gradle (7.6.3). Treating as valid configuration. The configured version of Java detected may conflict with the Gradle version in your new Flutter app.

    [RECOMMENDED] If so, to keep the default Gradle version 7.6.3, make sure to download a compatible Java version (Java 11 <= compatible Java version < Java 20). You may configure this compatible Java version by running: flutter config --jdk-dir=<JDK_DIRECTORY> Note that this is a global configuration for Flutter.

    Alternatively, to continue using your configured Java version, update the Gradle version specified in the following file to a compatible Gradle version: E:toDeletetestFPtest_flutterandroid/gradle/wrapper/gradle-wrapper.properties

    You may also update the Gradle version used by running ./gradlew wrapper --gradle-version=<COMPATIBLE_GRADLE_VERSION>.

    See https://docs.gradle.org/current/userguide/compatibility.html#java for details on compatible Java/Gradle versions, and see https://docs.gradle.org/current/userguide/gradle_wrapper.html#sec:upgrading_wrapper for more details on using the Gradle Wrapper command to update the Gradle version used.

    Based on this I looked at the Gradle - Java Compatibility matrix and tried different combinations of Java and Gradle. Finally, Java 19 and Gradle 8.7 worked.

    PS: The output of flutter create is for the new Flutter project with default version of Gradle (different from my original project).


  2. I have been having similar issue > Failed to transform debug (project :image_picker_android) to match attributes {artifactType=android-dex, asm-transformed-variant=NONE, but i knew its because of inconsistency between gradle version, java and my dependency and upon making sure they matched and still the problem persisted then i did a In case you are using android studio

    And now problem solved

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search