hwoarang69 11 Newbie Poster
What I am using:

I am using java & libgdx to create a game in desktop & android.

Error:

Could not find class 'com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator', referenced from method com.aa.GameStates.MainMenu.init

so on desktop version, everthing work fine and no errors. Problem is when I run it on android.

What I am thinking:

So I am guess android cant use this "freeTypeFontGenerator" libary but desktop version can. Is there different font libary I can use, which is for android and desktop? Bc I really wan tto use hyperspace_bold.ttf

Code:
public void init() {
    FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/hyperspace_bold.ttf"));
    titleFont = gen.generateFont(56);
    titleFont.setColor(Color.WHITE);
    font = gen.generateFont(20);
}



public void draw() {
        sb.setProjectionMatrix(MyGdxGame.camera.combined);
        sb.begin();

        // draw Title - center
        float width = titleFont.getBounds(title).width;
        titleFont.draw(sb, title, (Constants.WINDOW_WIDTH - width) / 2, 300);

        // draw menu - center
        for (int i = 0; i < menuItems.length; i++) {
            width = font.getBounds(menuItems[i]).width;
            if (currentItem == i) {
                font.setColor(Color.RED);
            } else {
                font.setColor(Color.WHITE);
            }
            font.draw(sb, menuItems[i], (Constants.WINDOW_WIDTH - width) / 2,
                    180 - 35 * i);
        }
        sb.end();
    }
JamesCherrill commented: This is a shining example of the irght way to ask a question. +15

Dani AI

Generated

Short answer: the FreeType classes live in the optional gdx‑freetype extension, and the Android build simply doesn’t have that extension packaged — so the runtime “class not found” happens on device even though desktop (which has the jar/natives available) runs fine. See the official gdx‑freetype page for details. (libgdx.com)

Fix (Gradle projects): add the freetype dependency to your core module and the freetype-platform natives to the Android module. Example dependency lines (adapt to your gdxVersion and Gradle config):

core/build.gradle:
dependencies {
  api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
  api "com.badlogicgames.gdx:gdx:$gdxVersion"
}

android/build.gradle:
dependencies {
  implementation project(':core')
  implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
  natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
  natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
  natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
  natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
}

See the libGDX dependency / freetype docs for the recommended setup. (libgdx.com)

Troubleshooting tips: make sure your TrueType file is in the project assets (Android must see it under the Android/assets path), refresh/clean the Gradle project and rebuild, and check the Android APK (or use “Analyze APK”) to confirm the freetype native jars are present. If you’re on an older non‑Gradle/Eclipse setup, add the extension jar and the platform natives into the Android project libs and export them — the same missing‑jar problem shows up in many libGDX threads. Also remember to dispose generators when done to avoid leaks. (libgdx.com)

For : the desktop working + Android failing is almost always a packaging/dependency issue rather than a font API difference. Add the gdx‑freetype extension to the project (or re-run the setup with Freetype enabled), ensure the font file is in the Android assets folder, then clean + rebuild. If problems persist, paste the Android logcat after rebuild and confirm the existence of the freetype native jars in the APK. (stackoverflow.com)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.