Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎉 Update boilerplate to match newer React Native #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
17 changes: 9 additions & 8 deletions android/src/main/java/org/linusu/RNGetRandomValuesModule.java
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
34 changes: 17 additions & 17 deletions android/src/main/java/org/linusu/RNGetRandomValuesPackage.java
Original file line number Diff line number Diff line change
@@ -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<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNGetRandomValuesModule(reactContext));
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// Deprecated from RN 0.47
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
public class RNGetRandomValuesPackage implements ReactPackage {
@NonNull
@Override
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new RNGetRandomValuesModule(reactContext));
return modules;
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@NonNull
@Override
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 {}
Expand Down Expand Up @@ -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)
}
}

Expand Down
6 changes: 1 addition & 5 deletions ios/RNGetRandomValues.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions react-native-get-random-values.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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