webrtc静态Android库无法导出JNI函数。

huangapple go评论81阅读模式
英文:

webrtc static android library can't export jni functions

问题

You can add other Java_org_webrtc_* JNI functions to your libmylibrary.so by including the necessary code in your library's source code and making sure it is properly linked with the Webrtc native library. Here are the general steps to do this:

  1. Add JNI Function Definitions: Define the additional JNI functions you want to use in your library. These definitions should match the JNI function signatures in the Webrtc native library. For example:

    extern "C" {
        // Define the JNI functions you want to use
        JNIEXPORT void JNICALL Java_org_webrtc_NewFunction(JNIEnv* env, jobject thiz);
        // Add more JNI function definitions here
    }
    
  2. Implement the JNI Functions: Implement the JNI functions in your library's source code. These implementations should match the behavior and functionality you need. For example:

    // Implement the JNI function
    JNIEXPORT void JNICALL Java_org_webrtc_NewFunction(JNIEnv* env, jobject thiz) {
        // Your implementation here
    }
    
  3. Link with libwebrtc: Ensure that your library is linked with the Webrtc native library (libwebrtc.a) during the build process. Based on the code you provided, it seems that you have already added libwebrtc to your LOCAL_STATIC_LIBRARIES. Make sure this linkage is correctly set in your Android.mk or CMakeLists.txt file.

  4. Build your Library: Build your library by running the appropriate build commands for your Android project. This can vary depending on your build system (e.g., ndk-build, Gradle, CMake).

  5. Include the JNI Functions: Once your library is successfully built, the additional Java_org_webrtc_* JNI functions will be included in your libmylibrary.so.

Please note that adding JNI functions should be done carefully to ensure they interact correctly with the rest of your library and the Webrtc native library. Make sure the function names, parameters, and return types match the Webrtc library's JNI functions and that the implementation of these functions is correct.

Also, consider handling any necessary exception handling, memory management, and error checking as required by JNI best practices.

英文:

I planning to use webrtc native in our library. The my library will be used by Android.

I've success build libwebrtc.a for android, using https://webrtc.github.io/webrtc-org/native-code/android/ link.

And I added it to Android.mk as pre-build static library.

# add webrtc
include $(CLEAR_VARS)
LOCAL_CFLAGS += -DWEBRTC_POSIX -DANDROID_BUILD
LOCAL_MODULE := webrtc
LOCAL_SRC_FILES := $(WEBRTC_ANDROID_LIBRARY)/$(TARGET_ARCH_ABI)/obj/libwebrtc.a
LOCAL_EXPORT_C_INCLUDES := $(WEBRTC_ANDROID_INCLUDE)/
include $(PREBUILT_STATIC_LIBRARY)

And my library configuration is:

LOCAL_MODULE := mylibrary
#  Release Build

LOCAL_CPPFLAGS += -std=c++20 -O2 -Os -Wall -Wpedantic  -fPIC -fvisibility=hidden -frtti -flto=full


LOCAL_CPPFLAGS += -ffunction-sections -fdata-sections
LOCAL_CFLAGS += -ffunction-sections -fdata-sections 
LOCAL_LDFLAGS += -flto=full -Wl,--gc-sections

#linking to shared and static libraries
LOCAL_STATIC_LIBRARIES := webrtc 

LOCAL_CPP_EXTENSION := .cpp .cc

LOCAL_LDLIBS := -llog -lOpenSLES

LOCAL_SRC_FILES := < a lot of my cpp files>

LOCAL_CPP_FEATURES := exceptions

include $(BUILD_SHARED_LIBRARY)

libwebrtc.a have a lot of jni functions.

$ ~/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-nm libwebrtc.a | grep Java_org_webrtc_
0000000000000000 T Java_org_webrtc_Histogram_nativeAddSample
0000000000000000 T Java_org_webrtc_Histogram_nativeCreateCounts
0000000000000000 T Java_org_webrtc_Histogram_nativeCreateEnumeration
0000000000000000 T Java_org_webrtc_NetworkMonitor_nativeNotifyConnectionTypeChanged
0000000000000000 T Java_org_webrtc_NetworkMonitor_nativeNotifyOfActiveNetworkList
0000000000000000 T Java_org_webrtc_NetworkMonitor_nativeNotifyOfNetworkConnect
0000000000000000 T Java_org_webrtc_NetworkMonitor_nativeNotifyOfNetworkDisconnect
0000000000000000 T Java_org_webrtc_NetworkMonitor_nativeNotifyOfNetworkPreference
0000000000000000 T Java_org_webrtc_JniCommon_nativeAddRef
0000000000000000 T Java_org_webrtc_JniCommon_nativeAllocateByteBuffer
0000000000000000 T Java_org_webrtc_JniCommon_nativeFreeByteBuffer
0000000000000000 T Java_org_webrtc_JniCommon_nativeReleaseRef
0000000000000000 T Java_org_webrtc_Logging_nativeEnableLogThreads
0000000000000000 T Java_org_webrtc_Logging_nativeEnableLogTimeStamps
0000000000000000 T Java_org_webrtc_Logging_nativeEnableLogToDebugOutput
0000000000000000 T Java_org_webrtc_Logging_nativeLog
0000000000000000 T Java_org_webrtc_BuiltinAudioDecoderFactoryFactory_nativeCreateBuiltinAudioDecoderFactory
0000000000000000 T Java_org_webrtc_BuiltinAudioEncoderFactoryFactory_nativeCreateBuiltinAudioEncoderFactory
0000000000000000 T Java_org_webrtc_LibaomAv1Decoder_nativeCreateDecoder
0000000000000000 T Java_org_webrtc_LibaomAv1Decoder_nativeIsSupported
0000000000000000 T Java_org_webrtc_LibaomAv1Encoder_nativeCreateEncoder
0000000000000000 T Java_org_webrtc_LibaomAv1Encoder_nativeIsSupported
0000000000000000 T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeAdaptFrame
0000000000000000 T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeAdaptOutputFormat
0000000000000000 T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeOnFrameCaptured
0000000000000000 T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeSetIsScreencast
0000000000000000 T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeSetState
0000000000000000 T Java_org_webrtc_H264Utils_nativeIsSameH264Profile
0000000000000000 T Java_org_webrtc_JavaI420Buffer_nativeCropAndScaleI420
0000000000000000 T Java_org_webrtc_NV12Buffer_nativeCropAndScale
0000000000000000 T Java_org_webrtc_NV21Buffer_nativeCropAndScale
0000000000000000 T Java_org_webrtc_TimestampAligner_nativeCreateTimestampAligner
0000000000000000 T Java_org_webrtc_TimestampAligner_nativeReleaseTimestampAligner
0000000000000000 T Java_org_webrtc_TimestampAligner_nativeRtcTimeNanos
0000000000000000 T Java_org_webrtc_TimestampAligner_nativeTranslateTimestamp
0000000000000000 T Java_org_webrtc_VideoDecoderFallback_nativeCreateDecoder
0000000000000000 T Java_org_webrtc_VideoDecoderWrapper_nativeOnDecodedFrame
0000000000000000 T Java_org_webrtc_VideoEncoderFallback_nativeCreateEncoder
0000000000000000 T Java_org_webrtc_VideoEncoderWrapper_nativeOnEncodedFrame
0000000000000000 T Java_org_webrtc_VideoTrack_nativeAddSink
0000000000000000 T Java_org_webrtc_VideoTrack_nativeFreeSink
0000000000000000 T Java_org_webrtc_VideoTrack_nativeRemoveSink
0000000000000000 T Java_org_webrtc_VideoTrack_nativeWrapSink
0000000000000000 T Java_org_webrtc_YuvHelper_nativeABGRToI420
0000000000000000 T Java_org_webrtc_YuvHelper_nativeCopyPlane
0000000000000000 T Java_org_webrtc_YuvHelper_nativeI420Copy
0000000000000000 T Java_org_webrtc_YuvHelper_nativeI420Rotate
0000000000000000 T Java_org_webrtc_YuvHelper_nativeI420ToNV12
0000000000000000 T Java_org_webrtc_LibvpxVp8Decoder_nativeCreateDecoder
0000000000000000 T Java_org_webrtc_LibvpxVp8Encoder_nativeCreateEncoder
0000000000000000 T Java_org_webrtc_LibvpxVp9Decoder_nativeCreateDecoder
0000000000000000 T Java_org_webrtc_LibvpxVp9Decoder_nativeIsSupported
0000000000000000 T Java_org_webrtc_LibvpxVp9Encoder_nativeCreateEncoder
0000000000000000 T Java_org_webrtc_LibvpxVp9Encoder_nativeIsSupported
0000000000000000 T Java_org_webrtc_audio_JavaAudioDeviceModule_nativeCreateAudioDeviceModule
0000000000000000 T Java_org_webrtc_audio_WebRtcAudioRecord_nativeCacheDirectBufferAddress
0000000000000000 T Java_org_webrtc_audio_WebRtcAudioRecord_nativeDataIsRecorded
0000000000000000 T Java_org_webrtc_audio_WebRtcAudioTrack_nativeCacheDirectBufferAddress
0000000000000000 T Java_org_webrtc_audio_WebRtcAudioTrack_nativeGetPlayoutData
0000000000000000 T Java_org_webrtc_AudioTrack_nativeSetVolume
0000000000000000 T Java_org_webrtc_CallSessionFileRotatingLogSink_nativeAddSink
0000000000000000 T Java_org_webrtc_CallSessionFileRotatingLogSink_nativeDeleteSink
0000000000000000 T Java_org_webrtc_CallSessionFileRotatingLogSink_nativeGetLogData
0000000000000000 T Java_org_webrtc_DataChannel_nativeBufferedAmount
0000000000000000 T Java_org_webrtc_DataChannel_nativeClose
0000000000000000 T Java_org_webrtc_DataChannel_nativeId
0000000000000000 T Java_org_webrtc_DataChannel_nativeLabel
0000000000000000 T Java_org_webrtc_DataChannel_nativeRegisterObserver
0000000000000000 T Java_org_webrtc_DataChannel_nativeSend
0000000000000000 T Java_org_webrtc_DataChannel_nativeState
0000000000000000 T Java_org_webrtc_DataChannel_nativeUnregisterObserver
0000000000000000 T Java_org_webrtc_DtmfSender_nativeCanInsertDtmf
0000000000000000 T Java_org_webrtc_DtmfSender_nativeDuration
0000000000000000 T Java_org_webrtc_DtmfSender_nativeInsertDtmf
0000000000000000 T Java_org_webrtc_DtmfSender_nativeInterToneGap
0000000000000000 T Java_org_webrtc_DtmfSender_nativeTones
0000000000000000 T Java_org_webrtc_MediaSource_nativeGetState
0000000000000000 T Java_org_webrtc_MediaStream_nativeAddAudioTrackToNativeStream
0000000000000000 T Java_org_webrtc_MediaStream_nativeAddVideoTrackToNativeStream
0000000000000000 T Java_org_webrtc_MediaStream_nativeGetId
0000000000000000 T Java_org_webrtc_MediaStream_nativeRemoveAudioTrack
0000000000000000 T Java_org_webrtc_MediaStream_nativeRemoveVideoTrack
0000000000000000 T Java_org_webrtc_MediaStreamTrack_nativeGetEnabled
0000000000000000 T Java_org_webrtc_MediaStreamTrack_nativeGetId
0000000000000000 T Java_org_webrtc_MediaStreamTrack_nativeGetKind
0000000000000000 T Java_org_webrtc_MediaStreamTrack_nativeGetState
0000000000000000 T Java_org_webrtc_MediaStreamTrack_nativeSetEnabled
0000000000000000 T Java_org_webrtc_PeerConnection_nativeAddIceCandidate
0000000000000000 T Java_org_webrtc_PeerConnection_nativeAddIceCandidateWithObserver
0000000000000000 T Java_org_webrtc_PeerConnection_nativeAddLocalStream
0000000000000000 T Java_org_webrtc_PeerConnection_nativeAddTrack
0000000000000000 T Java_org_webrtc_PeerConnection_nativeAddTransceiverOfType
0000000000000000 T Java_org_webrtc_PeerConnection_nativeAddTransceiverWithTrack
0000000000000000 T Java_org_webrtc_PeerConnection_nativeClose
0000000000000000 T Java_org_webrtc_PeerConnection_nativeConnectionState
0000000000000000 T Java_org_webrtc_PeerConnection_nativeCreateAnswer
0000000000000000 T Java_org_webrtc_PeerConnection_nativeCreateDataChannel
0000000000000000 T Java_org_webrtc_PeerConnection_nativeCreateOffer
0000000000000000 T Java_org_webrtc_PeerConnection_nativeCreatePeerConnectionObserver
0000000000000000 T Java_org_webrtc_PeerConnection_nativeCreateSender
0000000000000000 T Java_org_webrtc_PeerConnection_nativeFreeOwnedPeerConnection
0000000000000000 T Java_org_webrtc_PeerConnection_nativeGetCertificate
0000000000000000 T Java_org_webrtc_PeerConnection_nativeGetLocalDescription
0000000000000000 T Java_org_webrtc_PeerConnection_nativeGetNativePeerConnection
0000000000000000 T Java_org_webrtc_PeerConnection_nativeGetReceivers
0000000000000000 T Java_org_webrtc_PeerConnection_nativeGetRemoteDescription
0000000000000000 T Java_org_webrtc_PeerConnection_nativeGetSenders
0000000000000000 T Java_org_webrtc_PeerConnection_nativeGetTransceivers
0000000000000000 T Java_org_webrtc_PeerConnection_nativeIceConnectionState
0000000000000000 T Java_org_webrtc_PeerConnection_nativeIceGatheringState
0000000000000000 T Java_org_webrtc_PeerConnection_nativeNewGetStats
0000000000000000 T Java_org_webrtc_PeerConnection_nativeOldGetStats
0000000000000000 T Java_org_webrtc_PeerConnection_nativeRemoveIceCandidates
0000000000000000 T Java_org_webrtc_PeerConnection_nativeRemoveLocalStream
0000000000000000 T Java_org_webrtc_PeerConnection_nativeRemoveTrack
0000000000000000 T Java_org_webrtc_PeerConnection_nativeRestartIce
0000000000000000 T Java_org_webrtc_PeerConnection_nativeSetAudioPlayout
0000000000000000 T Java_org_webrtc_PeerConnection_nativeSetAudioRecording
0000000000000000 T Java_org_webrtc_PeerConnection_nativeSetBitrate
0000000000000000 T Java_org_webrtc_PeerConnection_nativeSetConfiguration
0000000000000000 T Java_org_webrtc_PeerConnection_nativeSetLocalDescription
0000000000000000 T Java_org_webrtc_PeerConnection_nativeSetLocalDescriptionAutomatically
0000000000000000 T Java_org_webrtc_PeerConnection_nativeSetRemoteDescription
0000000000000000 T Java_org_webrtc_PeerConnection_nativeSignalingState
0000000000000000 T Java_org_webrtc_PeerConnection_nativeStartRtcEventLog
0000000000000000 T Java_org_webrtc_PeerConnection_nativeStopRtcEventLog
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeCreateAudioSource
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeCreateAudioTrack
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeCreateLocalMediaStream
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeCreatePeerConnection
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeCreatePeerConnectionFactory
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeCreateVideoSource
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeCreateVideoTrack
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeDeleteLoggable
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeFindFieldTrialsFullName
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeFreeFactory
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeGetNativePeerConnectionFactory
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeInitializeAndroidGlobals
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeInitializeFieldTrials
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeInitializeInternalTracer
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeInjectLoggable
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativePrintStackTrace
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativePrintStackTracesOfRegisteredThreads
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeShutdownInternalTracer
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeStartAecDump
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeStartInternalTracingCapture
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeStopAecDump
0000000000000000 T Java_org_webrtc_PeerConnectionFactory_nativeStopInternalTracingCapture
0000000000000000 T Java_org_webrtc_RtcCertificatePem_nativeGenerateCertificate
0000000000000000 T Java_org_webrtc_RtpReceiver_nativeGetId
0000000000000000 T Java_org_webrtc_RtpReceiver_nativeGetParameters
0000000000000000 T Java_org_webrtc_RtpReceiver_nativeGetTrack
0000000000000000 T Java_org_webrtc_RtpReceiver_nativeSetFrameDecryptor
0000000000000000 T Java_org_webrtc_RtpReceiver_nativeSetObserver
0000000000000000 T Java_org_webrtc_RtpReceiver_nativeUnsetObserver
0000000000000000 T Java_org_webrtc_RtpSender_nativeGetDtmfSender
0000000000000000 T Java_org_webrtc_RtpSender_nativeGetId
0000000000000000 T Java_org_webrtc_RtpSender_nativeGetParameters
0000000000000000 T Java_org_webrtc_RtpSender_nativeGetStreams
0000000000000000 T Java_org_webrtc_RtpSender_nativeGetTrack
0000000000000000 T Java_org_webrtc_RtpSender_nativeSetFrameEncryptor
0000000000000000 T Java_org_webrtc_RtpSender_nativeSetParameters
0000000000000000 T Java_org_webrtc_RtpSender_nativeSetStreams
0000000000000000 T Java_org_webrtc_RtpSender_nativeSetTrack
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeCurrentDirection
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeDirection
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeGetMediaType
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeGetMid
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeGetReceiver
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeGetSender
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeSetDirection
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeStopInternal
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeStopStandard
0000000000000000 T Java_org_webrtc_RtpTransceiver_nativeStopped
0000000000000000 T Java_org_webrtc_TurnCustomizer_nativeFreeTurnCustomizer

But mylibrary.so file have a piece of this list:

home:arm64-v8a$ ~/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-nm libmylibrary.so | grep Java_org_webrtc_
00000000009e9898 T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeAdaptFrame
00000000009e9758 T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeAdaptOutputFormat
00000000009e9a68 T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeOnFrameCaptured
00000000009e9688 T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeSetIsScreencast
00000000009e969c T Java_org_webrtc_NativeAndroidVideoTrackSource_nativeSetState

Question: How can add other Java_org_webrtc_ jni functions to libmylibrary.so* ?

答案1

得分: 0

我之前已经为livekit-rust-sdk做过这个操作了,看一下这个代码

  1. 我使用llvm-readelf获取导出的JNI函数。
  2. 通过添加链接器参数将它们保留在二进制文件中:-Wl, --undefined={Java_org..},每个JNI函数一个参数。
  3. 使用链接器版本脚本将这些函数设为全局。
英文:

I already did that before for the livekit-rust-sdk, take a look at this code

  1. I'm getting the exported JNI functions using llvm-readelf
  2. Keeping them inside the binary by adding a linker argument: -Wl, --undefined={Java_org..} for each JNI function
  3. Make the functions global using the linker version script

答案2

得分: 0

这行代码解决了我的问题:将以下行添加到 Android.mk 文件中:

LOCAL_WHOLE_STATIC_LIBRARIES := webrtc

LOCAL_STATIC_LIBRARIES 中移除 webrtc

英文:

For me this line solve my problem: Add this line to Android.mk

LOCAL_WHOLE_STATIC_LIBRARIES := webrtc

Remove webrtc from LOCAL_STATIC_LIBRARIES.

huangapple
  • 本文由 发表于 2023年2月24日 01:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定