androidcmakeoboe

Where can I find a working CMakeLists.txt for new android studio project with oboe?


I'm starting a new project with oboe. I'm using the exact same CMakeLists.txt on Github/Oboe.

To be honest, this is my first android native project. I have solid knowledge of C++, but I've never used CMake in my life. I've been trying for hours to "fix" the sample CMakeLists.txt file (Researching everywhere to understand what does every single line do), but it's exhausting as I'm like an elephant on a glassware. Every time I touch a line, the error "mutates" because of me.

My goal here is not to learn CMake (At least for now, although I'll need it for sure on the near future) but to get the lib working, so I can start getting things done.

So here's my question: Where can I find a working copy of CMakeLists.txt with oboe as a single dependency to start making noise? If it simply doesn't exists and I MUST definitely learn CMake related stuff before making any move on native programming, then any starting point or suggestion will be highly appreciated.

I already read the NDK CMake guide, and searched lots of SO questions with the error "Cannot specify link libraries for target 'xxxxxxx' which is not built by this project" but this error is rising even from the oboe CMakeLists.txt file.

Thanks in advance.


Solution

  • CMakeLists.txt is sort of a bridge for gradle to pick up all the native stuff that you want to use in your app.

    Here is my example CMakeLists.txt which is working just fine for oboe integration.

    # 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.4.1)
    
    include_directories(third_party)
    include_directories(src/main/cpp/)
    
    # 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.
            audio-native-lib
    
            # Sets the library as a shared library.
            SHARED
    
            # Provides a relative path to your source file(s).
            #${audiosupport_sources}
            # main game files
            src/main/cpp/audio-support.cpp
            src/main/cpp/main/AudioPlayer.cpp
    
            # audio engine
            src/main/cpp/audio/AAssetDataSource.cpp
            src/main/cpp/audio/Player.cpp
    
            # UI engine
            src/main/cpp/ui/OpenGLFunctions.cpp
    
            # utility functions
            src/main/cpp/utils/logging.h
            src/main/cpp/utils/UtilityFunctions.cpp
            )
    
    
    set (TARGET_LIBS log android oboe GLESv2)
    MESSAGE(STATUS "Using NDK media extractor")
    add_definitions(-DUSE_FFMPEG=0)
    target_sources( audio-native-lib PRIVATE src/main/cpp/audio/NDKExtractor.cpp )
    set (TARGET_LIBS ${TARGET_LIBS} mediandk)
    
    # Specifies libraries CMake should link to your target library. You
    # can link multiple libraries, such as libraries you define in this
    # build script, prebuilt third-party libraries, or system libraries.
    target_link_libraries( audio-native-lib ${TARGET_LIBS} )
    
    # Set the path to the Oboe directory.
    # TODO change this to where you downloaded oboe library
    set (OBOE_DIR /Users/MyUser/Documents/repos/android_audio/oboe)
    # Add the Oboe library as a subdirectory in your project.
    # add_subdirectory tells CMake to look in this directory to
    # compile oboe source files using oboe's CMake file.
    # ./oboe specifies where the compiled binaries will be stored
    add_subdirectory (${OBOE_DIR} ./oboe-bin)
    
    
    
    target_compile_options(audio-native-lib
            PRIVATE -std=c++14 -Wall -Werror "$<$<CONFIG:RELEASE>:-Ofast>")
    

    You have to define all the include directories and also link the build files and in addition to that you have to link the gradle file with this CMakeLits.txt

    externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
    

    Finally you should also be careful about files being at the right place so project structure should look like this.