16

I am developing an app for both Android and iOS using React Native.

One of the libraries I'm using is a bit problematic because the app only compiles correctly:

  • ...on Android when using "react-native": "0.42.0".
  • ...on iOS when using "react-native": "0.37.0".

(Neither the library itself , nor the specific versions of react-native are important for this discussion. What's important is that I seemingly need two separate versions "at the same time" - a different one for each platform.)

I'm looking for a way to have a single codebase that can compile using the relevant tools for either platform w/o any modification1. The solutions I thought of are (to somehow):

  • Specify different package versions for each mobile OS.
  • Have completely different package.json files for each platform.

However, I have no idea how either of the above can be achieved or if they're even possible. I tried adding .ios and .android as explained in the React-Native docs on platform-specific code, but npm doesn't recognize these files.

So my questions are:

  1. Are my ideas feasible, and if so - how?
  2. Is there any other ways to get the desired result?

1 In C-like code, this would've been easy with pre-processor flags.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • What is the library you are using? I really wouldn't go for having different RN versions on ios/android. The difference between them will turn out really hard to handle at some point. 0.42+ have some bugfixes your ios app is going to miss. Above this you'll have to miss out on new available features. I would go for looking into the library and making it work on 0.42, opening a PR would help as long as the maintainer is on the project. – dhorelik Mar 03 '17 at 18:58
  • @dhorelik - I appreciate your comment. The library itself is being actively developed and I'm sure that eventually this discrepancy will be resolved. My question is of a more general nature, so I specifically didn't mention the library. In this specific case the RN version needs to be different, but it can just as easily be some other dependency. I would like to have a way to resolve a scenario where different library versions are required for some reason (due to some minor functionality change etc.). I think this question could benefit others in the future more if it remains general. – Dev-iL Mar 03 '17 at 19:36
  • @Dev-iL I am facing a situation where I am using a package for android and not for iOS, how can I achieve this with single code base without disturbing iOS build? – vignesh May 14 '18 at 06:19
  • @vignesh I'm hardly an expert on the topic, so the only idea that comes to mind is to create a local copy ("version") of the package, then remove the majority of the iOS code from it - so that it complies but has no effect. – Dev-iL Dec 09 '18 at 13:34
  • having a similar issue @Dev-iL -- did you ever find a solution? – Sara Inés Calderón May 22 '19 at 19:59
  • @Sara unfortunately, no. However, I stopped lookin a long time ago, so maybe there _is_ some solution for this out there by now. Please do post it if you find one... – Dev-iL May 23 '19 at 00:32

2 Answers2

0

You can use git branches, so you can keep different versions of your package.json file.

Oscar Rico
  • 389
  • 5
  • 18
  • Thank you for your suggestion. This is one of many possible workarounds which involve changing the files somehow. Note that I specifically requested a solution that "just works" without having to change anything about the code when triggering a build for either environment. – Dev-iL Apr 04 '23 at 09:20
  • Precisely you cand do that by writting a startup script. Something like: "git checkout androidbranch && rm -rf node_modules && yarn && yarn android" – Oscar Rico Apr 04 '23 at 17:32
-1

Haven't tried it yet, but I'm going to. Someone suggested I try this: "combine npm aliases in package.json alongside of platform-specific files that Metro supports (e.g. Component.ios.js)"

Possibly something like…

{
  "package-v1": "http://example.com/package-v1",
  "package-v2": "http://example.com/package-v2",
   // or GitHub based
  "package-v1": "orgname/package#v1",
  "package-v2": "orgname/package#v2",
}

// Component.ios.js
import "package-v1"

// Component.android.js
import "package-v2"
Sara Inés Calderón
  • 1,070
  • 12
  • 22