1、根据官网提供的步骤进行相应的设置
(1)npm i react-native-image-crop-picker --save
(2)react-native link react-native-image-crop-picker
这里命令窗很不幸地始终没什么动静(除了几行字)
(3)Make sure you are using Gradle 2.2.x (android/build.gradle):
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
...
}
...
}
(4)VERY IMPORTANT Add the following to your build.gradle’s repositories section. (android/build.gradle):
allprojects {
repositories {
mavenLocal()
jcenter()
maven { url "$rootDir/../node_modules/react-native/android" }
// jitpack repo is necessary to fetch ucrop dependency
maven { url "https://jitpack.io" }
}
}
(5)Add useSupportLibrary (android/app/build.gradle):
android {
...
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
...
}
...
}
(6)可选:app\src\main\AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
2、开始运行:
配置完上述内容后我用Android Studio重新build了一遍项目,但是报错了:
程序包com.reactnative.ivpusic.imagepicker不存在 找不到符号等等
然鹅还是有问题,运行起来还是有问题,应该是链接原生库没成功(react-native link react-native-image-crop-picker
)。最终经过多次尝试后,我修改了以下文件的以下地方:
(1)file: android/app/build.gradle:
dependencies {
...
compile project(':react-native-image-crop-picker')
}
(2)file: MainApplication.java
...
import com.reactnative.ivpusic.imagepicker.PickerPackage; // import package
public class MainApplication extends ReactApplication {
...
/**
* A list of packages used by the app. If the app uses additional views
* or modules besides the default ones, add more packages here.
*/
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new PickerPackage() // Add package
);
}
...
}
(3)file: android/settings.gradle:
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')
(4)此时再用Android Studio重新build了一遍项目即可成功使用react-native-image-crop-picker了,是在不行将node_modules删除,重新安装依赖,再多build几遍项目试试。该插件具体使用可以参考https://blog.csdn.net/u013718120/article/details/72781285。