Add custom library to React Native Modules NPM Package

166 Views Asked by At
  1. I created a react native library using create-react-libary with options JNI layer is also included. I have written all my c++ logic in it.
  2. I compiled FFMPEG libs for iOS and kept inside a folder called deps/ffmpeg/iOS/include and deps/ffmpeg/iOS/lib (Deps folder is inside the library which I created in step 1)
  3. For Android I did setup everything in CMakeLists.txt and it is working. But when it comes to IOS that header files and libs are not included and it is always showing that libavformat/avformat.h file not found I tried to give absolute path for libs and includes it is still not working.
  4. I am not able to understand how the linking process is going on when I execute yarn add ../multimagix-react-native-plugin
  5. Iam giving y pod files below for reference
  • 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 projects pod file
# 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

0

There are 0 best solutions below