如何将C ++文件正确链接到Android Studio中的现有Android项目? [英] How to link correctly C++ files to an existing Android Project in Android Studio?

查看:97
本文介绍了如何将C ++文件正确链接到Android Studio中的现有Android项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照,我正在尝试正确链接一些cpp和hpp文件.我从一个现有项目开始,我想连接在一起.我读到我有2种可能性:编写CMake文件或使用ndk-build将它们全部连接起来.通过尝试后者,我无法编写Application.mk和Android.mk文件,因为我不知道该怎么做.

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.

构建Grandle代码:

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+'

我正在尝试使用ndk-build.

I'm trying to use ndk-build.

app.grandle:

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:

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:

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:

处理'命令'C:/Users/cvlab/AppData/Local/Android/Sdk/ndk-bundle/ndk-build.cmd'结束时返回非零退出值2

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

怎么了?

推荐答案

我建议您使用Android Studio创建具有C ++支持的项目.您可以看到本文.如果您必须在Android/Kotlin代码和C ++类之间构建包装器,则可以阅读

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

这篇关于如何将C ++文件正确链接到Android Studio中的现有Android项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆