<span id="Label3"></p><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="color: rgb(30, 78, 121);"></span></span></span><p style="margin: 0in; padding-top: 0px; padding-bottom: 0px; font-size: 16pt;"><p style="margin: 0in; padding-top: 0px; padding-bottom: 0px; font-size: 16pt;"><span lang="en-US" style="font-family: Calibri;"><span style="color: rgb(51, 51, 51); line-height: 17.5px; font-family: ‘microsoft yahei‘; font-size: 14px;">Reprint please indicate the source:</span></span></p></p><p style="margin: 0in; padding-top: 0px; padding-bottom: 0px; font-size: 14px; font-family: ‘microsoft yahei‘; line-height: 35px;"><p style="margin: 0in; padding-top: 0px; padding-bottom: 0px; font-size: 14px; font-family: ‘microsoft yahei‘; line-height: 35px;"><span lang="zh-CN" style="font-family: ‘Microsoft YaHei‘;"><span style="color: rgb(51, 51, 51); font-family: ‘microsoft yahei‘; line-height: 17.5px;">http://blog.csdn.net/developer_jiangqq/article/details/50519677<br></span></span></p></p><p style="margin: 0in; padding-top: 0px; padding-bottom: 0px; font-size: 14px; font-family: ‘microsoft yahei‘; line-height: 35px;"><p style="margin: 0in; padding-top: 0px; padding-bottom: 0px; font-size: 14px; font-family: ‘microsoft yahei‘; line-height: 35px;"><span lang="zh-CN" style="font-family: ‘Microsoft YaHei‘;"><span style="color: rgb(51, 51, 51); font-family: ‘microsoft yahei‘; line-height: 17.5px;">This article is from: "jiang Qing's blog"</span></span></p></p><br><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"> <span style="color: rgb(30, 78, 121);">(</span> <span style="color: rgb(30, 78, 121);">i</span> <span style="color: rgb(30, 78, 121);">)</span> <span style="color: rgb(30, 78, 121);"></span> preface <span style="color: rgb(30, 78, 121);"></span> </span></span><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="font-family: Calibri; line-height: 26px; color: rgb(255, 0, 0); font-size: 14.6667px;">"good news" personal website has been running, the following blog and technical dry Goods and other wonderful articles will be synchronized update, please pay attention to the collection:</span>http://www.lcode.org</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">We have explained the react Native for Android environment, IDE installation configuration and application running, debugging related knowledge points. Today we are going to talk about a very useful point of knowledge. Porting our existing native Android project to react Native.</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">Newly created React native Technical Exchange Group (<span style="color:#E84C22;">282693535</span>), Welcome to daniel, React Native technology enthusiasts to join the exchange! at the same time, the left of the blog Welcome to scan the subscription number, mobile technology dry, wonderful article technology push!</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">In react native, react is actually more concerned with the views view Layer. So react native itself supports and allows us to easily and conveniently transplant an Android native project into react Native.</span></p></p><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"> <span style="color:#1E4E79;">(</span> <span style="color:#1E4E79;">two</span> <span style="color:#1E4E79;">)</span> <span style="color:#1E4E79;">Prerequisite Preparation Work</span> <span style="color:#1E4E79;"></span> </span></span><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">①. first, We have an Android app built with gradle, which you can create directly from Android Studio. Here I create a new directory testhello, and then use Android Studio to build an Android project under that Folder. Specific as Follows:</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"></span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">②. computer must have Node. JS installed, How to install it (click to Enter)</span></p></p><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"> <span style="color:#1E4E79;">(</span> <span style="color:#1E4E79;">three</span> <span style="color:#1E4E79;">) Android</span> <span style="color:#1E4E79;">Project related configuration</span> </span></span><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">2.1 Add react native dependencies in the Build.gradle of our Android project, then sync, the code is as Follows:</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_1_3337369" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_1_3337369" name="code" class="java">Compile ' com.facebook.react:react-native:0.17.+ '</pre></pre><p><p></p></p><p><p> <span style="font-family:kaiti_gb2312;font-size:14px;"><span style="color: #E84C22;">[</span> <span style=" Color: #E84C22; "> note </span> <span style="color: #E84C22;" ". < span> <span style="color: #E84C22;"> This synchronizes the </span> <span style="color: #E84C22;">0.17 </span> <span style="color: #E84C22;"> version </span> <span style="color: #E84C22;">react native </span> <span style="color: #E84C22;" the official version remains in the < span> <span style="color: #E84C22;">0.13 </span> <span style="color: #E84C22;"> No updates, my local </span> <span style="color: #E84C22;">reactnative </span> <span style="color: #E84C22;"> is </span> <span style="color: #E84C22;">0.17 </span> <span style="color: #E84C22;"> version, So everyone here configuration must be consistent or updated with your local Version. On this subject I studied this afternoon and stepped on a lot of pits. </span> <span style="color: #E84C22;">~~ </span> <span style="color: #E84C22;"> but </span> Span style= "color: #E84C22;" >reactnative </span> <span style="color: #E84C22;"> I've got the webmaster update the version over There. </span></span></span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">2.2 Next we need to add network access to the project Androidmanifest.xml</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_2_9814574" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_2_9814574" name="code" class="java"><uses-permission android:name= "android.permission.INTERNET"/></pre></pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">This is only used in the development phase to load the finest JavaScript code from the development server, and in the official release version, if necessary, the network permissions can be Deleted.</span></p></p><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"> <span style="color:#1E4E79;">(</span> <span style="color:#1E4E79;">four</span> <span style="color:#1E4E79;">)</span> <span style="color:#1E4E79;">Adding native code</span> </span></span><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">In the mainactivity of the Android project, we need to configure the relevant code to start running the react native library. We need to create the Reactrootview and then render the react reference inside, and set it as the main view of the Activity.</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">The specific code is as Follows:</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_3_752667" name="code" class="java">public class Mainactivity extends Activity implements Defaulthardwarebackbtnhandler {privatereactrootview MREACTROOTVI ew Privatereactinstancemanager mreactinstancemanager; @Override protectedvoid onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Mreactrootview = new Reactrootview (this); Mreactinstancemanager = Reactinstancemanager.builder (). setapplication (getapplication ()). SetB Undleassetname ("index.android.bundle"). setjsmainmodulename ("index.android"). addpackage (new M Ainreactpackage ()). setusedevelopersupport (buildconfig.debug). setinitiallifecyclestate (Lifecy clestate.resumed). Build (); Mreactrootview.startreactapplication (mreactinstancemanager, "testhello", null); Setcontentview (mreactrootview); } @Override publicvoid invokedefaultonbackpressed () {super.onbackpressed (); }}</pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">Note the following code:</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_4_6466505" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_4_6466505" name="code" class="java">Mreactrootview.startreactapplication (mreactinstancemanager, "testhello", null);</pre></pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">One of the above methods the second parameter of my side using Testhello to name, notice this to and the following things to unify, specifically we continue to look down.</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">In react native, In fact FB provides us with the Reactinstancemanger to manage the activity related life cycle for us, So we need to pass some actitivty related life cycle to reactinstancemanger. The specific code OnPause () and the Onresume () method are as Follows:</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"></span></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_5_7404596" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_5_7404596" name="code" class="java">@Override protectedvoid onPause () { super.onpause (); If (mreactinstancemanager! = Null) { mreactinstancemanager.onpause ();} } @Override protectedvoid onresume () { super.onresume (); If (mreactinstancemanager! = Null) { mreactinstancemanager.onresume (this,this); } }</pre></pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">Immediately following the event that returns a button key we also need to pass:</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_6_2802790" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_6_2802790" name="code" class="java">@Override public void onbackpressed () { if (mreactinstancemanager! = Null) { mreactinstancemanager.onbackpressed (); } else { super.onbackpressed (); }}</pre></pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">This allows us to handle the JavaScript code when the user presses the return key. of course, If the front end does not handle the event of the response, it will be recalled to the Invokedefaultonbackpressed () method Above. The fallback event is then transferred to the activity, and the following is how the activity should be executed. The default event is to close the Activity.</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">The final step is to deal with the device menu, by default we can shake the phone to pop-up menu, but for the simulator, this method is not applicable, so we intercept the event method to deal with the Relevant.</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_7_7993240" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_7_7993240" name="code" class="java">@Overridepublic boolean onKeyUp (int keycode, keyevent event) { if (keycode = = Keyevent.keycode_menu && Mreactinstancemanager = Null) { mreactinstancemanager.showdevoptionsdialog (); return true; } return Super.onkeyup (keycode, event);}</pre></pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">This concludes our Android project activity and configuration files as well as the most basic configuration methods.</span></p></p><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"> <span style="color:#1E4E79;">(</span> <span style="color:#1E4E79;">five</span> <span style="color:#1E4E79;">)</span> <span style="color:#1E4E79;">add</span> <span style="color:#1E4E79;">js</span> </span></span><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">Let's take the command line and switch to the root of the project first (my example is to switch to the Testhello Directory)</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">5.1. run NPM init on the command line as Follows:</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"><br></span></p></p><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">This command creates a Package.json file and prompts us to enter some information, which is not entered by default, but the name must be in full English lowercase oh, the results are as follows:</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"><br></span></p></p><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">5.2. command line run NPM install--save react-native for installing react native module and related node module, run as Follows:</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"><br></span></p></p><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">Here we start installing the node module and the react native module, and the results will generate Node_module folders as Follows:</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"><br></span></p></p><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">5.3. finally, Run the following command:</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_8_3707079" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_8_3707079" name="code" class="java">Curl-o. flowconfig Https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig</pre></pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">Do a flow configuration</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"><br></span></p></p><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">All of the above three steps have been completed, and we have created the node module and added the react-native npm dependency. Next we need to modify the Package.json file and add the following code to the scripts tag:</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_9_9105273" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_9_9105273" name="code" class="java">"start": "node_modules/react-native/packager/packager.sh"</pre></pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">The specific changes are as Follows:</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"><br></span></p></p><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">5.4. Now we create a file named: index.android.js, and then add the following code inside:</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_10_2867264" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_10_2867264" name="code" class="java">' Use strict '; var React = require (' react-native '); var { Text, View} = React; class Myawesomeapp extends React.comp Onent { render () { return ( <view style={styles.container}> <text style={styles.hello}> Hello, world</text> </View> ) }}var styles = React.StyleSheet.create ({ container:{ flex:1, justifycontent: ' center ', }, hello: { fontsize:20, textAlign: ' Center ', margin:10, },}); React.AppRegistry.registerComponent (' testhello ', () = testhello);</pre></pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">The above completed all the relevant code and React-native configuration, Note that the above are used testhello, I hope we all use the same name and the front mainactivity to unify Oh.</span></p></p><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"> <span style="color:#1E4E79;">(</span> <span style="color:#1E4E79;">six</span> <span style="color:#1E4E79;">)</span> <span style="color:#1E4E79;">running</span> the <span style="color:#1E4E79;">APP</span> </span></span><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">The above configuration steps are complete, we want to run the app, first we need to open the development server, using the following command:</span></p></p><p><p></p></p><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_11_4294160" name="code" class="java"><pre code_snippet_id="1556601" snippet_file_name="blog_20160114_11_4294160" name="code" class="java">NPM start</pre></pre><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">The results of the operation are as Follows:</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"><br></span></p></p><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">Next is the final step, run the react-native run-android command to compile and run the application, and the result is as Follows:</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"><br></span></p></p><p><p></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">This completes the migration of a simple Android native project to Reactnative.</span></p></p><span style="font-family:KaiTi_GB2312;font-size:14px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"> <span style="color:#1E4E79;">(</span> <span style="color:#1E4E79;">seven</span> <span style="color:#1E4E79;">)</span> <span style="color:#1E4E79;">Final summary</span> </span></span><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;">The transplant of Android native project to react native was also met with many problems during the day, for example, because the official website was not kept up-to-date, and was later discovered by the mining pits. You can add a group of react native Technical Exchange Group (<span style="color:#E84C22;">282693535</span>) or the bottom of the reply to a bit.</span></p></p><p><p><span style="font-family:KaiTi_GB2312;font-size:14px;"></span> <span style="font-family: KaiTi_GB2312;font-size:14px; line-height: 26px;">Respect the original, reproduced please specify: from sky, Qing (</span><span style="color: rgb(204, 0, 0);">http://blog.csdn.net/developer_jiangqq</span><span style="font-family: KaiTi_GB2312;font-size:14px; line-height: 26px;">) infringement must investigate! </span></p></p><p style="margin-top: 0px; margin-bottom: 1.1em; padding-top: 0px; padding-bottom: 0px; line-height: 26px; box-sizing: border-box;"><p style="margin-top: 0px; margin-bottom: 1.1em; padding-top: 0px; padding-bottom: 0px; line-height: 26px; box-sizing: border-box;"><span style="font-family:KaiTi_GB2312;font-size:14px;">Follow my subscription number (codedev123), share mobile development Technology (android/ios), Project management, and blog posts every day! (welcome attention, The first time to push the wonderful Article)</span></p></p><p style="margin-top: 0px; margin-bottom: 1.1em; padding-top: 0px; padding-bottom: 0px; line-height: 26px; box-sizing: border-box;"><p style="margin-top: 0px; margin-bottom: 1.1em; padding-top: 0px; padding-bottom: 0px; line-height: 26px; box-sizing: border-box;"><span style="font-family:KaiTi_GB2312;font-size:14px;"></span></p></p><p style="margin-top: 0px; margin-bottom: 1.1em; padding-top: 0px; padding-bottom: 0px; line-height: 26px; box-sizing: border-box;"><p style="margin-top: 0px; margin-bottom: 1.1em; padding-top: 0px; padding-bottom: 0px; line-height: 26px; box-sizing: border-box;"><span style="font-family:KaiTi_GB2312;font-size:14px;">Follow my Weibo to get more content</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; line-height: 26px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; line-height: 26px;"><span style="font-family:KaiTi_GB2312;font-size:14px;"></span></p></p><p><p>"React native development" React native porting native Android projects</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service