android studio从已有项目添加并使用cpp代码

Posted _less is more

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android studio从已有项目添加并使用cpp代码相关的知识,希望对你有一定的参考价值。

1、配置NDK和CMAKE

要有ndk路径

local.properities也要有

2、创建文件夹和cpp文件
可以是JNI

创建cpp

3、java里要有native方法声明
比如

//声明native方法
private native String jniTellMeWhy(String hiJni);

//使用静态代码块引入cpp
 static 
        System.loadLibrary("JniImgProc");
    

可以试试在oncreate里调用

String s = jniTellMeWhy("我是MainActivity,tell me why!");
Log.i("TAG",s);

相应的cpp文件JniImgProc.cpp

#include <jni.h>
#include <string.h>
#include <stdlib.h>

extern "C"
jstring Java_opencvproject_com_ImgProcessing_jniTellMeWhy(JNIEnv *env, jobject obj, jstring str) 
    const char *question = env->GetStringUTFChars(str, JNI_FALSE);
    char *answer = "fuck,no why!!!";
    char *data = (char *) malloc(strlen(question) + strlen(answer) + 1);
    strcpy(data, question);
    strcat(data, "JNI说:");
    strcat(data, answer);
    return env->NewStringUTF(data);

4、这里不使用ndk-build,用cmake
和cpp文件同一个目录下,创建CMakeLists.txt

# 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)

# 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.
             JniImgProc

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             JniImgProc.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# 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( # Specifies the target library.
                       JniImgProc

                       # Links the target library to the log library
                       # included in the NDK.
                       $log-lib )

5、build.gradle修改
在android里面添加如下指明CMakeLists.txt路径

externalNativeBuild 
        cmake 
            path "src/main/jni/CMakeLists.txt"
            version "3.10.2"
        
    

6、编译,便能在app/build/intermediates/cmake下找到编译好的.so
程序至此已经能够运行cpp代码

7、使用.so
理论上
把.so复制到libs里

在app的build.gradle里添加

android 

[..other things here...]

   sourceSets 
        main 
            jniLibs.srcDirs = ['libs'] // where libs is the name of the directory!
        
    

然后就能正常使用了

但我的情况是,因为我用了opencv,由于加了这个,连opencv的so也找不到了,提示要下载opencv manager

中途进行各种调试

最后测试结果是:可以把cmake注释掉

jniLibs.srcDirs = [‘libs’]当然也得加上,但是得把opencv的.so也移动到libs下,就能正常运行opencv和自己的native cpp代码

以上是关于android studio从已有项目添加并使用cpp代码的主要内容,如果未能解决你的问题,请参考以下文章

android studio NDK配置

尝试从 Android Studio 添加 Firebase 项目时出错

如何安装android studio

Android Studio:如何将列表视图中的项目添加到列表视图 onClick

怎样在androidstudio中导入gson包

android studio setup安装报错,该如何解决?