简体   繁体   English

如何在Java Android Studio项目中使用.so共享库

[英]How Do You Use .so Shared Library in Java Android Studio Project

I've been working at this problem for the past couple of days. 在过去的几天里,我一直在解决这个问题。 I am trying to use live555 ( http://www.live555.com/ ), a C/C++ streaming media server, as a library in a Java Android Studio project. 我正在尝试使用live555( http://www.live555.com/ ),一个C / C ++流媒体服务器,作为Java Android Studio项目中的库。 I have been able to use ndk-build to create a set of .so files, but I cannot find any documentation for how to now utilize the library within my Java application. 我已经能够使用ndk-build来创建一组.so文件,但是我找不到任何关于如何在我的Java应用程序中使用该库的文档。 Here is my setup: 这是我的设置:

  • Windows 8.1 64-bit Windows 8.1 64位
  • Oracle JDK 1.8 Oracle JDK 1.8
  • Android Studio 1.5.1 Android Studio 1.5.1
  • Gradle 1.5.0 Gradle 1.5.0
  • Build Tools 23.0.2 构建工具23.0.2

Steps taken to get where I am thus far: 到目前为止我所采取的步骤:

  1. Create a standard Android application using Android Studio. 使用Android Studio创建标准Android应用程序。
  2. Right click on the app module in the Project explorer and choose New | 右键单击Project explorer中的app模块,然后选择New | Folder | 文件夹| JNI Folder from the context menu. 上下文菜单中的JNI文件夹。
  3. Download the live555 sources from http://www.live555.com/liveMedia/public/ selecting the live555-latest.tar.gz file. http://www.live555.com/liveMedia/public/下载live555来源,选择live555-latest.tar.gz文件。
  4. Extract the sources to the JNI folder, such that you now have a folder structure like: 将源提取到JNI文件夹,这样您现在可以拥有如下文件夹结构:
    $PROJECT_DIR $ PROJECT_DIR
    |--app | --app
    |--|--src | - | --src
    |--|--|--main | - | - | - 主
    |--|--|--|--jni | - | - | - | --jni
    |--|--|--|--|--live | - | - | - | - | --live
    where the live folder contains the live555 sources. live文件夹包含live555源的位置。
  5. Inside the jni folder above, create the Android.mk and Application.mk files. 在上面的jni文件夹中,创建Android.mkApplication.mk文件。 The Application.mk file contains: Application.mk文件包含:

     APP_OPTM := release APP_ABI := armeabi armeabi-v7a APP_PLATFORM := android-21 APP_STL := stlport_shared 

    The Android.mk file contains: Android.mk文件包含:

     LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := live555 LOCAL_SRC_FILES := \\ live\\BasicUsageEnvironment\\BasicHashTable.cpp \\ live\\BasicUsageEnvironment\\BasicTaskScheduler0.cpp \\ ...All of the rest of the .c and .cpp files LOCAL_C_INCLUDES := \\ $(LOCAL_PATH)\\live \\ $(LOCAL_PATH)\\live\\BasicUsageEnvironment\\include \\ $(LOCAL_PATH)\\live\\BasicUsageEnvironment \\ $(LOCAL_PATH)\\live\\UsageEnvironment\\include \\ $(LOCAL_PATH)\\live\\UsageEnvironment \\ $(LOCAL_PATH)\\live\\groupsock\\include \\ $(LOCAL_PATH)\\live\\groupsock \\ $(LOCAL_PATH)\\live\\liveMedia\\include \\ $(LOCAL_PATH)\\live\\liveMedia \\ LOCAL_CPPFLAGS += -fPIC -fexceptions -DXLOCALE_NOT_USED=1 -DNULL=0 -DNO_SSTREAM=1 -UIP_ADD_SOURCE_MEMBERSHIP include $(BUILD_SHARED_LIBRARY) 
  6. In my $PROJECT_DIR\\gradle.properties file, I've included the line android.useDeprecatedNdk=true 在我的$PROJECT_DIR\\gradle.properties文件中,我包含了android.useDeprecatedNdk=true

  7. In $PROJECT_DIR\\local.properties , I have: $PROJECT_DIR\\local.properties ,我有:

     ndk.dir=C\\:\\\\Users\\\\user\\\\AppData\\\\Local\\\\Android\\\\ndk sdk.dir=C\\:\\\\Users\\\\user\\\\AppData\\\\Local\\\\Android\\\\sdk 
  8. In my $PROJECT_DIR\\app\\build.gradle file I have: 在我的$PROJECT_DIR\\app\\build.gradle文件中,我有:

     apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.company.android.myapp" minSdkVersion 21 targetSdkVersion 23 versionCode 1 versionName "1.0" ndk { moduleName "live555" } sourceSets.main { jni.srcDirs = [] jniLibs.srcDir "src\\\\main\\\\libs" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' } 
  9. I then run ndk-build in the $PROJECT_DIR\\app\\src\\main\\jni folder. 然后我在$PROJECT_DIR\\app\\src\\main\\jni文件夹中运行ndk-build

The ndk-build command results in the following folder structure/file output: ndk-build命令导致以下文件夹结构/文件输出:
$PROJECT_DIR $ PROJECT_DIR
|--app | --app
|--|--src | - | --src
|--|--|--main | - | - | - 主
|--|--|--|--libs | - | - | - | --libs
|--|--|--|--|--armeabi | - | - | - | - | --armeabi
|--|--|--|--|--|--liblive555.so | - | - | - | - | - | --liblive555.so
|--|--|--|--|--|--libstlport_shared.so | - | - | - | - | - | --libstlport_shared.so
|--|--|--|--|--armeabi-v7a | - | - | - | - | --armeabi-V7A
|--|--|--|--|--|--liblive555.so | - | - | - | - | - | --liblive555.so
|--|--|--|--|--|--libstlport_shared.so | - | - | - | - | - | --libstlport_shared.so
|--|--|--|--obj | - | - | - | --obj
|--|--|--|--|--local | - | - | - | - | --local
|--|--|--|--|--|--armeabi | - | - | - | - | - | --armeabi
|--|--|--|--|--|--|--objs | - | - | - | - | - | - | --objs
|--|--|--|--|--|--|--liblive555.so | - | - | - | - | - | - | --liblive555.so
|--|--|--|--|--|--|--libstlport_shared.so | - | - | - | - | - | - | --libstlport_shared.so
|--|--|--|--|--|--armeabi-v7a | - | - | - | - | - | --armeabi-V7A
|--|--|--|--|--|--|--objs | - | - | - | - | - | - | --objs
|--|--|--|--|--|--|--liblive555.so | - | - | - | - | - | - | --liblive555.so
|--|--|--|--|--|--|--libstlport_shared.so | - | - | - | - | - | - | --libstlport_shared.so

When I build the project from the Build | 当我从Build |中构建项目时 Make Project menu option, I have a Project structure in the Android view that looks like: 创建项目菜单选项,我在Android视图中有一个项目结构,如下所示:
app 应用
|--manifests | --manifests
|--java | --java
|--jniLibs | --jniLibs
|--|--armeabi | - | --armeabi
|--|--|--liblive555.so | - | - | --liblive555.so
|--|--|--libstlport_shared.so | - | - | --libstlport_shared.so
|--|--armeabi-v7a | - | --armeabi-V7A
|--|--|--liblive555.so | - | - | --liblive555.so
|--|--|--libstlport_shared.so | - | - | --libstlport_shared.so
|--res | --res

I addedd a Live555Ndk.java file to my project from which to begin referencing the live555 library. 我在我的项目中添加了一个Live555Ndk.java文件,从该文件开始引用live555库。 It contains: 它包含:

package com.company.android.myapp;

public class Live555Ndk {

    static {
        System.loadLibrary("live555");
    }

}

However, I do not know how to start creating object instances and calling methods on the live555 library now that I have the shared objects. 但是,我不知道如何开始创建对象实例并在live555库上调用方法,因为我有共享对象。 Can anyone point me in the right direction? 谁能指出我正确的方向?

Thank you for any insight. 感谢您的任何见解。

UPDATE 02/15/2016 更新 02/15/2016

OK, I've removed the ndk {} block from my app\\build.gradle file based on the below advice of mcwise. 好的,我已根据mcwise的以下建议从app \\ build.gradle文件中删除了ndk {}块。

I think I've wrapped my mind around how this works now, but I'm still not able to reference the live555 library. 我想我现在已经开始思考它是如何工作的,但我仍然无法引用live555库。 I started a new Android project with with a single activity and corresponding layout file. 我用一个活动和相应的布局文件开始了一个新的Android项目。 I added a jniLibs folder and copied in the armeabi\\liblive555.so etc files into that folder. 我添加了一个jniLibs文件夹,并将armeabi \\ liblive555.so等文件复制到该文件夹​​中。 I added in the Live555Ndk.java file I previously mentioned with the static constructor and call to SystemloadLibrary("live555"); 我在前面提到的Live555Ndk.java文件中添加了静态构造函数并调用SystemloadLibrary("live555"); . I also added a call to public native boolean isThisMediaSession(); 我还添加了对public native boolean isThisMediaSession();的调用public native boolean isThisMediaSession(); . I then used javah to generate a header file inside a jni folder. 然后我使用javah在jni文件夹中生成头文件。 I then created the corresponding .cpp file to create the method implementation. 然后我创建了相应的.cpp文件来创建方法实现。 However, I cannot include any header file from liblive555.so. 但是,我不能包含liblive555.so中的任何头文件。 It's like Android Studio is not even aware of the existence of the live555 library. 这就像Android Studio甚至不知道live555库的存在。 So, I cannot have a line such as #include <live555/MediaSession.hh> . 所以,我不能有像#include <live555/MediaSession.hh>这样的行。 Am I missing an additional step? 我错过了一个额外的步骤吗? This Android project only has the .so files, not the sources. 这个Android项目只有.so文件,而不是源文件。 Do I need to add the live555 sources to the jni folder? 我是否需要将live555源添加到jni文件夹?

Check out the many Android NDK samples developed by Google. 查看Google开发的许多Android NDK示例。 You can find them here . 你可以在这里找到它们。

The hello-gl2 example is a good place to start. hello-gl2示例是一个很好的起点。

gl_code.cpp shows you how to create JNI code which can be called from Java. gl_code.cpp向您展示了如何创建可以从Java调用的JNI代码。 Pay particular attention to this part of the code, which defines two methods and makes them visible to the Java layer: 特别注意代码的这一部分,它定义了两个方法并使它们对Java层可见:

extern "C" {
  JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env,
    jobject obj,  jint width, jint height);
  JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, 
    jobject obj);
};

JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env, jobject obj,  jint width, jint height)
{
  setupGraphics(width, height);
}

JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, jobject obj)
{
  renderFrame();
}

GL2JNILib.java shows you how to call the methods defined in gl_code.cpp from Java. GL2JNILib.java向您展示如何从Java调用gl_code.cpp中定义的方法。

public class GL2JNILib {

  static {
    System.loadLibrary("gl2jni");
   }

  /**
   * @param width the current view width
   * @param height the current view height
   */
   public static native void init(int width, int height);
   public static native void step();
}

UPDATE: 更新:

To build a C++ module which relies on the live555 library, you'll need to add the following section to Android.mk: 要构建依赖live555库的C ++模块,您需要将以下部分添加到Android.mk:

include $(CLEAR_VARS)
LOCAL_MODULE := your_module
LOCAL_SRC_FILES := your_module_file.cpp
LOCAL_SHARED_LIBRARIES := live555
include $(BUILD_SHARED_LIBRARY)

See this documentation for reference: 请参阅此文档以供参考:

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

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