didi/DoKit · error · IllegalArgumentException

invalid shadow size

Error message

invalid shadow size

What it means

Thrown by setShadowSize(float, float) inside ShadowUtils' shadow drawable when either the shadow size or the max shadow size is negative. The method then rounds both values to even integers and clamps shadowSize to maxShadowSize, so the only invalid input is a negative argument. This mirrors the validation in AndroidX CardView's RoundRectDrawableWithShadow.

Source

Thrown at Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ShadowUtils.java:264

            mAddPaddingForCorners = addPaddingForCorners;
            invalidateSelf();
        }

        @Override
        public void setAlpha(int alpha) {
            super.setAlpha(alpha);
            mCornerShadowPaint.setAlpha(alpha);
            mEdgeShadowPaint.setAlpha(alpha);
        }

        @Override
        protected void onBoundsChange(Rect bounds) {
            mDirty = true;
        }

        void setShadowSize(float shadowSize, float maxShadowSize) {
            if (shadowSize < 0 || maxShadowSize < 0) {
                throw new IllegalArgumentException("invalid shadow size");
            }
            shadowSize = toEven(shadowSize);
            maxShadowSize = toEven(maxShadowSize);
            if (shadowSize > maxShadowSize) {
                shadowSize = maxShadowSize;
            }
            if (mRawShadowSize == shadowSize && mRawMaxShadowSize == maxShadowSize) {
                return;
            }
            mRawShadowSize = shadowSize;
            mRawMaxShadowSize = maxShadowSize;
            mShadowSize = Math.round(shadowSize * mShadowMultiplier);
            mMaxShadowSize = maxShadowSize;
            mDirty = true;
            invalidateSelf();
        }

        @Override

View on GitHub (pinned to 626827cddb)

Solutions

  1. Clamp inputs before calling: use Math.max(0, size) for both shadowSize and maxShadowSize.
  2. Check the dimens/dimens.xml entries that supply the shadow sizes (missing qualifiers often yield wrong values).
  3. Guard call sites that compute sizes from un-laid-out views (width/height are 0 or negative before onLayout).

Example fix

// before
shadowDrawable.setShadowSize(cornerRadius - elevation, maxRadius);

// after
shadowDrawable.setShadowSize(Math.max(0f, cornerRadius - elevation), Math.max(0f, maxRadius));
Defensive patterns

Strategy: validation

Validate before calling

float safeShadow = Math.max(0f, shadowSize);
float safeMax = Math.max(0f, maxShadowSize);
if (safeMax < safeShadow) safeMax = safeShadow;

Prevention

When it happens

Trigger: Constructing the shadow drawable or calling setShadowSize() / setMaxShadowSize() with a negative value, e.g. dimens that resolve to a negative number, or computing shadow size as a difference like (smallerDimen - largerDimen) that goes below zero.

Common situations: A dp-to-pixel dimension missing on some build flavors resolves to a negative fallback; a size computed from view measurements before layout returns a negative number; copy-pasted CardView shadow code fed with computed sizes.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/c9def75f2cb4405e. Report an issue: GitHub.