airbnb/lottie-android · error · IllegalArgumentException

Cannot interpolate between gradients. Lengths vary ({} vs {}

Error message

Cannot interpolate between gradients. Lengths vary ({} vs {})

What it means

GradientColor.lerp() interpolates between two gradient color stops element-by-element. Before the lerp loop, it asserts gc1.colors.length == gc2.colors.length; if the two gradients have a different number of color entries, it throws IllegalArgumentException. The source comments note that AnimatableGradientColorValue.ensureInterpolatableKeyframes() is supposed to pad gradients so all keyframes share the same color count — if that normalization step fails or is bypassed, this guard fires.

Source

Thrown at lottie/src/main/java/com/airbnb/lottie/model/content/GradientColor.java:45

    return colors.length;
  }

  public void lerp(GradientColor gc1, GradientColor gc2, float progress) {
    // Fast return in case start and end is the same
    // or if progress is at start/end or out of [0,1] bounds
    if (gc1.equals(gc2)) {
      copyFrom(gc1);
      return;
    } else if (progress <= 0f) {
      copyFrom(gc1);
      return;
    } else if (progress >= 1f) {
      copyFrom(gc2);
      return;
    }

    if (gc1.colors.length != gc2.colors.length) {
      throw new IllegalArgumentException("Cannot interpolate between gradients. Lengths vary (" +
          gc1.colors.length + " vs " + gc2.colors.length + ")");
    }

    for (int i = 0; i < gc1.colors.length; i++) {
      positions[i] = MiscUtils.lerp(gc1.positions[i], gc2.positions[i], progress);
      colors[i] = GammaEvaluator.evaluate(progress, gc1.colors[i], gc2.colors[i]);
    }

    // Not all keyframes that this GradientColor are used for will have the same length.
    // AnimatableGradientColorValue.ensureInterpolatableKeyframes may add extra positions
    // for some keyframes but not others to ensure that it is interpolatable.
    // If there are extra positions here, just duplicate the last value in the gradient.
    for (int i = gc1.colors.length; i < positions.length; i++) {
      positions[i] = positions[gc1.colors.length - 1];
      colors[i] = colors[gc1.colors.length - 1];
    }
  }

View on GitHub (pinned to 05ea92e903)

Solutions

  1. Ensure all keyframes for a given gradient property define the same number of color stops. Re-export the animation from After Effects / LottieFiles with consistent gradient settings.
  2. If calling lerp() directly, pre-normalize both GradientColor instances to the same length by copying positions/colors and padding with the last value.
  3. Check that AnimatableGradientColorValue.ensureInterpolatableKeyframes() was invoked during parsing (it runs automatically for standard LottieComposition parsing); if you construct GradientColor objects outside the parser, replicate that normalization logic.

Example fix

// before — direct lerp with mismatched lengths
GradientColor gc1 = new GradientColor(pos3, colors3); // 3 stops
GradientColor gc2 = new GradientColor(pos5, colors5); // 5 stops
result.lerp(gc1, gc2, 0.5f); // throws

// after — normalize to the larger size before lerping
int maxLen = Math.max(gc1.getSize(), gc2.getSize());
GradientColor normGc1 = gc1.copyWithPositions(paddedPositions(maxLen, gc1));
GradientColor normGc2 = gc2.copyWithPositions(paddedPositions(maxLen, gc2));
result.lerp(normGc1, normGc2, 0.5f);
Defensive patterns

Strategy: validation

Validate before calling

// Validate gradient color counts before lerping
public static boolean canLerp(GradientColor gc1, GradientColor gc2) {
    return gc1.getSize() == gc2.getSize();
}

Try / catch

try {
    result.lerp(gc1, gc2, progress);
} catch (IllegalArgumentException e) {
    // Gradient stop counts differ — fall back to nearest keyframe
    result.copyFrom(progress < 0.5f ? gc1 : gc2);
}

Prevention

When it happens

Trigger: The Lottie JSON defines a gradient animation where keyframes have inconsistent numbers of gradient color stops (e.g., one keyframe has 3 colors and another has 5). This also occurs if lerp() is called directly on two GradientColor instances constructed with mismatched array lengths, bypassing the ensureInterpolatableKeyframes normalization.

Common situations: Hand-edited or programmatically generated Lottie JSON with inconsistent gradient definitions across keyframes; corrupted animation files exported from tools that don't normalize gradient stop counts; calling GradientColor.lerp() directly in custom drawing code with manually constructed GradientColor objects.

Related errors


AI-assisted analysis of airbnb/lottie-android@05ea92e903 (2026-08-14). Data as JSON: /api/errors/d18aa21099cb8001. Report an issue: GitHub.