Skip to content

Fixing "Cannot invoke 'String.length()'" Dexing Error in Gradle Builds

Problem Statement

When building Android projects with Gradle, you might encounter a dexing error preventing successful compilation:

> Failed to transform commons-lang3-3.14.0.jar 
  > Execution failed for DexingNoClasspathTransform
    > Error while dexing
    java.lang.NullPointerException: Cannot invoke "String.length()" because "<parameter1>" is null

This error typically occurs when:

  1. Your environment has changed without corresponding project updates
  2. There are compatibility issues between libraries, Gradle versions, or JDK versions
  3. Dependency versions are mismatched or too new for your current configuration

The project might build successfully after a long period without changes, only to fail suddenly due to external dependencies or toolchain updates. Common remediation steps like ./gradlew clean or clearing Gradle caches often don't resolve it.

1. Use Compatible JDK Version

The most common fix involves switching to JDK 17 instead of newer versions (like JDK 21):

Step-by-Step

  1. Download JDK 17 from Adoptium
  2. Configure in Android Studio:
    • File → Project Structure → SDK Location → JDK Location
    • Set path to JDK 17 installation directory
  3. Alternatively for CLI builds, set environment variable:
    bash
    # Linux/macOS
    export JAVA_HOME=/path/to/jdk17
    
    # Windows Command Prompt
    set JAVA_HOME=C:\path\to\jdk17
    
    # Windows PowerShell
    $env:JAVA_HOME = "C:\path\to\jdk17"

2. Update Android Gradle Plugin (AGP) and Gradle

Upgrade AGP in build.gradle:

groovy
plugins {
    id 'com.android.application' version '8.4.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.20' apply false
}

Upgrade Gradle in gradle-wrapper.properties:

properties
distributionUrl=https:\//services.gradle.org/distributions/gradle-8.7-bin.zip

Use Android Studio's AGP Upgrade Assistant:

  • Tools → Android → AGP Upgrade Assistant
  • Follow on-screen instructions to migrate to compatible versions

3. Resolve Dependency Conflicts

Version mismatches between plugins and libraries are frequent causes:

Library and Plugin Alignment

groovy
dependencies {
    // Align with Kotlin plugin version
    implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.9.20' // Matches plugin
    implementation 'androidx.core:core-ktx:1.12.0' // Instead of 1.13.1
    implementation 'com.google.android.material:material:1.11.0' // Instead of 1.12.0
}

Remove problematic dependencies:

yaml
dependencies:
  # Avoid generic version specifiers
  # camera: ^0.10.0  # Instead of camera: any

4. Downgrade Problematic Libraries (Temporary Fix)

If newer libraries cause dexing issues, revert to stable versions:

groovy
dependencies {
    // Downgrade specific library
    implementation 'org.apache.commons:commons-lang3:3.13.0' // Instead of 3.14.0
}

Common Pitfalls to Avoid

WARNING

Avoid Floating Dependency Versions: Using + or any in dependencies causes nondeterministic builds:

groovy
// ❌ Avoid
implementation 'androidx.core:core-ktx:+' 

// ✅ Specify version
implementation "androidx.core:core-ktx:$exact_version"

DANGER

JDK Mismatch Symptoms:

  • Works with Gradle 8.x but fails on older Gradle
  • Fails consistently across environments regardless of cache cleanup
  • Appears after automatic Java or Android Studio updates

Verification Steps

After applying fixes:

  1. Run build with clean cache:
    bash
    ./gradlew clean build --refresh-dependencies
  2. Verify environment settings:
    bash
    ./gradlew -v  # Check Gradle JDK version
  3. Check dependency tree for conflicts:
    bash
    ./gradlew :app:dependencies

Why These Changes Work

  1. JDK 17: Recent Android builds (especially AGP < 8) contain bytecode incompatible with JDK 21
  2. AGP/Gradle Updates: Resolve dexing toolchain issues with newer libraries
  3. Version Alignment: Prevents runtime class conflicts arising from API differences
  4. Library Downgrades: Temporarily bypass bugs in newer library versions while waiting for fixes

TIP

If issues persist after applying these solutions:

  1. Verify all Kotlin/Gradle/Java versions in official compatibility docs
  2. Check library GitHub issues for known problems with specific versions
  3. Consider migrating to newer Gradle as long-term solution

Final Recommendations: Start with JDK 17 and AGP updates before modifying dependencies. Test configuration changes incrementally to isolate solution vectors.