Changing the package name in a react-native app
Asked Answered
D

5

7

I've created an app in react-native. I need the package name to be: com.org.appname

React-native does not allow you to specify this as the package name in the init, or to change it after init.

react-native init --package="com.org.appname" 

does not work

Changing the package name as described in Change package name for Android in React Native also doesn't work and results in the following error on react-native run-android

Failed to finalize session : INSTALL_FAILED_VERSION_DOWNGRADE

Debera answered 8/4, 2017 at 11:2 Comment(0)
D
19

in iOS project you just need to change BundleID with Xcode.

In Android project you need to change package name in 4 files:

android/app/src/main/java/com/reactNativeSampleApp/MainActivity.java
android/app/src/main/java/com/reactNativeSampleApp/MainApplication.java
android/app/src/main/AndroidManifest.xml
android/app/build.gradle

good practice is also to modify BUCK file in Android project, in two places, and correspondingly adjust hierarchy in android project, although it is not necessary:

BUCK file:

 android_build_config(
     name = "build_config",
     package = "app.new.name",
 )

 android_resource(
     name = "res",
     package = "app.new.name",
     res = "src/main/res",
 )

run command ./gradlew clean

Dolerite answered 23/5, 2017 at 14:59 Comment(1)
Is there an update to this? Currently only changing the BundleID with Xcode did not work with RN 0.61.Airel
H
8

According to this https://saumya.github.io/ray/articles/72/, you can run this command from the start

react-native init MyAwesomeProject -package "com.example.app"

But if you already generated your app via

react-native init MyAwesomeProject

You can modify app name in file

android/app/src/main/res/values/strings.xml

Then you can modify package name in files

android/app/src/main/java/com/reactNativeSampleApp/MainActivity.java
android/app/src/main/java/com/reactNativeSampleApp/MainApplication.java
android/app/src/main/AndroidManifest.xml ( optional as per my experience )
android/app/build.gradle

Lastly, run the following commands (from inside the app's android/ directory)

./gradlew clean
./gradlew assembleRelease

To change iOS version package name, use Xcode

Hearten answered 16/5, 2017 at 23:12 Comment(2)
where is this stuff documented? even just the cli options? crazy that so much has been put into this project, yet nothing into detailed documentation!Mickelson
the -package option is not supported, does not work and the article in the link is incorrect in other ways, suggest removing it.Mickelson
N
1

I use the react-native-rename npm package.

Install using

npm install react-native-rename -g

Then from the root of your React Native project execute the following

react-native-rename "MyApp" -b com.mycompany.myapp

react-native-rename on npm

Normannormand answered 15/12, 2018 at 15:21 Comment(1)
This did work with prior version in android, however using it with 0.71.2 wont work anymore. Already updated the package to it's new version.Freshman
C
0

If your text editor able to replace a string in all of your project files it is very easy to change package id.

I am using PhpStorm and I just replaced my old.package.id with new.package.id in all of my project files. It worked for me

Clerissa answered 31/10, 2017 at 4:3 Comment(0)
Q
0

simply just change your package name in these 4 files

android => app => src => main => java => com => reactNativeSampleApp => MainActivity.java & MainApplication.java android => app => src => main => AndroidManifest.xml android => app => build.gradle

Questionable answered 6/3, 2021 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.