简体   繁体   English

如何更改 React Native 中的“Bundle Identifier”?

[英]How to change the "Bundle Identifier" within React Native?

Starting a new react-native project, the xcode-project gots the bundle-identifier "org.reactjs.native.example.XYZApp".启动一个新的 react-native 项目,xcode-project 获得了 bundle-identifier “org.reactjs.native.example.XYZApp”。 XYZ is the placeholder here for my real project name. XYZ 是我真实项目名称的占位符。

Is there any way to change this bundle identifier on react-native side?有没有办法在 react-native 端更改这个包标识符? Sure, I can change it in XCode.当然,我可以在 XCode 中更改它。 But this is not safe because it can be overriden when react-native will recreate the xcode-project, which could happen at any time, as well es when rebuilding the project.但这并不安全,因为它可以在 react-native 重新创建 xcode-project 时被覆盖,这可能随时发生,在重建项目时也是如此。

Based on the react-native version you are using there are bunch of files that have to be changed on both platforms (android and ios)根据您使用的 react-native 版本,必须在两个平台(android 和 ios)上更改大量文件

List of files to edit:要编辑的文件列表:

IOS IOS

  1. [application_build_scheem] .plist (this is a dict/xml file that contains keys that are used by default if no changes added to project (react-native-cli init type of project) [application_build_scheem] .plist(这是一个 dict/xml 文件,其中包含默认使用的键,如果没有向项目添加更改(react-native-cli init 类型的项目)

ANDROID安卓

  1. strings.xml (change app_name key value) strings.xml(更改app_name键值)
  2. manifest.xml (change package attribute) manifest.xml(更改属性)
  3. build.gradle (change applicationId value) build.gradle(更改applicationId值)
  4. MainActivity.java (change java class package ) MainActivity.java(更改java类
  5. MainApplication.java (change java class package ) MainApplication.java(更改java类

It's worth mentioning that there is an an npm module ( https://www.npmjs.com/package/react-native-ci-tools ) that does all of the above;值得一提的是,有一个 npm 模块( https://www.npmjs.com/package/react-native-ci-tools )可以完成上述所有工作; use it and make life easy.使用它,让生活更轻松。

Update (6/Mar/2019)更新 (6/Mar/2019)

Update Folder Paths:更新文件夹路径:

We also need to change the folder paths to match new name -- change com.oldName to com.newname.mobile , then change folder paths from app/src/java/com/oldName/ to app/src/java/com/newname/mobile/ .我们还需要更改文件夹路径以匹配新名称——将com.oldName更改为com.newname.mobile ,然后将文件夹路径从app/src/java/com/oldName/更改为app/src/java/com/newname/mobile/

For those who want a simple way to do exactly this I create react-native-app-id, It'll basically edit the files mentioned.对于那些想要一种简单的方法来做到这一点的人,我创建了 react-native-app-id,它基本上会编辑提到的文件。 react-native-ci-tools actually forked my code to produce a now outdated react-native-app-id-fixed , which won't work with the latest react native. react-native-ci-tools 实际上分叉了我的代码以生成现在过时的 react-native-app-id-fixed ,它不适用于最新的 react native。 Mine however will..然而我的会..

https://github.com/kyle-ssg/react-native-app-id https://github.com/kyle-ssg/react-native-app-id

usage is as easy as react-native-app-id com.foo.bar使用就像react-native-app-id com.foo.bar

To update iOS bundle id -更新 iOS 捆绑包 ID -

  • From terminal or command prompt navigate to react-native project folder and then go inside iOS folder.从终端或命令提示符导航到 react-native 项目文件夹,然后进入 iOS 文件夹。
  • enter command Open ProjectName.xcodeproj输入命令打开 ProjectName.xcodeproj

This command will open the project in Xcode where you need to update bundle identifier information of the app此命令将在 Xcode 中打开您需要更新应用程序包标识符信息的项目

To update android package name follow below steps -要更新 android 包名称,请按照以下步骤操作 -

  • Update folder structure inside “android/app/src/main/java/” folder based on your new package name eg "com/companyName/appName"根据您的新包名称更新“android/app/src/main/java/”文件夹中的文件夹结构,例如“com/companyName/appName”
  • Update package name inside MainActivity.java file更新 MainActivity.java 文件中的包名
  • Update package name inside MainApplication.java file更新 MainApplication.java 文件中的包名
  • Update package name inside /android/app/src/main/AndroidManifest.xml file更新 /android/app/src/main/AndroidManifest.xml 文件中的包名
  • Update package name inside /android/app/BUCK file更新 /android/app/BUCK 文件中的包名
  • Update package name inside /android/app/build.gradle file更新 /android/app/build.gradle 文件中的包名

If you want to change bundle identifier in react-native project.如果您想在 react-native 项目中更改包标识符。 You must have to change app identifier separately for both side android and ios.您必须分别为 android 和 ios 更改应用程序标识符。

ios ios

Select root project in xcode, select General tab, You can see Bundle identifier into identity section and change your new bundle identifier for ios application在 xcode 中选择根项目,选择常规选项卡,您可以在身份部分看到捆绑标识符并更改 ios 应用程序的新捆绑标识符

android安卓

  1. Goto your AndroidManifest.xml.转到您的 AndroidManifest.xml。
  2. Just place your cursor in the package name, don't select it just place it.只需将光标放在包名称上,不要选择它,只需放置它。
  3. Then press shift+F6(window)/F6 (mac) you will get a popup window, fill new identifier and select Rename package.然后按 shift+F6(window)/F6 (mac) 你会得到一个弹出窗口,填写新标识符并选择重命名包。 It will change from everywhere in android studio.它会在 android studio 的任何地方发生变化。

Even with react-native-app-id you must move the files to the new folder即使使用 react-native-app-id,您也必须将文件移动到新文件夹

I ended using a npm task for upgrading, first upgrade then run the command for rename, move the android files to the correct path and re-link just in case我结束使用 npm 任务进行升级,首先升级然后运行重命名命令,将 android 文件移动到正确的路径并重新链接以防万一

Edit the PRODUCT_BUNDLE_IDENTIFIER associated with the project in ios/project.pbxproj .ios/project.pbxproj编辑与项目关联的PRODUCT_BUNDLE_IDENTIFIER It is much safer to use XCode.使用 XCode 安全得多。

I know that the question asked to do it without XCode, for people keen on using Xcode , when inside your react native project:我知道这个问题要求在没有 XCode 的情况下完成,对于热衷于使用 Xcode 的人,当在您的本机项目中时:

  1. xed ios Open the ios folder in xcode xed ios在xcode中打开ios文件夹
  2. Click the top left icon ( project navigator )单击左上角的图标( project navigator
  3. Click the root item in the left panel ProjectName单击左侧面板ProjectName的根ProjectName
  4. In the center view, click General在中心视图中,单击General
  5. Under identity , change the Bundle identifieridentity ,更改 Bundle 标识符

I have a more lowkey solution that I have been using.我有一个更低调的解决方案,我一直在使用。 This is not a replacement for the above solutions, but simply, one more way to do it.这不是上述解决方案的替代品,而是简单的另一种方法。 I have only tested this on iOS but I assume it would work for Android as well.我只在 iOS 上测试过这个,但我认为它也适用于 Android。

I noticed that in most cases, the project created would have a com.xyz.something which is something like this.我注意到在大多数情况下,创建的项目会有一个 com.xyz.something 是这样的。

org.reactjs.native.example.abcyourprojectname-extras

I realised, instead of checking each and every folder (and not having to use Xcode), I simply do a search for (in Visual Studio Code, with the project folder open)我意识到,而不是检查每个文件夹(并且不必使用 Xcode),我只需搜索(在 Visual Studio Code 中,打开项目文件夹)

org.reactjs.native.example

This would list all the files with the bundle identifier.这将列出所有带有包标识符的文件。 Just change it your preferred bundle identifier, and thats about it.只需将其更改为您首选的包标识符,仅此而已。 This way, even if tomorrow, there are any file changes or format changes or whatever, searching will still be consistent way of doing things.这样,即使明天有任何文件更改或格式更改或其他任何事情,搜索仍将是一致的处事方式。

(again, this is a low key solution, but it seems to work for me) (同样,这是一个低调的解决方案,但它似乎对我有用)

Here's my solution without adding some external packages.这是我的解决方案,没有添加一些外部包。 Just run npx react-native-rename <newName> -b <bundleIdentifier> inside your project root directory.只需在项目根目录中运行npx react-native-rename <newName> -b <bundleIdentifier> https://github.com/junedomingo/react-native-rename https://github.com/junedomingo/react-native-rename

Unfortunately not.不幸的是没有。 In fact we were just discussing this today in the react Native Facebook group.事实上,我们今天刚刚在 react Native Facebook 小组中讨论了这个问题。 https://m.facebook.com/groups/586400221495560?view=permalink&id=738954276240153&comment_id=739065029562411&notif_t=group_comment_reply&ref=m_notif https://m.facebook.com/groups/586400221495560?view=permalink&id=738954276240153&comment_id=739065029562411&notif_t=group_comment_reply&ref=m_notif

The best plan right now is to choose the 'd' option when upgrading to verify the files before allowing then to be overridden.现在最好的计划是在升级时选择“d”选项以验证文件,然后再允许被覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM