自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

史上最詳細(xì)的Android原生APP中添加ReactNative進(jìn)行混合開發(fā)教程

移動(dòng)開發(fā) Android
混合開發(fā)已經(jīng)是未來的一種趨勢(shì),混合開發(fā)中使用的技術(shù)很多,不外乎Html5、JS框架通過一定的技術(shù)和原始交互,目前主流混合開發(fā)React Native、Cordova、APICloud、MUI等等,目前網(wǎng)上寫教程的人很多,但是React Native更新速度很快,根據(jù)他們的教程,中間會(huì)遇到各種問題,今天我和大家一起踩踩各種坑,讓小伙伴們快速集成到自己的APP中。

背景

React Native出來已經(jīng)一段時(shí)間了,相對(duì)來說也算穩(wěn)定了,在很多的企業(yè)中都實(shí)際使用他們,混合開發(fā)已經(jīng)是未來的一種趨勢(shì),混合開發(fā)中使用的技術(shù)很多,不外乎Html5、JS框架通過一定的技術(shù)和原始交互,目前主流混合開發(fā)React Native、Cordova、APICloud、MUI、AppCan、Sencha Touch、jQuery Mobile等等(其他的小伙伴們自己收集),目前網(wǎng)上寫教程的人很多,但是React Native更新速度很快,根據(jù)他們的教程,中間會(huì)遇到各種問題,今天我和大家一起踩踩各種坑,讓小伙伴們快速集成到自己的APP中。

集成的小伙伴一定要注意圖片中標(biāo)注和備注哦,不然有可能會(huì)走彎路哦

集成步驟

參考官方文檔->react native 文檔

本文使用開發(fā)環(huán)境 Android studio

注意***的React Native支持的***的SDK為16(android4.1)

npm環(huán)境,小伙伴們自己安裝 nodeJS自己安裝,可以參考官方文檔安裝環(huán)境,有問題可以發(fā)411437734@qq.com溝通

創(chuàng)建Android項(xiàng)目(已有項(xiàng)目跳過)

1.打開Android studio   

打開Android studio 

2.輸入項(xiàng)目名稱,選擇項(xiàng)目目錄,點(diǎn)擊next   

輸入項(xiàng)目名稱    

選擇項(xiàng)目目錄,點(diǎn)擊next    

    

 

至此項(xiàng)目創(chuàng)建完成(需要注意的是***的React Native支持***SDK版本為16 android4.1)

React Native集成到上面我們創(chuàng)建的ReactNativeAPPDemo中

參考Facebook react native文檔

1.進(jìn)入項(xiàng)目根目錄,添加JS到項(xiàng)目中-點(diǎn)擊Android studio中的Terminal(如下圖)   

進(jìn)入項(xiàng)目根目錄,添加JS到項(xiàng)目中-點(diǎn)擊Android studio中的Terminal 

分別執(zhí)行一下語句

  1. npm init 
  2. npm install --save react react-native 
  3. curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig  

init 主要根據(jù)提醒生成package.json文件

install --save react react-native 安裝React 和React Native

curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig 下載.flowconfig文件

參考圖片   

 

查看項(xiàng)目中有node_modules,說明react和react native 安裝完成   

查看項(xiàng)目中有node_modules 

在項(xiàng)目根目錄添加.flowconfig

參考下圖   

在項(xiàng)目根目錄添加.flowconfig 

也可以手動(dòng)創(chuàng)建在瀏覽器打開https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig網(wǎng)址復(fù)制內(nèi)容創(chuàng)建文件   

 

ReactNativeAppDemo配置相關(guān)內(nèi)容

1.添加"start": "node node_modules/react-native/local-cli/cli.js start" 到package.json 文件下 scripts標(biāo)簽 修改前   

 

修改后   

 

2.添加index.android.js文件到項(xiàng)目中   

添加index.android.js文件到項(xiàng)目中   

  1. 'use strict'; import React from 'react'; import { 
  2.   AppRegistry, 
  3.   StyleSheet, 
  4.   Text, 
  5.   View 
  6. from 'react-native'; class HelloWorld extends React.Component { 
  7.   render() {     return (       <View style={styles.container}> 
  8.         <Text style={styles.hello}>Hello, World</Text> 
  9.       </View
  10.     ) 
  11.   } 
  12. } var styles = StyleSheet.create({ 
  13.   container: { 
  14.     flex: 1, 
  15.     justifyContent: 'center'
  16.   }, 
  17.   hello: { 
  18.     fontSize: 20, 
  19.     textAlign: 'center'
  20.     margin: 10, 
  21.   }, 
  22. }); 
  23. AppRegistry.registerComponent('HelloWorld', () => HelloWorld);  

至此React native配置基本完成

3.App build.gradle配置

  1. dependencies { 
  2. ... 
  3. compile "com.facebook.react:react-native:+" // From node_modules.}  

這里注意不要使用maven中的,因?yàn)槲覀兪褂玫氖俏覀儽镜豱ode_modules中的,注意***版本中支持的是23,appcompat-v7:23.0.1,暫時(shí)沒有試24的api   

 

整個(gè)工程build.gradle配置

  1. allprojects { 
  2. repositories { 
  3.     ... 
  4.     maven {         // All of React Native (JS, Android binaries) is installed from npm 
  5.         url "$rootDir/node_modules/react-native/android" 
  6.     } 
  7. ... 
  8.    

 

添加<uses-permission android:name="android.permission.INTERNET" />到AndroidManifest.xml:

確定External Libraries   

    

 

確定是下是***的,例如確定是0.34.1而不是0.20.1,如果出現(xiàn)請(qǐng)檢查 

  1. maven { 
  2.            `url "$rootDir/../node_modules/react-native/android"`//地址是否正確 
  3.        }  
  4. 修改url "$rootDir*/..*/node_modules/react-native/android"為url "$rootDir/node_modules/react-native/android"  

添加native code

官方給出的   

 

到時(shí)***版本中提供了更加簡(jiǎn)單的方式,沒錯(cuò)就是繼承。   

 

在這里我們也需要自定義一個(gè)Application否則 運(yùn)行時(shí)會(huì)報(bào)錯(cuò),不信的小伙伴可以試一試   

    

 

到此為止,ReactNative 集成到已有項(xiàng)目中完成!!!迫不及待的運(yùn)行試試吧!!   

 

在Terminal中運(yùn)行 npm start,看到下圖表示啟動(dòng)成功   

 

運(yùn)行以后發(fā)現(xiàn)如下錯(cuò)誤   

 

react-native報(bào)錯(cuò):Could not get BatchedBridge, make sure your bundle is packaged correctly

莫緊張,這是因?yàn)閎undle沒有打包好。找不到編譯打包后的js文件。其實(shí)就是android studio默認(rèn)的尋找js文件地址和react-native自己的工具編譯所使用的地址不同。

解決方法

方法一

進(jìn)入項(xiàng)目,在android/app/src/main下新建assets目錄。執(zhí)行以下命令

  1. $> react-native start > /dev/null 2>&1 &   
  2. $> curl "http://localhost:8081/index.android.bundle?platform=android" -o 
  3. "app/src/main/assets/index.android.bundle"  

在項(xiàng)目根目錄下執(zhí)行

  1. <!--$> (cd android/ && ./gradlew assembleDebug)-->$> (cd 項(xiàng)目名稱/ && ./gradlew assembleDebug) 

把創(chuàng)建的apk安裝到android設(shè)備

方法二

進(jìn)入項(xiàng)目,在android/app/src/main下新建assets目錄

啟動(dòng)服務(wù)器

  1. $>react-native start 
  2. $>react-native bundle --platform android --dev false --entry-file index.android.js --bundl  

在assets文件夾下會(huì)生成index.android.bundle文件,把創(chuàng)建的apk文件安裝到android設(shè)備

方法三

進(jìn)入項(xiàng)目,在android/app/src/main下新建assets目錄

在package.json中配置下面代碼

  1. "scripts": { 
  2.     "start""node node_modules/react-native/local-cli/cli.js start"
  3.     "bundle-android""react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output app/src/main/assets/index.android.bundle --sourcemap-output app/src/main/assets/index.android.map --assets-dest android/app/src/main/res/" 
  4.   }, 

 個(gè)人推薦使用方法三,方法三解決不了推薦方法二 手動(dòng)執(zhí)行

修改剛剛的package.json文件  

 

 

如果真機(jī)(模擬器)需要執(zhí)行

  1. adb reverse tcp:8081 tcp:8081 

一定要確定連接網(wǎng)絡(luò)哦!! 

 

  

 

 

 

開心的進(jìn)行開發(fā)吧!

其他可能遇到的問題

ReactNative兼容64位Android手機(jī)

libgnustl_shared.so" is 32-bit instead of 64-bit類似錯(cuò)誤

解決方法

  1. 在項(xiàng)目的根目錄的 gradle.properties 里面添加一行android.useDeprecatedNdk=true.
  2. 在 build.gradle 文件里添加以下代碼
  1.  android { 
  2. ... 
  3. defaultConfig { 
  4.    ... 
  5.    ndk { 
  6.        abiFilters "armeabi-v7a""x86" 
  7.    } 
  8.  
  9.    packagingOptions { 
  10.        exclude "lib/arm64-v8abrealm-jni.so" 
  11.    } 
  12. }   

Genymotion模擬器運(yùn)行顯示沒有連接到developement server如下圖 

 

 

 

先檢查是否連接到網(wǎng)絡(luò)

點(diǎn)擊模擬器中Menu菜單彈出下面圖片,點(diǎn)擊Dev Settings 

 

 

 

4.點(diǎn)擊Debugging 下面的Debug Server host & port for device填寫地址和端口

 

責(zé)任編輯:龐桂玉 來源: 安卓巴士Android開發(fā)者門戶
相關(guān)推薦

2017-06-06 13:35:59

AndroidToolbar

2015-05-19 11:11:29

JavaScript事件使用指南

2010-08-26 10:28:43

2016-05-10 13:21:23

reactios原生模塊

2009-07-03 16:45:25

JSP實(shí)用教程

2012-07-18 12:44:02

2013-07-15 14:51:55

2018-06-08 10:18:22

Python裝飾器迭代器

2024-12-25 16:12:18

2011-08-15 13:44:07

iPhone開發(fā)UITableView

2021-02-26 14:05:32

PandasPython編程語言

2020-09-15 09:45:23

Pandas代碼Python

2013-08-05 11:34:02

2012-10-31 09:16:36

IT管理

2014-04-09 09:55:12

2012-12-25 09:53:40

域名

2014-12-17 10:29:59

混合應(yīng)用Hybrid App開發(fā)實(shí)戰(zhàn)

2021-01-28 10:12:02

鴻蒙JSJava

2020-04-09 11:23:30

微軟域名僵尸網(wǎng)絡(luò)

2011-08-29 09:19:25

c語言
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)