37

I have messed the files up in the Android folder and would like to rebuild the Android project inside of the Android folder. How do I do this?

Is it safe to delete the Android folder and then run react-native run-android, will that make it again?

Everything I've coded was wrote in index.android.js.

User
  • 23,729
  • 38
  • 124
  • 207
  • You may try `cd android; ./gradlew clean` however if you delete android folder and try to run, you will see an error like, "Did you mean to run in an React Native project folder?" The worst case is creating a new project and transferring the files if clean won't work. – eden Apr 05 '17 at 09:03

10 Answers10

35

The solution if ur version of react-native >= 0.61.0 :

  1. sudo rm -rf android/ ios/

  2. yarn add react-native-eject

  3. npx react-native eject

enjoy !

Don Branson
  • 13,631
  • 10
  • 59
  • 101
Jeremy HOFFMANN
  • 1,230
  • 1
  • 11
  • 21
33

EDIT: Original answer was written in 2017 (RN v.0.47?). React-native environment evolved and AFAIK this solution is no longer valid.

Probably safe to remove.


I had similar problem, and ./gradlew clean didn't work, but i found hacky solution from How can I regenerate ios folder in React Native project?

Delete android folder (probably ios too), and then hacky part ->:

react-native upgrade 

(it regenerates folders, but it shouldn't be used to upgrade RN..)

And then

react-native link
react-native run-android

It worked for me :)

EDIT : one observation -> react-native sometimes goes crazy and cannot run some commands. Launching a new terminal helps. Bizarre :)

3176243
  • 561
  • 4
  • 7
  • 3
    I don't get it. This comment `react-native upgrade` is literally meant to upgrade RN, how do you come to the conclusion of "(it regenerates folders, but it shouldn;t be used to upgrade RN..)"? Maybe this answer is outdated. – hannojg Jan 17 '20 at 20:21
  • 1
    Not working for me 2022. – FabioDev Jan 06 '22 at 11:20
25

Try to do this:

cd android
gradlew clean 
cd ..
react-native run-android
Yur D
  • 484
  • 1
  • 5
  • 9
13

Here my solution: (I'm doing this for android)

First command:(Navigation to the Android folder of your react-native project)

cd Android

Second command:

./gradlew clean

Third commmand:

./gradlew cleanBuildCache

I'm not fan of deleting android folder. (I think this not a best approaches)

To generate apk file for production: (First one from /Android)(second one from root project)

./gradlew bundleRelease
react-native run-android --variant=release

Happy coding!!!

soywod
  • 4,377
  • 3
  • 26
  • 47
Md. A. Barik
  • 429
  • 5
  • 12
8

Running following commands should do the trick:

sudo rm -rf android/ ios/ #Delete Android and ios folders first...
react-native eject
react-native upgrade //rebuilds android/ios folders
react-native link
react-native run-android
react-native run-ios
Dean Meehan
  • 2,511
  • 22
  • 36
meta4
  • 788
  • 1
  • 10
  • 24
5

EXPO users: If you've used Expo then you can run expo run:android

If the folder is malformed (or not present) it will clear and rebuild.

HungryArthur
  • 1,037
  • 11
  • 18
  • Seems it's rebuilding it, yes, if missing, but not always updating the AndroidManifest.xml with the new numbers (version and code) I've put in app.json – Marco Bresciani Jan 19 '22 at 16:42
4

Below command will create the android and ios folders.

react-native eject
slfan
  • 8,950
  • 115
  • 65
  • 78
  • 1
    For some reason `react-native upgrade` wasn't rebuilding android/ios dirs for me but this did. – DjH Mar 28 '19 at 14:15
  • 2
    this is only true when using expo with react-native. – der_michael Dec 02 '19 at 22:14
  • 1
    you have to install the `react-native` library first by using the following command `yarn add react-native-eject` Then you could do `react-native eject` This will recreate your folders – Jonathan Sanchez Nov 24 '20 at 01:00
1

Yes, it is safe to delete the Android and IOS folder and you can make it again by following steps:

1.First Method: react-native eject <=0.59.0

React-native had this command in the previous version. Running this command will check if the ios and android directories exist and then rebuild whichever one is missing.

sudo rm -rf android/ ios/

react-native eject // Will create new Android and IOS folder.

react-native link

react-native run-android

2.Second Method: react-native upgrade --legacy true>=0.60.0

Now you should use the react-native upgrade --legacy true command to back up your android folders or ios as the case may be as this command will replace your files.

Ravi Sharma
  • 507
  • 6
  • 21
0

Funny solution here Create a new react native app and copy the new android folder over and then run $react-native run-android

  • 1
    Can you please add the solution to your answer instead of only linking to it? In the (unlikely) case of your source going down, the answer would still be here. – Tvde1 Nov 19 '19 at 15:49
  • Wow, you are right. It's not even there any more. But I did mention what it said. Except anyone trying this, create the new project with the same name as your project(just rename your original project folder to a temporary name, and change it back when you're done), so it doesn't run into problems later. – N Nasiriyani Dec 03 '19 at 14:52
-1

In 2022 January: I am using windows. Inside my VS code terminal:

rm android/   

But to delete Android and ios folders first you do "rm android/ ios/" It may warn you of your action, accept.

Next:

npm run android //rebuilds android/ios folders
npm run ios //rebuilds android/ios folders
AnatuGreen
  • 579
  • 7
  • 14