Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

android

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: write a CMake file or use ndk-build to connect them all. By trying the latter I can not write the Application.mk and Android.mk files because I do not know what I need to do.

Build Grandle code:

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"

defaultConfig {
    applicationId "com.google.android.gms.samples.vision.face.facetracker"
    minSdkVersion 9
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
       }
    }
 }

 dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   compile 'com.android.support:support-v4:24.2.0'
   compile 'com.android.support:design:24.2.0'
   compile 'com.google.android.gms:play-services-vision:9.4.0+'

EDIT: I'm trying to use ndk-build.

app.grandle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

   defaultConfig {
       applicationId "com.google.android.gms.samples.vision.face.facetracker"
       minSdkVersion 9
       targetSdkVersion 24
       versionCode 1
       versionName "1.0"


}
sourceSets.main {
    jni.srcDirs = [] //disable automatic ndk-build call
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
    commandLine "C:/Users/cvlab/AppData/Local/Android/sdk/ndk-bundle/ndk-build.cmd",
            'NDK_PROJECT_PATH=build/intermediates/ndk',
            'NDK_LIBS_OUT=src/main/jniLibs',
            'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
            'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}


buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   compile 'com.android.support:support-v4:24.2.0'
   compile 'com.android.support:design:24.2.0'
   compile 'com.google.android.gms:play-services-vision:9.4.0+'
}

Application.mk:

APP_PLATFORM := android-24
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
APP_STL := gnustl_static
APP_CPPFLAGS := -std=gnu++11 -frtti -fexceptions

ifeq ($(NDK_DEBUG),0)
  APP_CPPFLAGS += -DNDEBUG
endif

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
OPENCVROOT:= C:\Users\cvlab\Downloads\opencv-3.3.0-android-sdk

OPENCV_INSTALL_MODULES := on
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

LOCAL_MODULE := face-lib
LOCAL_SRC_FILES += $(LOCAL_PATH)/Baseline.cpp $(LOCAL_PATH)/Heartbeat.cpp $(LOCAL_PATH)/opencv.cpp $(LOCAL_PATH)/RPPG.cpp
LOCAL_LDLIBS += -llog -ldl
LOCAL_CPPFLAGS += -std=gnu++11 -frtti -fexceptions

LOCAL_MODULE := MyLibs

include $(BUILD_SHARED_LIBRARY)

Sync, make project and clean project are resulting without any error but I'm running into this error:

Process 'command 'C:/Users/cvlab/AppData/Local/Android/Sdk/ndk-bundle/ndk-build.cmd'' finished with non-zero exit value 2

What's wrong?

like image 219
DoctorWho Avatar asked Oct 16 '22 14:10

DoctorWho


2 Answers

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

like image 72
Bruno Avatar answered Nov 01 '22 13:11

Bruno


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
)
like image 36
James Poag Avatar answered Nov 01 '22 13:11

James Poag