Skip to content

Commit be0103f

Browse files
committed
fix(android): support React Native 0.65
BREAKING CHANGE: Due to OkHttp 4 update, will not work for older RN versions. Fixes #30
1 parent 6eeece4 commit be0103f

File tree

12 files changed

+71
-45
lines changed

12 files changed

+71
-45
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ API 30 or higher.
1212
npm install react-native-file-access
1313
```
1414

15+
### Compatibility
16+
17+
For React Native 0.64 and older, use 1.x.x. For React Native 0.65+, use 2.x.x.
18+
1519
## Usage
1620

1721
```js

android/build.gradle

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ buildscript {
44

55
repositories {
66
google()
7-
jcenter()
7+
mavenCentral()
88
}
99

1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.2.1'
11+
classpath 'com.android.tools.build:gradle:4.2.1'
1212
// noinspection DifferentKotlinGradleVersion
1313
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1414
}
@@ -27,7 +27,7 @@ def getExtOrIntegerDefault(name) {
2727

2828
android {
2929
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
30-
30+
3131
defaultConfig {
3232
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
3333
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
@@ -43,15 +43,10 @@ android {
4343
lintOptions {
4444
disable 'GradleCompatible'
4545
}
46-
compileOptions {
47-
sourceCompatibility JavaVersion.VERSION_1_8
48-
targetCompatibility JavaVersion.VERSION_1_8
49-
}
5046
}
5147

5248
repositories {
5349
mavenCentral()
54-
jcenter()
5550
google()
5651

5752
def found = false
@@ -126,5 +121,5 @@ dependencies {
126121
// noinspection GradleDynamicVersion
127122
api 'com.facebook.react:react-native:+'
128123
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
129-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
124+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'
130125
}

android/gradle.properties

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FileAccess_kotlinVersion=1.4.10
2-
FileAccess_compileSdkVersion=29
3-
FileAccess_buildToolsVersion=29.0.2
4-
FileAccess_targetSdkVersion=29
5-
FileAccess_minSdkVersion=18
1+
FileAccess_kotlinVersion=1.5.21
2+
FileAccess_compileSdkVersion=30
3+
FileAccess_buildToolsVersion=30.0.2
4+
FileAccess_targetSdkVersion=30
5+
FileAccess_minSdkVersion=21

android/src/main/java/com/alpha0010/fs/FileAccessModule.kt

+35-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ class FileAccessModule(reactContext: ReactApplicationContext) :
4141
)
4242
}
4343

44+
// https://github.com/facebook/react-native/blob/v0.65.1/Libraries/EventEmitter/NativeEventEmitter.js#L22
45+
@ReactMethod
46+
fun addListener(eventType: String) = Unit
47+
48+
// https://github.com/facebook/react-native/blob/v0.65.1/Libraries/EventEmitter/NativeEventEmitter.js#L23
49+
@ReactMethod
50+
fun removeListeners(count: Int) = Unit
51+
4452
@ReactMethod
4553
fun appendFile(path: String, data: String, encoding: String, promise: Promise) {
4654
ioScope.launch {
@@ -113,7 +121,12 @@ class FileAccessModule(reactContext: ReactApplicationContext) :
113121
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
114122
reactApplicationContext.contentResolver.insert(
115123
MediaStore.Downloads.EXTERNAL_CONTENT_URI,
116-
ContentValues().apply { put(MediaStore.Downloads.DISPLAY_NAME, targetName) }
124+
ContentValues().apply {
125+
put(
126+
MediaStore.Downloads.DISPLAY_NAME,
127+
targetName
128+
)
129+
}
117130
)?.let { reactApplicationContext.contentResolver.openOutputStream(it) }
118131
} else {
119132
@Suppress("DEPRECATION")
@@ -136,7 +149,9 @@ class FileAccessModule(reactContext: ReactApplicationContext) :
136149
put(
137150
MediaStore.Audio.AudioColumns.DATA,
138151
File(
139-
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),
152+
Environment.getExternalStoragePublicDirectory(
153+
Environment.DIRECTORY_MUSIC
154+
),
140155
targetName
141156
).absolutePath
142157
)
@@ -147,13 +162,23 @@ class FileAccessModule(reactContext: ReactApplicationContext) :
147162
"images" -> {
148163
reactApplicationContext.contentResolver.insert(
149164
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
150-
ContentValues().apply { put(MediaStore.Images.Media.DISPLAY_NAME, targetName) }
165+
ContentValues().apply {
166+
put(
167+
MediaStore.Images.Media.DISPLAY_NAME,
168+
targetName
169+
)
170+
}
151171
)
152172
}
153173
"video" -> {
154174
reactApplicationContext.contentResolver.insert(
155175
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
156-
ContentValues().apply { put(MediaStore.Video.Media.DISPLAY_NAME, targetName) }
176+
ContentValues().apply {
177+
put(
178+
MediaStore.Video.Media.DISPLAY_NAME,
179+
targetName
180+
)
181+
}
157182
)
158183
}
159184
else -> null
@@ -286,7 +311,12 @@ class FileAccessModule(reactContext: ReactApplicationContext) :
286311
ioScope.launch {
287312
try {
288313
if (!parsePathToFile(source).renameTo(parsePathToFile(target))) {
289-
parsePathToFile(source).also { it.copyTo(parsePathToFile(target), overwrite = true) }
314+
parsePathToFile(source).also {
315+
it.copyTo(
316+
parsePathToFile(target),
317+
overwrite = true
318+
)
319+
}
290320
.delete()
291321
}
292322
promise.resolve(null)

android/src/main/java/com/alpha0010/fs/NetworkHandler.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.facebook.react.bridge.ReadableMap
66
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
77
import com.facebook.react.modules.network.OkHttpClientProvider
88
import okhttp3.*
9+
import okhttp3.RequestBody.Companion.toRequestBody
910
import java.io.IOException
1011

1112
const val FETCH_EVENT = "FetchEvent"
@@ -46,10 +47,10 @@ class NetworkHandler(reactContext: ReactContext) {
4647
if (init.hasKey("path")) {
4748
parsePathToFile(init.getString("path")!!)
4849
.outputStream()
49-
.use { response.body()!!.byteStream().copyTo(it) }
50+
.use { response.body!!.byteStream().copyTo(it) }
5051
}
5152

52-
val headers = response.headers().names().map { it to response.header(it) }
53+
val headers = response.headers.names().map { it to response.header(it) }
5354
emitter.emit(
5455
FETCH_EVENT, Arguments.makeNativeMap(
5556
mapOf(
@@ -58,9 +59,9 @@ class NetworkHandler(reactContext: ReactContext) {
5859
"headers" to Arguments.makeNativeMap(headers.toMap()),
5960
"ok" to response.isSuccessful,
6061
"redirected" to response.isRedirect,
61-
"status" to response.code(),
62-
"statusText" to response.message(),
63-
"url" to response.request().url().toString()
62+
"status" to response.code,
63+
"statusText" to response.message,
64+
"url" to response.request.url.toString()
6465
)
6566
)
6667
)
@@ -82,7 +83,7 @@ class NetworkHandler(reactContext: ReactContext) {
8283
if (init.hasKey("body")) {
8384
builder.method(
8485
init.getString("method")!!,
85-
RequestBody.create(null, init.getString("body")!!)
86+
init.getString("body")!!.toRequestBody(null)
8687
)
8788
} else {
8889
builder.method(init.getString("method")!!, null)
@@ -104,7 +105,7 @@ class NetworkHandler(reactContext: ReactContext) {
104105
.newBuilder()
105106
.addNetworkInterceptor { chain ->
106107
val originalResponse = chain.proceed(chain.request())
107-
originalResponse.body()
108+
originalResponse.body
108109
?.let { originalResponse.newBuilder().body(ProgressResponseBody(it, listener)).build() }
109110
?: originalResponse
110111
}

android/src/main/java/com/alpha0010/fs/ProgressResponseBody.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import okhttp3.ResponseBody
44
import okio.Buffer
55
import okio.BufferedSource
66
import okio.ForwardingSource
7-
import okio.Okio
7+
import okio.buffer
88

99
typealias ProgressListener = (bytesRead: Long, contentLength: Long, done: Boolean) -> Unit
1010

@@ -22,7 +22,7 @@ class ProgressResponseBody(
2222
override fun contentLength() = responseBody.contentLength()
2323

2424
override fun source(): BufferedSource {
25-
return bufferedSource ?: Okio.buffer(object : ForwardingSource(responseBody.source()) {
25+
return bufferedSource ?: object : ForwardingSource(responseBody.source()) {
2626
var totalBytesRead = 0L
2727

2828
override fun read(sink: Buffer, byteCount: Long): Long {
@@ -38,6 +38,6 @@ class ProgressResponseBody(
3838

3939
return bytesRead
4040
}
41-
}).also { bufferedSource = it }
41+
}.buffer().also { bufferedSource = it }
4242
}
4343
}

example/android/app/build.gradle

+1-6
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ android {
124124

125125
compileSdkVersion rootProject.ext.compileSdkVersion
126126

127-
compileOptions {
128-
sourceCompatibility JavaVersion.VERSION_1_8
129-
targetCompatibility JavaVersion.VERSION_1_8
130-
}
131-
132127
defaultConfig {
133128
applicationId "com.example.reactnativefileaccess"
134129
minSdkVersion rootProject.ext.minSdkVersion
@@ -213,7 +208,7 @@ dependencies {
213208
// Run this once to be able to run the application with BUCK
214209
// puts all compile dependencies into folder libs for BUCK to use
215210
task copyDownloadableDepsToLibs(type: Copy) {
216-
from configurations.compile
211+
from configurations.implementation
217212
into 'libs'
218213
}
219214

example/android/build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
buildscript {
44
ext {
5-
buildToolsVersion = "29.0.3"
5+
buildToolsVersion = "30.0.2"
66
minSdkVersion = 21
77
compileSdkVersion = 30
88
targetSdkVersion = 30
99
ndkVersion = "20.1.5948944"
1010
}
1111
repositories {
1212
google()
13-
jcenter()
13+
mavenCentral()
1414
}
1515
dependencies {
16-
classpath("com.android.tools.build:gradle:4.1.0")
16+
classpath("com.android.tools.build:gradle:4.2.1")
1717

1818
// NOTE: Do not place your application dependencies here; they belong
1919
// in the individual module build.gradle files
@@ -22,6 +22,7 @@ buildscript {
2222

2323
allprojects {
2424
repositories {
25+
mavenCentral()
2526
mavenLocal()
2627
maven {
2728
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
@@ -33,7 +34,6 @@ allprojects {
3334
}
3435

3536
google()
36-
jcenter()
3737
maven { url 'https://www.jitpack.io' }
3838
}
3939
}

example/android/gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919

2020
android.useAndroidX=true
2121
android.enableJetifier=true
22-
FLIPPER_VERSION=0.75.1
22+
FLIPPER_VERSION=0.93.0
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

example/ios/Podfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require_relative '../node_modules/react-native/scripts/react_native_pods'
22
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
33

4-
platform :ios, '10.0'
4+
platform :ios, '11.0'
55

66
target 'FileAccessExample' do
77
config = use_native_modules!

example/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
"start": "react-native start"
1010
},
1111
"dependencies": {
12-
"react": "17.0.1",
13-
"react-native": "0.64.1"
12+
"react": "17.0.2",
13+
"react-native": "0.65.1"
1414
},
1515
"devDependencies": {
1616
"@babel/core": "^7.12.10",
1717
"@babel/runtime": "^7.12.5",
1818
"babel-plugin-module-resolver": "^4.0.0",
19-
"metro-react-native-babel-preset": "^0.64.0"
19+
"metro-react-native-babel-preset": "^0.66.0",
20+
"react-native-codegen": "^0.0.7"
2021
}
2122
}

0 commit comments

Comments
 (0)