React Natvie's official documentation for integrating with Existing Apps has taught us in detail how to integrate React Natvie into today's Android projects. I documented the process of integrating react native into existing Android on Windows, based on the steps of the official documentation.
Requirements for integration into Android projects:
- An existing Android project
- Setting up a good environment (see previous article: http://my.oschina.net/common1140/blog/550501)
First, create an Android project (this is used to simulate an existing Andorid project)
Increased dependency of react Natvie in existing Android Build.gradle files
Click Maven Central to view the latest version of React Natvie, the latest version is already 0.18.0.
Add compile ' com.facebook.react:react-native:0.18.+ ' to the Build.gradle file.
Permission to join the Androidmanifest.xml in the Access network
<uses-permission android:name= "Android.permission.INTERNET"/>
Copy the following code into the project (Remember to register the class in Androidmanifest.xml)
Public classMyreactactivityextendsActivityImplementsDefaulthardwarebackbtnhandler { Public Static voidlaunch (activity activity) {Intent Intent=NewIntent (activity, myreactactivity.class); Activity.startactivity (Intent); } PrivateReactrootview Mreactrootview; PrivateReactinstancemanager Mreactinstancemanager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Mreactrootview=NewReactrootview ( This); Mreactinstancemanager=Reactinstancemanager.builder (). Setapplication (Getapplication ()). Setbundleassetname ( c1>"Index.android.bundle"). Setjsmainmodulename ("Index.android"). Addpackage (Newmainreactpackage ()). Setusedevelopersupport (buildconfig.debug). Setinitiallifecyclestat E (lifecyclestate.resumed). build (); Mreactrootview.startreactapplication (Mreactinstancemanager,"Myawesomeapp",NULL); Setcontentview (Mreactrootview); } @Overrideprotected voidOnresume () {Super. Onresume (); if(Mreactinstancemanager! =NULL) {Mreactinstancemanager.onresume ( This, This); }} @Overrideprotected voidOnPause () {Super. OnPause (); if(Mreactinstancemanager! =NULL) {mreactinstancemanager.onpause (); }} @Override Public voidinvokedefaultonbackpressed () {Super. onbackpressed (); } @Override Public voidonbackpressed () {if(Mreactinstancemanager! =NULL) {mreactinstancemanager.onbackpressed (); } Else { Super. onbackpressed (); }} @Override Public BooleanOnKeyUp (intKeyCode, KeyEvent event) { if(KeyCode = = Keyevent.keycode_menu && Mreactinstancemanager! =NULL) {mreactinstancemanager.showdevoptionsdialog (); return true; } return Super. OnKeyUp (KeyCode, event); }}
Next, we will add JS to the existing project
Open the command line at the root of the project (switch to the root directory, hold down SHIFT, and right-click the "Open command Window Here")
Input NPM init, actually play these things out (by), at first I thought it was a mistake, and then just know it was filled (generated Package.json this file)
Witty I entered the text after the parentheses in the name, pop the following error (can not be capitalized), and then put the bracketed text all in lowercase.
Just fill in the instructions after the parentheses, and finally generate the Package.json file at the root of the project.
Last hint is this OK? Enter Yes
Then, enter NPM install--save react-native
Here to wait ...
Download finished
Next, enter Curl-o. Flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig
Enter actually eject curl is not an internal command. (Baidu Check Curl is a Linux system gadget used to download files, Windows does not have this tool)
Here we analyze the function of this command. Download the file from Https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig and the file name is. flowconfig.
Then we will use other download tools to download this file to the root directory is not OK. (Do not perform the above command)
Here I use the 360 speed browser new download task to copy the link to the download task there. (Remember that the file is. flowconfig)
Download and drop it into the root directory
Some people may ask not to be able to copy it from other projects. Flowconfig? Theoretically yes, the left side is the other items, the right is just download, at the bottom of the file can be seen the version is different. Therefore, it is recommended to download the latest. flowconfig file.
Then, input react-native start service, found an error (Package.json cannot parse).
Open Package.json to delete the test line and save it.
Run react-native start again and you can see that the service is started and the port is 8081.
Enter address in browser: http://localhost:8081/index.android.bundle?platform=android
If it can be opened, it proves that the service is no problem.
Last enter the Gradlew installdebug command to run the project.
My existing project has a minsdkversion of 15, while react Natvie 0.18.0 needs minsdkversion for 16. This will change the minsdkversion to 16 before running.
The compilation was successful.
(If you are debugging with a real machine, you can read the previous article first)
Integrating Reactnative into existing Android projects on Windows