Different package versions for Android and iOS
Asked Answered
Z

3

17

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.

Ziguard answered 3/3, 2017 at 18:27 Comment(6)
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.Emulsion
@Emulsion - 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.Ziguard
@Ziguard 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?Wesson
@Wesson 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.Ziguard
having a similar issue @Ziguard -- did you ever find a solution?Benson
@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...Ziguard
W
0

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

Whorled answered 3/4, 2023 at 18:49 Comment(2)
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.Ziguard
Precisely you cand do that by writting a startup script. Something like: "git checkout androidbranch && rm -rf node_modules && yarn && yarn android"Whorled
W
0

you can remove the library and then clear everything gradle pods delete package.lock and pod lock file delete node modules and then again install the library using yarn it fixed my problem

Wilda answered 17/6 at 9:51 Comment(0)
E
-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"
Entropy answered 23/5, 2019 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.