提问人:Vetagiri Hrushikesh 提问时间:11/8/2023 更新时间:11/8/2023 访问量:19
将自定义库添加到 React Native Modules NPM 包中
Add custom library to React Native Modules NPM Package
问:
- 我使用 create-react-library 创建了一个 react native 库,其中还包括 JNI 层选项。我已经在其中编写了我所有的 c++ 逻辑。
- 我为 iOS 编译了 FFMPEG 库,并保存在一个名为 deps/ffmpeg/iOS/include 和 deps/ffmpeg/iOS/lib 的文件夹中(Deps 文件夹位于我在步骤 1 中创建的库内)
- 对于Android,我确实在CMakeLists.txt中设置了所有内容,并且它正在工作。但是当涉及到IOS时,头文件和库不包括在内,并且它总是显示未找到libavformat/avformat.h文件,我试图为libs提供绝对路径,并包含它仍然不起作用。
- 当我执行yarn add时,我无法理解链接过程是如何进行的。/multimagix-react-native-plugin
- 我在下面提供了 y pod 文件以供参考
- multimagix-react-native-plugin.podspec
require "json"
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
Pod::Spec.new do |s|
s.name = "multimagix-react-native-plugin"
s.version = package["version"]
s.summary = package["description"]
s.homepage = package["homepage"]
s.license = package["license"]
s.authors = package["author"]
s.platforms = { :ios => "11.0" }
s.source = { :git => "https://github.com/MultiMagix/multimagix-react-native-plugin.git", :tag => "#{s.version}" }
s.source_files = "ios/**/*.{h,m,mm}", "cpp/**/*.{hpp,cpp,c,h}"
# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
if respond_to?(:install_modules_dependencies, true)
install_modules_dependencies(s)
else
s.dependency "React-Core"
# Don't install the dependencies when we run `pod install` in the old architecture.
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
s.pod_target_xcconfig = {
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
}
s.dependency "React-Codegen"
s.dependency "RCT-Folly"
s.dependency "RCTRequired"
s.dependency "RCTTypeSafety"
s.dependency "ReactCommon/turbomodule/core"
end
end
end
- FFmpegLibrary.podspec
Pod::Spec.new do |spec|
spec.name = 'FFmpegLibrary'
spec.version = '1.0.0'
spec.summary = 'Custom FFmpeg library for iOS.'
spec.homepage = 'https://yourlibrarywebsite.com'
spec.license = { :type => 'Custom License', :file => 'LICENSE.txt' }
spec.author = { 'Your Name' => '[email protected]' }
spec.source = { :git => 'https://github.com/yourusername/FFmpegLibrary.git', :tag => '1.0.0' }
spec.module_name = 'FFmpegLibrary'
# Set compiler flags and other build settings as needed.
spec.compiler_flags = '-DFLAG1 -DFLAG2'
spec.pod_target_xcconfig = {
"HEADER_SEARCH_PATHS" => '"$(PODS_ROOT)/Headers/Public/FFmpegLibrary"',
"OTHER_LDFLAGS" => '"-L$(PODS_ROOT)/FFmpegLibrary"'
}
# Specify the header files to include in the Podspec.
spec.source_files = 'deps/ffmpeg/ios/include/**/*.h'
spec.vendored_libraries = [
'deps/ffmpeg/iOS/lib/libavcodec.a',
'deps/ffmpeg/iOS/lib/libavdevice.a',
'deps/ffmpeg/iOS/lib/libavfilter.a',
'deps/ffmpeg/iOS/lib/libavformat.a',
'deps/ffmpeg/iOS/lib/libavutil.a',
'deps/ffmpeg/iOS/lib/libswresample.a',
'deps/ffmpeg/iOS/lib/libswscale.a'
]
# Define the minimum iOS deployment target.
spec.platform = :ios, '12.4'
end
- React Native 项目 pod 文件
# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
'require.resolve(
"react-native/scripts/react_native_pods.rb",
{paths: [process.argv[1]]},
)', __dir__]).strip
def node_require(script)
# Resolve script with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
"require.resolve(
'#{script}',
{paths: [process.argv[1]]},
)", __dir__]).strip
end
node_require('react-native/scripts/react_native_pods.rb')
node_require('react-native-permissions/scripts/setup.rb')
platform :ios, min_ios_version_supported
prepare_react_native_project!
setup_permissions([
# 'AppTrackingTransparency',
# 'BluetoothPeripheral',
# 'Calendars',
'Camera',
# 'Contacts',
# 'FaceID',
# 'LocationAccuracy',
# 'LocationAlways',
# 'LocationWhenInUse',
'MediaLibrary',
'Microphone',
# 'Motion',
# 'Notifications',
'PhotoLibrary',
# 'PhotoLibraryAddOnly',
# 'Reminders',
# 'Siri',
# 'SpeechRecognition',
# 'StoreKit',
])
# If you are using a `react-native-flipper` your iOS build will fail when `NO_FLIPPER=1` is set.
# because `react-native-flipper` depends on (FlipperKit,...) that will be excluded
#
# To fix this you can also exclude `react-native-flipper` using a `react-native.config.js`
# ```js
# module.exports = {
# dependencies: {
# ...(process.env.NO_FLIPPER ? { 'react-native-flipper': { platforms: { ios: null } } } : {}),
# ```
flipper_config = ENV['NO_FLIPPER'] == "1" ? FlipperConfiguration.disabled : FlipperConfiguration.enabled
linkage = ENV['USE_FRAMEWORKS']
if linkage != nil
Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
use_frameworks! :linkage => linkage.to_sym
end
target 'MultiMagix' do
# pod 'multimagix-reactx-native-plugin', :path => '../node_modules/multimagix-react-native-plugin'
pod 'FFmpegLibrary', :path => '../node_modules/multimagix-react-native-plugin/FFmpegLibrary.podspec'
config = use_native_modules!
# Flags change depending on the env values.
flags = get_default_flags()
use_react_native!(
:path => config[:reactNativePath],
# Hermes is now enabled by default. Disable by setting this flag to false.
:hermes_enabled => flags[:hermes_enabled],
:fabric_enabled => flags[:fabric_enabled],
# Enables Flipper.
#
# Note that if you have use_frameworks! enabled, Flipper will not work and
# you should disable the next line.
:flipper_configuration => flipper_config,
# An absolute path to your application root.
:app_path => "#{Pod::Config.instance.installation_root}/.."
)
pod 'RNVectorIcons', :path => '../node_modules/react-native-vector-icons'
target 'MultiMagixTests' do
inherit! :complete
# Pods for testing
end
post_install do |installer|
# https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/react_native_pods.rb#L197-L202
react_native_post_install(
installer,
config[:reactNativePath],
:mac_catalyst_enabled => false
)
__apply_Xcode_12_5_M1_post_install_workaround(installer)
end
end
答: 暂无答案
评论