1.Mac系統(tǒng),Android-Studo2.2.3,ReactNative0.40
2.必須要先成功運(yùn)行HelloWorld工程,node_modules和package.json
3.創(chuàng)建安裝項(xiàng)目名稱'LaiHH',選中最低兼容16,android 4.1版本,創(chuàng)建一個(gè)Empty Activity空項(xiàng)目
4.對(duì)應(yīng)目錄結(jié)構(gòu)如下
5.build.gradle加上代碼
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
//原生Android集成ReactNative需要增加
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
6.build.gradle加上代碼(2個(gè)文件不一樣的,對(duì)應(yīng)的加上)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.laihuihuang.laihh"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
// exclude group: 'com.android.support', module: 'support-annotations'
// })
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.facebook.react:react-native:+'//ReactNative組件
compile project(':react-native-image-picker')//相冊(cè)選擇
compile project(':react-native-image-crop-picker')//相冊(cè)選中
compile project(':react-native-smart-pull-to-refresh-listview')//下拉刷新
compile project(':react-native-picker')//選擇器
// compile project(':imagepickerModule')
// compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3' //optional
// compile 'com.github.bumptech.glide:glide:3.6.1' //optional
// compile 'com.squareup.picasso:picasso:2.4.0' //optional
}
7.settings.gradle加上自己常用的第三方組件
rootProject.name = 'LaiHH'
include ':react-native-image-picker'
project(':react-native-image-picker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-image-picker/android')
include ':react-native-image-crop-picker'
project(':react-native-image-crop-picker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-image-crop-picker/android')
include ':react-native-smart-pull-to-refresh-listview'
project(':react-native-smart-pull-to-refresh-listview').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-smart-pull-to-refresh-listview/android')
include ':react-native-picker'
project(':react-native-picker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-picker/android')
//include ':imagepickerModule'
//project(':imagepickerModule').projectDir = new File(rootProject.projectDir, '../node_modules/imagepickerModule')
include ':app'
8.創(chuàng)建全局對(duì)象MainApplication
package com.example.laihuihuang.laihh;
import android.app.Application;
import android.util.Log;
import com.imagepicker.ImagePickerPackage;//相冊(cè)選擇
import com.beefe.picker.PickerViewPackage;//選擇器
import com.reactnative.ivpusic.imagepicker.PickerPackage;//相冊(cè)多選
import com.reactnativecomponent.swiperefreshlayout.RCTSwipeRefreshLayoutPackage;//下拉刷新
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new PickerPackage(),//單選
new RCTSwipeRefreshLayoutPackage(),//下拉刷新
new PickerViewPackage(), //選中數(shù)字
new ImagePickerPackage() //相冊(cè)
);
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}
9.創(chuàng)建RNActivity
package com.example.laihuihuang.laihh;
import android.os.Bundle;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.widget.RelativeLayout;
import android.view.KeyEvent;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.shell.MainReactPackage;
import com.imagepicker.ImagePickerPackage;
import com.beefe.picker.*;
import com.reactnative.ivpusic.imagepicker.*;
import com.reactnativecomponent.swiperefreshlayout.*;
public class RNActivity extends ReactActivity {
private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// mReactRootView = new ReactRootView(this);
// mReactInstanceManager = ReactInstanceManager.builder()
// .setApplication(getApplication())
// .setBundleAssetName("index.android.bundle")
// .setJSMainModuleName("index.android")
// .addPackage(new MainReactPackage())
// .addPackage(new PickerViewPackage())
// .addPackage(new ImagePickerPackage())
// .addPackage(new RCTSwipeRefreshLayoutPackage())
// .addPackage(new PickerPackage())
// .setUseDeveloperSupport(BuildConfig.DEBUG)
// .setInitialLifecycleState(LifecycleState.RESUMED)
// //.setUseOldBridge(true) // uncomment this line if your app crashes
// .build();
// mReactRootView.startReactApplication(mReactInstanceManager, "LaiHH", null);//這里名字要對(duì)應(yīng)你的項(xiàng)目
// setContentView(mReactRootView);
}
@Override
protected String getMainComponentName() {
return "LaiHH"; // 你的項(xiàng)目的名字
}
}
10.編輯activity_rn.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/test_r"
tools:context="com.example.laihuihuang.laihh.RNActivity">
<com.facebook.react.ReactRootView
android:id="@+id/test_js"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
11.增加權(quán)限以及設(shè)置全局對(duì)象 android:name=".MainApplication"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.laihuihuang.laihh">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RNActivity"/>
</application>
</manifest>
12.編輯activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.laihuihuang.laihh.MainActivity">
<Button
android:text="點(diǎn)擊我跳轉(zhuǎn)到ReactNative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
</RelativeLayout>
13.編輯MainActivity
package com.example.laihuihuang.laihh;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.content.Intent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, RNActivity.class);//傳入上下文,和要跳轉(zhuǎn)的界面
startActivity(intent);
}
});
}
}
14.最好clean下功能.重新編譯下工程,啟動(dòng)服務(wù)器運(yùn)行