brodycj / create-react-native-module

MIT License
665 stars 52 forks source link

How can I use an external library (project.Framework) in this create-react-native-module #207

Open eshiett1995 opened 4 years ago

eshiett1995 commented 4 years ago

I am creating a react native library using this library, but my issue is how to add an external library (e.g library.framework) into it.

When I drag the library to a normal application project I see the library added to Frameworks, Libraries, and Embedded content under the "General tab" and I am able to use it perfectly.

Image - using the library in a normal application project

But when I create an react native module and try to use the library I created it the created-react-library > ios. The is no general tab, hence the library cannot be accessed.

Image - using the library in a react native create module > ios project

what is the proper way of using an external library in a react-native-create-library module?

brodycj commented 4 years ago

I would recommend adding it to the podspec file in the generated ios subdirectory.

I would like to get this documented (someday).

mateioprea commented 4 years ago

@eshiett1995 did you managed to do it? I opened a ticket last night related to this, without searching (@brodybits you can delete it, sorry).

TfADrama commented 4 years ago

I would recommend adding it to the podspec file in the generated ios subdirectory.

I would like to get this documented (someday).

How do i add an external lib to the podspec?

westd commented 4 years ago

I did it by following the example of --use-apple-networking when creating the library. Edit the spec, add your dependency immediately after creating, then run yarn install and pod install in the example directory

brian-chung-dragon commented 4 years ago

@westd I have the same problem and couldn't solve. So I tried to add the dependency in the pod spec, but I couldn't run pod isntall because the pod file is missing in the example folder. How did you solve this problem?

TfADrama commented 4 years ago

I used this but still not working. I'm still trying.

  s.frameworks = "GemiusSDK"
  s.vendored_frameworks = 'GemiusSDK.framework'
  s.preserve_paths      = 'GemiusSDK/GemiusSDK.framework'
  s.xcconfig            = { 'FRAMEWORK_SEARCH_PATHS' => '$(SRCROOT)/GemiusSDK/' }
  s.public_header_files = 'GemiusSDK.framework/Headers/*.h', 'SourceCode/*.h'
TfADrama commented 4 years ago

Okey, i removed everything and started again and now it works. It was easy after all. I removed all the references to te project on my external lib and in the podspec i just added this line:

s.vendored_frameworks = 'ios/GemiusSDK.framework'

Pod install, execute main project and now there is no error and everything works.

westd commented 4 years ago

Yes it only worked for me after I started from scratch and made the changes to the pod spec before running any commands. Adding a dependency after building the project once, I haven't figured it out. Hopefully I won't need any more dependencies :)

westd commented 4 years ago

@westd I have the same problem and couldn't solve. So I tried to add the dependency in the pod spec, but I couldn't run pod isntall because the pod file is missing in the example folder. How did you solve this problem?

@brian-chung-dragon when generating the module i set the react native version to 0.61 - this has the effect of creating a pod file in the example project (you don't add your dependency to that pod file though, you add it to the podspec in the root directory)

zabojad commented 4 years ago

@TfADrama @westd do you know if there is any way to tell in the podspec that we want to embed the framework?

TfADrama commented 4 years ago

@TfADrama @westd do you know if there is any way to tell in the podspec that we want to embed the framework?

  s.dependency "React" --> This is a pod
  s.dependency "ReachabilitySwift" --> This is a pod
  s.vendored_frameworks = 'ios/PSReader.xcframework', 'ios/ZIPFoundation.xcframework' --> These are .frameworks or .xcframeworks

The above example has frameworks from pods and from downloaded frameworks. With the downloaded framework i just copied the .framework file to my ios folder.

mateioprea commented 4 years ago

@zabojad here's an example: https://github.com/mateioprea/react-native-intercom/blob/master/react-native-intercom.podspec

zabojad commented 4 years ago

@TfADrama @westd Guys, I use s.vendored_frameworks = ... myself and it works but I was wondering if it also "embeds" the framework, ie: put it in the "Embeds framework" list of the root project:

Screenshot 2020-10-21 at 17 49 07
pgoley commented 3 years ago

This approach of using vendored_frameworks in the Podspec works great however I also need to support clients linking my library the old way directly in Xcode. Getting all sorts of linker errors when trying to embed the framework this way. Has anyone had any luck with embedding a framework in your library and linking directly in Xcode without using Cocoapods?

vokhuyetOz commented 2 years ago

it should be closed, the above solution is vendored_frameworks

dnelsonlivecurrence commented 1 year ago

I was able to add the following to my podspec to get the native module I was working with imported:

iOS

For the XCFramework: s.vendored_frameworks = 'ios/MiSnapSDK/*.{xcframework}'

For the needed resources (in this case there were multiple language resources and I had to make sure to import only English): s.resources = "ios/MiSnapSDK/MiSnapUX/**/*.{png,jpeg,jpg,storyboard,xib,xcassets}","ios/MiSnapSDK/MiSnapUX/UX_Resources/en.lproj/MiSnapSDKLocalizable.strings"

And I modified the source files to only import the bridge code, and the .h / .m files that weren't imported with the vendor frameworks: s.source_files = "ios/*.{h,m,mm}","ios/MiSnapSDK/MiSnapUX/UX_Files/*.{h,m,mm}","ios/MiSnapSDK/MiSnapUX/UX2_Files/*.{h,m,mm}"

When I used the default ios/**/*.{h,m,mm} I was getting build errors about duplicate files Error: Multiple commands produce

Android

Under dependencies in the build.gradle I added the associated .aar files like so:

  compileOnly files('libs/api-release.aar')
  compileOnly files('libs/barcode-release.aar')
  compileOnly files('libs/barcodecontroller-release.aar')
  compileOnly files('libs/misnapcamera-release.aar')
  compileOnly files('libs/mibidata-release.aar')
  compileOnly files('libs/imageutils-release.aar')
  compileOnly files('libs/misnapscience-release.aar')
  compileOnly files('libs/misnapworkflow_UX2-release.aar')
  compileOnly files('libs/misnapcontroller-release.aar')
  compileOnly files('libs/misnaphybridcontroller-release.aar')
  compileOnly files('libs/misnapdetector-release.aar')
  compileOnly files('libs/misnapextractioncontroller-release.aar')

And then in the project's app/build.gradle I implemented the .aar files with implementation files('../../node_modules/react-native-misnap/android/libs/api-release.aar') for each file.