MuntashirAkon/AppManager · error · IllegalArgumentException

Unknown density {densityName}

Error message

Unknown density {densityName}

What it means

getDensityFromName looks up a density bucket name (e.g. "mdpi", "xhdpi") in StaticDataset.DENSITY_NAME_TO_DENSITY and throws IllegalArgumentException when the name is not a known Android density qualifier. It enforces that only recognized AAPT density names are converted to numeric density values.

Source

Thrown at app/src/main/java/io/github/muntashirakon/AppManager/apk/ApkUtils.java:320

        OsEnvironment.UserEnvironment userEnvironment = OsEnvironment.getUserEnvironment(userId);
        Path[] extDirs = userEnvironment.getExternalDirs();
        Path writableExtDir = null;
        for (Path extDir : extDirs) {
            if (extDir.canWrite() || Objects.requireNonNull(extDir.getFilePath()).startsWith("/storage/emulated")) {
                writableExtDir = extDir;
                break;
            }
        }
        if (writableExtDir == null) {
            throw new FileNotFoundException("Couldn't find any writable Obb dir");
        }
        return writableExtDir;
    }

    public static int getDensityFromName(@Nullable String densityName) {
        Integer density = StaticDataset.DENSITY_NAME_TO_DENSITY.get(densityName);
        if (density == null) {
            throw new IllegalArgumentException("Unknown density " + densityName);
        }
        return density;
    }

    @NonNull
    private static ByteBuffer getAndroidManifestFromApk(
            @NonNull List<CentralDirectoryRecord> cdRecords, @NonNull DataSource lhfSection)
            throws IOException, ApkFormatException, ZipFormatException {
        CentralDirectoryRecord androidManifestCdRecord = findCdRecord(cdRecords, MANIFEST_FILE);
        if (androidManifestCdRecord == null) {
            throw new ApkFormatException("Missing " + MANIFEST_FILE);
        }
        return ByteBuffer.wrap(LocalFileRecord.getUncompressedData(
                lhfSection, androidManifestCdRecord, lhfSection.size()));
    }

    @Nullable
    private static CentralDirectoryRecord findCdRecord(

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Normalize the input to a canonical Android density name (ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi, nodpi, tvdpi) before calling.
  2. Pre-validate with StaticDataset.DENSITY_NAME_TO_DENSITY.containsKey(name) and use a default density when absent.
  3. Wrap in try-catch for IllegalArgumentException and fall back to DisplayMetrics.DENSITY_DEFAULT.
  4. If parsing from file names, fix the extraction regex so only the density segment is passed.

Example fix

// before
int density = ApkUtils.getDensityFromName(densityName);
// after
int density = StaticDataset.DENSITY_NAME_TO_DENSITY.containsKey(densityName)
        ? ApkUtils.getDensityFromName(densityName)
        : DisplayMetrics.DENSITY_DEFAULT;
Defensive patterns

Strategy: validation

Validate before calling

if (densityName == null || !StaticDataset.DENSITY_NAME_TO_DENSITY.containsKey(densityName)) {
    densityName = "nodpi"; // or skip
}

Type guard

boolean isValidDensity(String name) {
    return name != null && StaticDataset.DENSITY_NAME_TO_DENSITY.containsKey(name);
}

Try / catch

try {
    int density = ApkUtils.getDensityFromName(name);
} catch (IllegalArgumentException e) {
    int density = DisplayMetrics.DENSITY_DEFAULT;
}

Prevention

When it happens

Trigger: Calling ApkUtils.getDensityFromName with a name absent from StaticDataset.DENSITY_NAME_TO_DENSITY — e.g. misspelled qualifiers ("hdpiar"), unknown/new density buckets, or a null/empty string extracted from a split APK name.

Common situations: Parsing config strings from APK split filenames with an unexpected format, handling vendor-specific density labels, or passing user input / locale-mixed strings instead of canonical Android density names.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/161a61d5af98eaf2. Report an issue: GitHub.