Skip to content Skip to sidebar Skip to footer

How To Link Correctly C++ Files To An Existing Android Project In Android Studio?

Following this I'm trying to link correctly some cpp and hpp files. I'm starting by an existing project and I would to connect all toghether. I read that I have 2 possibilities: w

Solution 1:

I suggest you to use Android Studio to create a project with C++ support. You can see this article for example. If you have to build a wrapper between your Android/Kotlin code and your C++ classes, you can read this thread

Solution 2:

Use CMake, the build system is much faster. Here is a small example

app.gradle

android {
    compileSdkVersion // your SDK
    buildToolsVersion // your build tools
    defaultConfig {
        // Your config


        externalNativeBuild {
            cmake {
                arguments '-DANDROID_PLATFORM=android-15',
                        '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static',
                        '-DANDROID_CPP_FEATURES=rtti exceptions'
            }
        }
    }
    externalNativeBuild {
        cmake {
            path '../../gameSource/CMakeLists.txt'
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
            jniDebuggable true
            minifyEnabled false
        }
    }
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

# Set Src Dir, relative to this fileset ( GAME_SRC_DIR ../../../Source)

########################################################## Add Game Sources#########################################################set ( GAME_SRC
    ${GAME_SRC_DIR}/main.cpp
    ${GAME_SRC_DIR}/other.cpp
)

set ( INC_DIRS
    ${GAME_SRC_DIR}/include
    ${GAME_SRC_DIR}/Lib/firebase_cpp_sdk/include
)

# Add Include Directories
include_directories(${INC_DIRS})

# add paths to lib.a sources
link_directories(
    ${GAME_SRC_DIR}/Lib/firebase_cpp_sdk/libs/android/${ANDROID_ABI}/gnustl/
    )

# now build app's shared libset(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -Wno-potentially-evaluated-expression")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__ANDROID__ -DANDROID")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DGL_GLEXT_PROTOTYPES=1 -DIOAPI_NO_64 -DUSE_FILE32API ")

add_library(game SHARED ${GAME_SRC})


# add lib dependencies
target_link_libraries(

                      // your libXXX.a as  XXX
                      // eg libEGL.a   as  EGL
)

Post a Comment for "How To Link Correctly C++ Files To An Existing Android Project In Android Studio?"