JNI初步(五)jni ndk 一个.so文件依赖另一个.so文件的写法

jin 的步骤首先参考https://blog.csdn.net/we1less/article/details/108930467

注意:本文是在ndk环境下编写

1.写native类声明native方法

package com.godv.audiosuc;

public class NativePlayers {
    static{
        System.loadLibrary("JNI_ANDROID_AUDIOS");
        System.loadLibrary("JNI_ANDROID_TEST");
    }
    //native方法
    public static native int show(String url);
    public static native String shutDown();
}

2.java-h 生成对应的jni 头文件

/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* Header for class com_godv_audiosuc_NativePlayers */

#ifndef _Included_com_godv_audiosuc_NativePlayers
#define _Included_com_godv_audiosuc_NativePlayers
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_godv_audiosuc_NativePlayers
 * Method:    show
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_godv_audiosuc_NativePlayers_show
  (JNIEnv *, jclass, jstring);

/*
 * Class:     com_godv_audiosuc_NativePlayers
 * Method:    shutDown
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_godv_audiosuc_NativePlayers_shutDown
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

3.写实现类调用另一个cpp中的方法 (这里称这个类为  方法类

#include "NativePlayers.h"
#include 
#include 
#include "Test.h"

JNIEXPORT jint JNICALL Java_com_godv_audiosuc_NativePlayers_show
        (JNIEnv * env, jclass clazz, jstring jstr)
{
    Test t;
    return t.play();
}

JNIEXPORT jstring JNICALL Java_com_godv_audiosuc_NativePlayers_shutDown
        (JNIEnv * env, jclass clazz)
{
    jstring str = env->NewStringUTF("Im godv !");
    return str;
}

3.1.方法类c++

#ifndef FFMPEG_TEST_H
#define FFMPEG_TEST_H

#include 
#include 

class Test{
public:
    int play();
};


#endif //FFMPEG_TEST_H
#include "Test.h"

int Test::play() {
    return 0;
}

4.首先将方法类封装成.so动态库

4.1android.mk文件

LOCAL_PATH := $(call my-dir)    
 
include $(CLEAR_VARS)            
LOCAL_MODULE := JNI_ANDROID_TEST
LOCAL_SRC_FILES := Test.cpp    
include $(BUILD_SHARED_LIBRARY) 

 4.2ndk-build 生成 libJNI_ANDROID_TEST.so

5.写生成jni .so文件   android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := libJNI_ANDROID_TEST
LOCAL_SRC_FILES := libJNI_ANDROID_TEST.so
include $(PREBUILT_SHARED_LIBRARY)

#LOCAL_ALLOW_UNDEFINED_SYMBOLS := true

include $(CLEAR_VARS)

LOCAL_MODULE := JNI_ANDROID_AUDIOS

LOCAL_SRC_FILES := NativePlayers.cpp

LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog

LOCAL_SHARED_LIBRARIES := libJNI_ANDROID_TEST \

include $(BUILD_SHARED_LIBRARY)

7.调用完毕

你可能感兴趣的:(jni学习,c++,jni,android,ndk)