How to link correctly C++ files to an existing Android Project in Android Studio?

后端 未结 2 729
一个人的身影
一个人的身影 2021-01-19 08:05

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 possibi

相关标签:
2条回答
  • 2021-01-19 08:19

    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

    0 讨论(0)
  • 2021-01-19 08:41

    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 file
    set ( 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 lib
    set(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
    )
    
    0 讨论(0)
提交回复
热议问题