简体   繁体   中英

Changing the package name in a react-native app

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

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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