{"record":{"id":"0cc96c9602151a24","repo":"material-components/material-components-android","slug":"theme-overlay-should-be-used-with-the-accompanying","errorCode":null,"errorMessage":"Theme overlay should be used with the accompanying int[] attributes.","messagePattern":"Theme overlay should be used with the accompanying int\\[\\] attributes\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/color/HarmonizedColorAttributes.java","lineNumber":91,"sourceCode":"   * would:\n   *\n   * <p>\n   *   1. look up the resources values in the theme overlay `Context`.\n   *   2. retrieve the harmonized resources with Primary.\n   *   3. replace `@color/material_harmonized_color_error`,\n   *      `@color/material_harmonized_color_on_error`, etc. with the harmonized resources.\n   *\n   * <p>That way the Error roles in the theme overlay would point to harmonized resources.\n   */\n  @NonNull\n  public static HarmonizedColorAttributes createMaterialDefaults() {\n    return create(HARMONIZED_MATERIAL_ATTRIBUTES, R.style.ThemeOverlay_Material3_HarmonizedColors);\n  }\n\n  private HarmonizedColorAttributes(\n      @NonNull @AttrRes int[] attributes, @StyleRes int themeOverlay) {\n    if (themeOverlay != 0 && attributes.length == 0) {\n      throw new IllegalArgumentException(\n          \"Theme overlay should be used with the accompanying int[] attributes.\");\n    }\n    this.attributes = attributes;\n    this.themeOverlay = themeOverlay;\n  }\n\n  /** Returns the array of color attributes for harmonization. */\n  @NonNull\n  public int[] getAttributes() {\n    return attributes;\n  }\n\n  /** Returns the custom theme overlay for harmonization, default is 0 if not specified. */\n  @StyleRes\n  public int getThemeOverlay() {\n    return themeOverlay;\n  }\n}","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/color/HarmonizedColorAttributes.java#L73-L109","documentation":"HarmonizedColorAttributes couples a theme overlay style with the set of attributes that overlay redefines. Constructing one with a non-zero themeOverlay but an empty attributes array is contradictory (an overlay that harmonizes nothing), so the constructor throws IllegalArgumentException. The public create() overloads normally prevent this; it happens with custom factory paths or reflection.","triggerScenarios":"Calling create(new int[0], R.style.MyOverlay) or directly constructing HarmonizedColorAttributes with an empty int[] and a non-zero overlay res id; building attributes dynamically from filtered lists that end up empty.","commonSituations":"Dynamically assembling harmonized attribute lists per feature and passing an empty list when the feature is disabled; copy-pasted custom builders that default to a real overlay style but derive attributes from config.","solutions":["Pass the actual attributes to harmonize alongside the overlay, or use HarmonizedColorAttributes.createMaterialDefaults().","If attributes are computed dynamically, fall back to create(attrs) with overlay=0 when the list is empty.","Never pair a themeOverlay with an empty attributes array."],"exampleFix":"// before\nval attrs = HarmonizedColorAttributes.create(intArrayOf(), R.style.ThemeOverlay_App_Harmonized) // throws\n\n// after\nval attrs = if (harmonizeList.isEmpty())\n    HarmonizedColorAttributes.createMaterialDefaults()\nelse\n    HarmonizedColorAttributes.create(harmonizeList.toIntArray(), R.style.ThemeOverlay_App_Harmonized)","handlingStrategy":"validation","validationCode":"fun buildAttributes(attrs: IntArray, @StyleRes overlay: Int): HarmonizedColorAttributes {\n  return if (overlay != 0 && attrs.isEmpty())\n    HarmonizedColorAttributes.create(attrs) // overlay 0, no contradiction\n  else\n    HarmonizedColorAttributes.create(attrs, overlay)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pair a themeOverlay with an empty attribute array.","Prefer HarmonizedColorAttributes.createMaterialDefaults() unless you truly need custom attributes.","Unit-test custom harmonization builders for the empty-list case."],"tags":["android","material-components","dynamic-color","theming","validation"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}