我是靠谱客的博主 快乐煎蛋,这篇文章主要介绍React Native For Android,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
Gradle DSL method not found: 'url()' Possible causes:The project '***' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0). <a href="fixGradleElements"> Upgrade plugin to version 3.0.0-alpha1 and sync project The project '***' may be using a version of Gradle that does not contain the method.

解决办法:用来设置node_modules的父目录,如果在根目录下直接删掉,如果你很执着,同步后,就会报错:
“Failed to resolve: com.facebook.react:react-native:0.x.x”
参考:http://www.cnblogs.com/details-666/p/RN.html

复制代码
1
Warning:Conflict with dependency 'com.google.code.findbugs:jsr305'. Resolved versions for app (3.0.1) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

解决办法:在您的应用程序build.gradle添加内的下列Android { }:

复制代码
1
2
3
configurations.all { resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9' }

参考:http://blog.csdn.net/qq_23089525/article/details/52777520

复制代码
1
Error:Dex writing phase: classes.dex has too many IDs. Try using multi-dex

解决办法:打开Dex分包开关,具体如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1. android { defaultConfig { // Enabling multidex support. multiDexEnabled true } } (**添加在主module中,子module如果比较大,也可以添加**) 2. dependencies { //noinspection GradleCompatible compile 'com.android.support:multidex:1.0.1'} (**注意multidex为小写**) 3.在自己的Application中重写 @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }

参考:http://blog.csdn.net/t12x3456/article/details/40837287

添加了百度sdk的可能会抛出 libBaiduMapSDK_base_v4_3_1.so” is 32-bit instead of 64-bit
解决办法:
将arm64-v8a,armeabi,armeabi-v7a包都放在lib下
并且在app的build.gradle的android标签下sourceSets {
main() {
jniLibs.srcDirs = [‘libs’]
}
}
不然会报错: No implementation found for int com.baidu.platform.comjni.tools.JNITools.initClass

could find DSO to load: libreactnativejni.so
解决办法:
build.gradle中加入:

复制代码
1
2
3
4
5
6
7
8
9
android { ... defaultConfig { ... ndk { abiFilters "armeabi-v7a", "x86" } }

参考:http://www.jianshu.com/p/35ae1990673a

ConnectivityManagerCompat.()’ is inaccessible to class ‘com.facebook.react.modules.netinfo.NetInfoModule’
解决办法:compile ‘com.android.support:appcompat-v7:23.0.1’ 导入此版本的v7包
确认项目的maven路径是否正确,Gradle配置成在线编译,不然gradle可能本地编译不通过

复制代码
1
2
3
4
5
6
7
8
allprojects { repositories { jcenter() maven { url "$rootDir/node_modules/react-native/android" } } }

No virtual method setCallWebSocket(Lokhttp3/Call;)V in class Lokhttp3/internal/Internal; or its super classes (declaration of ‘okhttp3.internal.Internal’ appears in /data/app/。。/。。.apk)
解决办法:
在app的build.gradle里添加

复制代码
1
2
3
4
5
6
android{ configurations.all { ..... resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' } }

react native 错误:evaluating ReactInternals.ReactCurrentOwner
解决办法:
切到项目主目录,执行命令行 yarn add react@16.0.0-alpha.12 重启服务:npm start
参考:https://segmentfault.com/q/1010000009822772

打包时存在的坑
解决办法:请移步http://blog.csdn.net/u011965040/article/details/53331859?locationNum=15&fps=1
调试
模拟器:按双R没反应,可能和某些软件冲突了,或者没有开发者菜单键,可以将开发者菜单设置成其他监听:
比如设置成音量键监听

复制代码
1
2
3
4
5
6
7
8
9
10
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { //当我们点击菜单的时候打开发者菜单,一个弹窗(此处需要悬浮窗权限才能显示) if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mReactInstanceManager != null) { mReactInstanceManager.showDevOptionsDialog(); return true; } return super.onKeyUp(keyCode, event); }

这样点击音量键的时候就弹出开发者菜单,同理,可以设置其他监听
真机调试:
连接不了服务:
可以修改真机访问的主机名:
开发者菜单 => Dev Settings => Debug server host&port for device
查看电脑主机ip地址:cmd => ipconfig
将主机地址填入host输入框,如 xx.xx.xx.xx:8081 , 摇晃手机reload

Refusing to install react as a dependency of itself

解决方法:

你不能把你的项目取名为 react

你项目的 package.json 里面 name 的值不能是 react

也不能是你项目其他依赖包的名字

也就是和依赖包不能重名

undefined is not an object (evaluating ‘_react2.default.NativeModules.*

解决办法:根据官网的配置,但是还是在调用android本地方法的时候还是抛出异常,查看js代码调用android的方法是: React.NativeModules.自定义的module.本地方法;
正确的调用方法是:NativeModules.自定义的module.本地方法;

tried to show an alert while not attached to an activity
解决办法:将rn嵌入到现有项目中,根据官网的文档进行嵌入:http://reactnative.cn/docs/0.45/integration-with-existing-apps.html#content 有的人可能会遗漏一些细节,出现上面这个问题的原因是
:没有将onPause,onResume,onDestory也加进加载React的Activity中:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class IntentReactActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler { private static final int OVERLAY_PERMISSION_REQ_CODE = 1; private ReactRootView mReactRootView; private ReactInstanceManager mReactInstanceManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mReactRootView = new ReactRootView(this); mReactInstanceManager = ReactInstanceManager.builder() .setApplication(FIFApplication.getInstance()) .setBundleAssetName("index.android.bundle") .setJSMainModuleName("index.android") .addPackage(new MainReactPackage()) .addPackage(new IntentReactPackage(this)) .setUseDeveloperSupport(com.fif.wisdie.BuildConfig.DEBUG) .setInitialLifecycleState(LifecycleState.RESUMED) .build(); // 注意这里的HelloWorld必须对应“index.android.js”中的 // “AppRegistry.registerComponent()”的第一个参数 mReactRootView.startReactApplication(mReactInstanceManager, "***", null); setContentView(mReactRootView); } @Override protected void onPause() { super.onPause(); if (mReactInstanceManager != null) { mReactInstanceManager.onHostPause(this); } } @Override protected void onResume() { super.onResume(); if (mReactInstanceManager != null) { mReactInstanceManager.onHostResume(this, this); } } @Override protected void onDestroy() { super.onDestroy(); if (mReactInstanceManager != null) { mReactInstanceManager.onHostDestroy(); } } @Override public void onBackPressed() { if (mReactInstanceManager != null) { mReactInstanceManager.onBackPressed(); } else { super.onBackPressed(); } } }

adb devices检测不到夜神模拟器
解决办法:
1、dos下,cd进入到夜神模拟器的bin目录
代码:
nox_adb connect 127.0.0.1:62001
2.dos下,进入进Android SDK下的platform-tools目录
代码:
adb connect 127.0.0.1:62001

最后

以上就是快乐煎蛋最近收集整理的关于React Native For Android的全部内容,更多相关React内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部