集成React Native到Android問題總結

官方文檔

http://reactnative.cn/docs/0.41/integration-with-existing-apps.html#content

引用的react native版本

這是一個巨坑:

根據官方文檔的做法可以添加本地的引用

allprojects {
    repositories {
        ...
        maven {
            url "$rootDir/../node_modules/react-native/android"
        }
    }
    ...
}

但是當時不知怎么了,把本地的引用加到了這里面

buildscript {
    repositories {
        jcenter()
        maven {
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

導致后面的流程根本沒有辦法跑通

這是因為在jcenter和本地maven都存在的時候,發現默認引用的是0.2x的版本,導致很多類都對應不上,其實可以通過查看源碼的方式去查看使用的react-native的版本號,還有一種就是直接指定react-native的版本號,如何查看使用的版本號呢?

當執行完一些的npm命令后,在文件夾中有一個文件 package.json 仔細找找會看到

  "dependencies": {
    "react": "^15.4.2",
    "react-native": "^0.41.2"
  }

找到版本號了吧,直接寫死就可以了

compile "com.facebook.react:react-native:0.41.2"

編譯版本問題

Caused by: java.lang.IllegalAccessError: Method 'void android.support.v4.net.ConnectivityManagerCompat.<init>()' is inaccessible to class 'com.facebook.react.modules.netinfo.NetInfoModule'

應該是React Native源碼中使用的 v4 包的版本號和工程中使用的版本號不一致導致的,這個也很好解決,找到對應的版本號,改成一致就可以,或者修改 reactNative當中的版本號,或者修改項目中的自己使用的版本號信息,'com.android.support:appcompat-v7:23.0.1'

ip和port設置

如果根據官方文檔運行的話,有可能是網絡問題,這里我直接寫死了

SharedPreferences mPreferneces= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = mPreferneces.edit();
editor.putString("debug_http_host","192.168.1.1:8081"); // 服務器所在的ip地址
editor.commit();

一種修改端口號的方法 react-native start --port 8083

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容