简体   繁体   English

如何在Android中构建没有JNI的共享库?

[英]How to build a shared library without JNI in Android?

I develop an Android application, which needs to be a shared library. 我开发了一个Android应用程序,它需要是一个共享库。 I have already made the same lib in Linux (using gcc), so I want to compile the same shared lib in Android (ARM processor). 我已经在Linux中使用了相同的lib(使用gcc),所以我想在Android(ARM处理器)中编译相同的共享库。 But NDK only suports JNI. 但NDK只支持JNI。 Now I want to build a separate shared library without direct JNI interaction. 现在我想构建一个没有直接JNI交互的独立共享库。 I have a lot of C files and headers, so i cannot write JNI for everything. 我有很多C文件和标题,所以我不能为所有东西编写JNI。 I want build my library for ARM processors. 我想为ARM处理器构建我的库。 How can i do this? 我怎样才能做到这一点?

my stuture is
  ----->JNI
     ---->myfile.c(jni c code)
      ----->android.mk(here i call my two shared lib)

       folder1
             --->include
             ----src
             ---->lib(here i will get my shared lib)
       folder 2
            ----->include
            ----->src
            ----->lib(here i will get my 2nd shared lib)

Here I want to build my shared lib separately and then I will call it. 在这里,我想分别构建我的共享库,然后我会调用它。 Is it possible to build a shred lib without JNI in Android? 在Android中没有JNI可以构建一个shred库吗?

Android NDK makes no requirements for JNI compliance of the binaries built with it. Android NDK对使用它构建的二进制文件的JNI合规性没有要求。 But usually Android applications are written in Java, and when they use native components, the natural path is to use JNI. 但通常Android应用程序是用Java编写的,当它们使用本机组件时,自然路径就是使用JNI。

Android NDK provides a rich and easy to use ndk-build command, which requires you to prepare at least one Android.mk file that is used by their make system. Android NDK提供了丰富且易于使用的ndk-build命令,它要求您准备至少一个make系统使用的Android.mk文件。 In many cases it is worth the effort to prepare such file based on your makefile(s). 在许多情况下,根据您的makefile准备此类文件是值得的。 But if the project that you port from another platform uses advanced configure or Cmake features, you can skip the Android.mk completely, and use the standalone toolchain which is part of the NDK, as described in NDK official documentation. 但如果你从另一个平台端口采用先进的项目configureCmake功能,你可以跳过Android.mk完全,并使用独立的工具链,这是NDK的一部分, 描述 NDK官方文档。 Note that Android does not support full Linux glibc , but rather a subset called bionic . 请注意,Android不支持完整的Linux glibc ,而是支持bionic的子集。 The STL support is also not obvious, you have to choose one of the 7 options ! STL支持也不明显,你必须选择7个选项之一

You can stop right here, get the shared library or an executable file and use them in traditional Linux manner. 您可以在此处停止,获取共享库或可执行文件,并以传统的Linux方式使用它们。 You can build your Android application all in C++ . 您可以使用C ++构建所有Android应用程序

But the easiest approach is to build a Java Android application using Android SDK, and call some native methods via JDK. 但最简单的方法是使用Android SDK构建Java Android应用程序,并通过JDK调用一些本机方法。 You will need a wrapper .so for that, a library that exports JNI functions to Java and calls the non-JNI libraries in turn. 您将需要一个包装.so对于这一点,该出口JNI功能的Java和调用非JNI库又将库。 In Android.mk you can refer to these libraries with LOCAL_SHARED_LIBRARIES . Android.mk您可以使用LOCAL_SHARED_LIBRARIES引用这些库。 You may find the following question useful for this part: ndk build library outside main project source tree . 您可能会发现以下问题对此部分有用: 在主项目源树之外的ndk构建库

You will need to load the native libraries "manually" in your Java code. 您需要在Java代码中“手动”加载本机库。 The system loader only searches for unresolved references in /system/lib folder. 系统加载程序仅搜索/system/lib文件夹中的未解析引用。

The conventional place is the static constructor of the Java class that uses the native methods. 传统的地方是使用本机方法的Java类的静态构造函数。 The order of loading is important. 装载顺序很重要。 If libone.so depends on libtwo.so , and libjni.so depends on libone and on libtwo , you will have something like 如果libone.so依赖于libtwo.so ,而libjni.so依赖于libonelibtwo ,你会有类似的东西

{
  System.Load("two");
  System.Load("one");
  System.Load("jni");
}

You should also make sure that the Android SDK builder finds the files in libs/armeabi or libs-armeabi-v7a directory when it creates the APK file. 您还应确保Android SDK 构建器在创建APK文件时在libs/armeabilibs-armeabi-v7a目录中找到这些文件。

It is possible to build native library without writing any JNI specific C-code. 可以在不编写任何JNI特定C代码的情况下构建本机库。 You need to create Android.mk and Application.mk files for your library. 您需要为库创建Android.mkApplication.mk文件。 You have a valid makefile ready, so you can use it to make an Android.mk . 您已准备好有效的makefile ,因此您可以使用它来制作Android.mk

Application.mk can be as follows: Application.mk可以如下:

APP_OPTIM := release
APP_PLATFORM := android-7
APP_STL := gnustl_static
APP_CPPFLAGS += -frtti 
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -DANDROID
APP_ABI := armeabi-v7a
APP_MODULES := <put-the-name-of-your-library-here>

Use ndk-build to compile the library. 使用ndk-build编译库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM