1

I need to add my native library to Android source for others applications can use it (call functions from this library in their code). I need to use library like embed without adding it to every project in that I want use it. But i can't find information about this.

Please give me information how to do this. Sorry

4

1 回答 1

3

据我了解,您要做的是在 Android 上运行 c/c++ 代码。我对吗?

为此,您应该使用Android NDK构建您的原生库,然后将其加载到 java 代码中。

安装 ndk 后,您需要按照 4 个简单的步骤在 android 上运行本机代码:

1)为您的本机代码创建Java“包装器” - for。例如。创建名为的类MyNatives,它将保存用native关键字声明的方法。这告诉编译器,此方法的实现是在本机库中完成的。创建静态初始化程序,它将加载库。例如:

    public class MyNatives { 
        static {
            System.loadLibrary("hello-jni"); 
        }
        public void native nativeMethod(int x); 
    }

2)编译代码,并运行javah为您的本机类调用的工具(有一些 eclipsee 插件可以为您执行此操作,例如sequoyah

    cd <your project path>
    mkdir jni
    javah -d jni -classpath bin/classes com.example.MyNatives

这将在 jni 目录中生成头文件(android 项目中的所有本机代码都应该在这个目录中)

3)从生成的标头中添加方法的实现

4)为android构建系统和构建库创建makefile。Makefile 应命名为Android.mk. 它使用了一些特定的变量和宏,有关更多信息,请参阅 NDK 文档,例如:

    LOCAL_PATH := $(call my-dir)

    include $(CLEAR_VARS)
    # your library name
    LOCAL_MODULE    := hello-jni

    # all source files
    LOCAL_SRC_FILES := hello-jni.c

    include $(BUILD_SHARED_LIBRARY)

构建库只需ndk-build从项目根目录调用

有关详细信息,请参阅 Android NDK 文档。希望它有所帮助。

于 2013-08-07T06:21:05.497 回答