diff --git a/android/build.gradle b/android/build.gradle index 9f8e491..29a8551 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,12 +1,10 @@ buildscript { - // The Android Gradle plugin is only required when opening the android folder stand-alone. - // This avoids unnecessary downloads and potential conflicts when the library is included as a - // module dependency in an application project. if (project == rootProject) { repositories { mavenCentral() google() } + def buildGradleVersion = ext.has('buildGradlePluginVersion') ? ext.get('buildGradlePluginVersion') : '4.2.2' dependencies { @@ -23,28 +21,39 @@ def safeExtGet(prop, fallback) { android { compileSdkVersion safeExtGet('compileSdkVersion', 30) - buildToolsVersion safeExtGet('buildToolsVersion', '30.0.3') - defaultConfig { minSdkVersion safeExtGet('minSdkVersion', 16) targetSdkVersion safeExtGet('targetSdkVersion', 30) versionCode 1 versionName "1.0" + + } + + buildTypes { + release { + minifyEnabled false + } } lintOptions { - abortOnError false + disable 'GradleCompatible' + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 } } repositories { - mavenCentral() - google() + mavenLocal() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm - url "$rootDir/../node_modules/react-native/android" + url("$rootDir/../node_modules/react-native/android") } + google() + mavenCentral() } dependencies { - implementation 'com.facebook.react:react-native:+' + //noinspection GradleDynamicVersion + implementation "com.facebook.react:react-native:+" // From node_modules } diff --git a/android/src/main/java/org/linusu/RNGetRandomValuesModule.java b/android/src/main/java/org/linusu/RNGetRandomValuesModule.java index 37c728d..2ce6dea 100644 --- a/android/src/main/java/org/linusu/RNGetRandomValuesModule.java +++ b/android/src/main/java/org/linusu/RNGetRandomValuesModule.java @@ -1,30 +1,31 @@ package org.linusu; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; - import android.util.Base64; +import androidx.annotation.NonNull; + import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; -import com.facebook.react.bridge.Callback; -public class RNGetRandomValuesModule extends ReactContextBaseJavaModule { +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; - private final ReactApplicationContext reactContext; +public class RNGetRandomValuesModule extends ReactContextBaseJavaModule { + public static final String NAME = "RNGetRandomValues"; public RNGetRandomValuesModule(ReactApplicationContext reactContext) { super(reactContext); - this.reactContext = reactContext; } @Override + @NonNull public String getName() { - return "RNGetRandomValues"; + return NAME; } @ReactMethod(isBlockingSynchronousMethod = true) + @NonNull public String getRandomBase64(int byteLength) throws NoSuchAlgorithmException { byte[] data = new byte[byteLength]; SecureRandom random = new SecureRandom(); diff --git a/android/src/main/java/org/linusu/RNGetRandomValuesPackage.java b/android/src/main/java/org/linusu/RNGetRandomValuesPackage.java index b5703b2..d76ddfd 100644 --- a/android/src/main/java/org/linusu/RNGetRandomValuesPackage.java +++ b/android/src/main/java/org/linusu/RNGetRandomValuesPackage.java @@ -1,28 +1,28 @@ package org.linusu; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import androidx.annotation.NonNull; import com.facebook.react.ReactPackage; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.uimanager.ViewManager; -import com.facebook.react.bridge.JavaScriptModule; -public class RNGetRandomValuesPackage implements ReactPackage { - @Override - public List createNativeModules(ReactApplicationContext reactContext) { - return Arrays.asList(new RNGetRandomValuesModule(reactContext)); - } +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; - // Deprecated from RN 0.47 - public List> createJSModules() { - return Collections.emptyList(); - } +public class RNGetRandomValuesPackage implements ReactPackage { + @NonNull + @Override + public List createNativeModules(@NonNull ReactApplicationContext reactContext) { + List modules = new ArrayList<>(); + modules.add(new RNGetRandomValuesModule(reactContext)); + return modules; + } - @Override - public List createViewManagers(ReactApplicationContext reactContext) { - return Collections.emptyList(); - } + @NonNull + @Override + public List createViewManagers(@NonNull ReactApplicationContext reactContext) { + return Collections.emptyList(); + } } diff --git a/index.js b/index.js index df701c3..6756ed0 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,10 @@ const base64Decode = require('fast-base64-decode') -const { NativeModules } = require('react-native') +const { NativeModules, Platform } = require('react-native') + +const LINKING_ERROR = + "The package 'react-native-get-random-values' doesn't seem to be linked. Make sure: \n\n" + + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + + '- You rebuilt the app after installing the package\n' class TypeMismatchError extends Error {} class QuotaExceededError extends Error {} @@ -29,7 +34,7 @@ function getRandomBase64 (byteLength) { } else if (NativeModules.ExpoRandom) { return NativeModules.ExpoRandom.getRandomBase64String(byteLength) } else { - throw new Error('Native module not found') + throw new Error(LINKING_ERROR) } } diff --git a/ios/RNGetRandomValues.m b/ios/RNGetRandomValues.m index 4eaa540..f2098d6 100644 --- a/ios/RNGetRandomValues.m +++ b/ios/RNGetRandomValues.m @@ -2,13 +2,9 @@ @implementation RNGetRandomValues -- (dispatch_queue_t)methodQueue -{ - return dispatch_get_main_queue(); -} RCT_EXPORT_MODULE() -RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString*, getRandomBase64:(NSUInteger)byteLength) { +RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getRandomBase64:(NSUInteger)byteLength) { NSMutableData *data = [NSMutableData dataWithLength:byteLength]; int result = SecRandomCopyBytes(kSecRandomDefault, byteLength, data.mutableBytes); if (result != errSecSuccess) { diff --git a/react-native-get-random-values.podspec b/react-native-get-random-values.podspec index f7075eb..e127640 100644 --- a/react-native-get-random-values.podspec +++ b/react-native-get-random-values.podspec @@ -6,15 +6,14 @@ Pod::Spec.new do |s| s.name = "react-native-get-random-values" s.version = package["version"] s.summary = "getRandomValues for React Native" - s.description = "A small implementation of `getRandomValues` for React Native." s.homepage = "https://github.com/LinusU/react-native-get-random-values" s.license = "MIT" s.authors = { "Linus Unnebäck" => "linus@folkdatorn.se" } + s.platforms = { :ios => "9.0", :tvos => "9.0", :osx => "10.14" } s.source = { :git => "https://github.com/LinusU/react-native-get-random-values.git", :tag => "v#{s.version}" } - s.source_files = "ios/**/*.{h,m,swift}" - s.requires_arc = true + s.source_files = "ios/**/*.{h,m,mm}" s.dependency "React-Core" end