[FIXED] How to import FreeType to my Android Studio NDK project using CMake

Issue

Hello I am beginner in Android NDK programming and I need some help getting freetype library to work with my project. I’ve been trying for 3 hours straight to somehow import freetype to my Android Studio project. I searched on the internet and could not find any solution that was working. I downloaded the library and put it in my cpp folder of the project.But I don’t know how to include freetype. Any help would be appreaciated!

This is how my CMakeLists.txt look and for the files that I have added it works for them:

cmake_minimum_required(VERSION 3.10.2)



project("firstnative")


include_directories(stb/stb_lib
        GoldFlow/Core
        GoldFlow/Graphics
        GoldFlow/Math
        GoldFlow/glm
        GoldFlow/glm/gtc
        GoldFlow/entt
        GoldFlow/physics
        GoldFlow/scripts

    GoldFlow/freetype/include
    GoldFlow/freetype/include/freetype/
    GoldFlow/freetype/include/freetype/config
    GoldFlow/freetype/include/freetype/internal
    GoldFlow/freetype/include/freetype/internal/services
        )



add_library( 
             Native
           
             SHARED


        GoldFlow/Math/GoldMath.cpp
        GoldFlow/Graphics/Shader.cpp
        GoldFlow/Graphics/Renderer2D.cpp
        GoldFlow/Graphics/Camera.cpp
        GoldFlow/Graphics/Texture.cpp
        GoldFlow/Graphics/SpriteSheet.cpp
        GoldFlow/Core/Scene.cpp
        GoldFlow/Core/GameObject.cpp
        GoldFlow/Core/Application.cpp
        GoldFlow/Core/Controls.cpp
        GoldFlow/Core/Timer.cpp
        GoldFlow/physics/AABB.cpp
        GoldFlow/physics/Objects.cpp
        GoldFlow/scripts/ControllerScript.cpp
        GoldFlow/scripts/CharacterMovingScript.cpp
        


        native.cpp)


add_library(
    Freetype

    SHARED

    GoldFlow/freetype/src/autofit/autofit.c
    GoldFlow/freetype/src/base/ftbase.c
    GoldFlow/freetype/src/base/ftbbox.c
    GoldFlow/freetype/src/base/ftbdf.c
    GoldFlow/freetype/src/base/ftbitmap.c
    GoldFlow/freetype/src/base/ftcid.c
    GoldFlow/freetype/src/base/ftfstype.c
    GoldFlow/freetype/src/base/ftgasp.c
    GoldFlow/freetype/src/base/ftglyph.c
    GoldFlow/freetype/src/base/ftgxval.c
    GoldFlow/freetype/src/base/ftinit.c
    GoldFlow/freetype/src/base/ftmm.c
    GoldFlow/freetype/src/base/ftotval.c
    GoldFlow/freetype/src/base/ftpatent.c
    GoldFlow/freetype/src/base/ftpfr.c
    GoldFlow/freetype/src/base/ftstroke.c
    GoldFlow/freetype/src/base/ftsynth.c
    GoldFlow/freetype/src/base/fttype1.c
    GoldFlow/freetype/src/base/ftwinfnt.c
    GoldFlow/freetype/src/bdf/bdf.c
    GoldFlow/freetype/src/bzip2/ftbzip2.c
    GoldFlow/freetype/src/cache/ftcache.c
    GoldFlow/freetype/src/cff/cff.c
    GoldFlow/freetype/src/cid/type1cid.c
    GoldFlow/freetype/src/gzip/ftgzip.c
    GoldFlow/freetype/src/lzw/ftlzw.c
    GoldFlow/freetype/src/pcf/pcf.c
    GoldFlow/freetype/src/pfr/pfr.c
    GoldFlow/freetype/src/psaux/psaux.c
    GoldFlow/freetype/src/pshinter/pshinter.c
    GoldFlow/freetype/src/psnames/psnames.c
    GoldFlow/freetype/src/raster/raster.c
    GoldFlow/freetype/src/sfnt/sfnt.c
    GoldFlow/freetype/src/smooth/smooth.c
    GoldFlow/freetype/src/truetype/truetype.c
    GoldFlow/freetype/src/type1/type1.c
    GoldFlow/freetype/src/type42/type42.c
    GoldFlow/freetype/src/winfonts/winfnt.c

)

find_library( 
              log-lib
              
              log )

find_library(GLES-lib

             GLESv3)



target_link_libraries( 
                       Native
             
                       ${log-lib}
                       ${GLES-lib}
                       ${Freetype}

        )

The error I am getting now is this: C:\Users\infer\AndroidStudioProjects\FirstNative\app\src\main\cpp\GoldFlow\freetype\src\base\ftbdf.c:40:14: error: use of undeclared identifier ‘FT_ERR_PREFIXInvalid_Face_Handle’; did you mean ‘FT_Err_Invalid_Face_Handle’?

Solution

Ok the solution was very simple. All I did actually was I created directory in cpp folder named freetype and in that dir I’ve put every freetype file and just added that folder as sub directory in CMake and linked at the end and now eveyrthing works. Here is my CMake:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.
project("firstnative")

include_directories(stb/stb_lib
        GoldFlow/Core
        GoldFlow/Graphics
        GoldFlow/Math
        GoldFlow/glm
        GoldFlow/glm/gtc
        GoldFlow/entt
        GoldFlow/physics
        GoldFlow/scripts
        GoldFlow/text
        )

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        Native
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        GoldFlow/Math/GoldMath.cpp
        GoldFlow/Graphics/Shader.cpp
        GoldFlow/Graphics/Renderer2D.cpp
        GoldFlow/Graphics/Camera.cpp
        GoldFlow/Graphics/Texture.cpp
        GoldFlow/Graphics/SpriteSheet.cpp
        GoldFlow/Core/Scene.cpp
        GoldFlow/Core/GameObject.cpp
        GoldFlow/Core/Application.cpp
        GoldFlow/Core/Controls.cpp
        GoldFlow/Core/Timer.cpp
        GoldFlow/physics/AABB.cpp
        GoldFlow/physics/Objects.cpp
        GoldFlow/scripts/ControllerScript.cpp
        GoldFlow/scripts/CharacterMovingScript.cpp
        GoldFlow/text/TextRenderer.cpp
        native.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
#sd

find_library( # Sets the name of the path variable.
        log-lib
        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log )

find_library(GLES-lib
        GLESv3)

add_subdirectory(freetype)

target_link_libraries( # Specifies the target library.
        Native
        # Links the target library to the log and gl es library
        # included in the NDK.
        ${log-lib}
        ${GLES-lib}
        freetype
        )

Answered By – GoldSpark

Answer Checked By – Willingham (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published