您的位置:首页 > Web前端 > React

React-native植入原生Android应用

2016-07-13 23:18 423 查看
首先按照React Native的官网上所说的步骤来。

注意事项

1. 一些其他文件:

curl -o .flowconfig

https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig

上面的这个语句,其作用只是新建了一个.flowconfig文件,这些文件可以直接从其他RN项目里拷贝,如下图:



其中,index.android.js里的注册组件名要和Android项目中的一样。

2.报错记录:

只是做了上面的这些,当然会报错。

首先Android版本不能低于16的问题,这个简单,在app里的
build.gradle
文件里将
minSdkVersion
设置为16或者以上就行。

其次才是大问题,当前的react native的版本是0.26.3,Android里的依赖V7包的版本为:
compile 'com.android.support:appcompat-v7:23.4.0'


所报错为:

FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground()


网络上的资料显示,可能是V7版本的原因,资料参见:求助帖1RN集成安卓原生教程github issues讨论

参照第二个帖子里的资料,修改了v7版本号到23.0.1,在
gradle.properties
文件里的末尾行添加android.useDeprecatedNdk=true,上面的错误消失,应用可以运行了,可喜可贺,可喜可贺。

但是出现了另一个错误

undefined is not a function
(evaluating 'UIManager.setChildren(containerTag,created Tags)')




查阅github上资料显示,可能是react native版本号0.26.2以上的问题(此时为0.26.3),于是修改版本号为react版本0.14.7,

react-native版本号为0.25.1,如下图:



问题成功解决,项目移植成功!

PS:其他问题

• 在本人的移植中,新建项目后的依赖就是

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha2'
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support:support-annotations:23.4.0'
}


在设置
com.android.support:appcompat-v7:23.4.0
版本为23.0.1,并且加入
compile 'com.facebook.react:react-native:0.20.+'
这句话之后的编译中,会报错:

Error:Conflict with dependency 'com.google.code.findbugs:jsr305'. Resolved versions for app (3.0.0) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.0.1) and test app (23.4.0) differ. See http://g.co/androidstudio/app-test-app-conflict for details.


修改方法是,删除上面依赖中的 androidTestCompile的三句话。

• 很多情况下不成功是因为npm install不全,所以还是直接拷贝成功的node_modules文件夹。所以简单来说,可以直接拷贝除了安卓项目代码之外的其他文件夹,然后再做相应更改。如图:



然后,安卓依赖里的
compile 'com.android.support:support-v4:23.0.1'
不是必须的,只有在你的项目用到了V4包时才需要上面这句话,确保其版本在23.0.1 ,而不能是其他版本,这点跟V7包是一样的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  native rn 植入原生