Android studio 编译C++代码 成共享库

1、新建工程,建立工程的时候需要勾选Include C++ support, 然后选择Empty Activity.

2、在app/src/main下建立一个jni目录文件,在jni目录下建立一个test.cpp,代码如下

//
// Created by Administrator on 2018/12/21.
//
#ifndef _CPP_Test_H_
#define _CPP_Test_H_

extern "C"
{
    int addNumber(int a, int b) {
        return a + b;
    }
};
#endif

3、配置app目录下的 CMakeList.txt,  设置生成库的名字,设置C++代码文件的路径,

add_library( # Sets the name of the library.
            nativetest-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
        src/main/jni/test.cpp)
target_link_libraries( # Specifies the target library.
                       nativetest-lib

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

4、生成apk,Build-Build Apk-apk,

5、用压缩工具打开 apk包,在lib文件下找到 libnativetest-lib.so ,这就是需要的动态库(名字是 lib+设置的库的名字)

6、把库导入unity的 Plungins/Android目录下

7、简单的调用


using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

public class TestAdd : MonoBehaviour {

    [DllImport("nativetest-lib")]
    private extern static int addNumber(int a, int b);


    public Text text;


    public void show()
    {
        text.text += addNumber(300, 50).ToString();
    }


}
 

猜你喜欢

转载自blog.csdn.net/m0_37981386/article/details/85164620
今日推荐