謹以此例介紹如何從源代碼編譯RN項目牵触,讓你隨心所欲。
a.以下辦法要自己編譯ndk
1準備編譯react-native源碼
注意不推薦從github重新拉取代碼挑胸,直接在node_modules底下有對應版本的源碼
執(zhí)行官網(wǎng)的步驟到“讓第三方模塊使用你的分支”,這時候需要把所有第三方包的build.gradle中
compile 'com.facebook.react:react-native:+'
改成compile project(':ReactAndroid')
2修改源碼
1)fetch
文件路徑
node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java
添加方法:
public static OkHttpClient.Builder getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return builder;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
修改方法:
public static OkHttpClient createClient() {
// No timeouts by default
OkHttpClient.Builder client = getUnsafeOkHttpClient()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS)
.cookieJar(new ReactCookieJarContainer());
return enableTls12OnPreLollipop(client).build();
}
2)WebView
文件路徑
node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java
在內(nèi)部類ReactWebViewClient
中添加重載方法:
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
super.onReceivedSslError(view, handler, error);
}
b.不需要編譯ndk (把普通依賴改成aar依賴)
為什么要研究這種辦法宰闰?現(xiàn)在都是協(xié)作開發(fā)茬贵,如果你負責修改了源代碼,但是你的同時并不需要關心這塊移袍,難道他也要進行復雜的配置并且編譯NDK嗎解藻?未必。
1 新建module選擇如下的選項葡盗,按照提示引入a步驟得到的aar螟左,路徑是
ReactAndroid module下的build/outputs/aar/ReactAndroid-release.aar
2 在新建一個Android Library的Module,名字就叫ReactAndroidDelegate吧。用來部署屬于你自己的React依賴戳粒。這個路狮,主要是其build.gradle依賴配置,清單如下:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile project(':ReactAndroid')
// react-native dependencies
compile 'javax.inject:javax.inject:1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.facebook.fbui.textlayoutbuilder:textlayoutbuilder:1.0.0'
compile 'com.facebook.fresco:fresco:0.11.0'
compile 'com.facebook.fresco:imagepipeline-okhttp3:0.11.0'
compile 'com.facebook.soloader:soloader:0.1.0'
compile 'com.google.code.findbugs:jsr305:3.0.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'
compile 'com.squareup.okhttp3:okhttp-ws:3.4.1'
compile 'com.squareup.okio:okio:1.9.0'
compile 'org.webkit:android-jsc:r174650'
}
此處切不可省略這個步驟蔚约,因為其他以來react-native的會找不到一些以來奄妨,這里面下面的一段就是從react原生的配置清單里拉過來的。
3 把所有的第三方module里的build.gradle中compile 'com.facebook.react:react-native:+'替換成compile project(':ReactAndroidDelegate')
怎樣檢驗你的更改是有效的苹祟,在Project->External Libraries下面查看反編譯的class文件里的代碼砸抛,是否是你修改過的樣子。
本例的文件路徑就是截圖右下角的OkHttpClientProvider.
至此你就可以把aar文件上傳到你們團隊的git服務器树枫,只有2M左右直焙,就可以像平時一樣的協(xié)作開發(fā)了。
現(xiàn)在有沒有感覺整個react-native的世界都在你的掌控中