Skip to content

Commit 6310858

Browse files
authored
Merge pull request #510 from dcangulo/new-arch
feat: add new arch support
2 parents 3a0da89 + a1297e4 commit 6310858

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+5766
-4669
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,9 @@ android/keystores/debug.keystore
7777
# generated by bob
7878
lib/
7979

80+
# React Native Codegen
81+
ios/generated
82+
android/generated
83+
8084
# tests
8185
.tap

.yarnrc.yml

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ nodeLinker: node-modules
22
nmHoistingLimits: workspaces
33

44
plugins:
5-
- path: scripts/pod-install.cjs
65
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
76
spec: "@yarnpkg/plugin-interactive-tools"
87
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 David Angulo
3+
Copyright (c) 2024 David Angulo
44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal
66
in the Software without restriction, including without limitation the rights

README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ Proof Key for Code Exchange (PKCE) challenge generator for React Native.
1717
|Node.js |🟢
1818

1919
## Installation
20+
### New Arch
2021
```bash
2122
yarn add react-native-pkce-challenge
22-
npx pod-install ios # iOS Only
23-
npx pod-install macos # macOS Only
23+
npx pod-install
24+
```
25+
26+
### Old Arch
27+
```bash
28+
29+
npx pod-install
2430
```
2531

2632
## Usage
@@ -60,4 +66,8 @@ See [UPGRADING.md](UPGRADING.md)
6066
See [CHANGELOGS.md](CHANGELOGS.md)
6167

6268
## License
63-
Copyright © 2023 David Angulo, released under the MIT license, see [LICENSE](LICENSE).
69+
Copyright © 2024 David Angulo, released under the MIT license, see [LICENSE](LICENSE).
70+
71+
---
72+
73+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)

android/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ project(PkceChallenge)
44
set (CMAKE_VERBOSE_MAKEFILE ON)
55
set (CMAKE_CXX_STANDARD 14)
66

7-
add_library(cpp
7+
add_library(react-native-pkce-challenge
88
SHARED
99
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp"
1010
../cpp/react-native-pkce-challenge.cpp

android/build.gradle

+37
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ import java.nio.file.Paths
22
import org.apache.tools.ant.taskdefs.condition.Os
33

44
buildscript {
5+
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
6+
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["PkceChallenge_kotlinVersion"]
7+
58
repositories {
69
google()
710
mavenCentral()
811
}
912

1013
dependencies {
1114
classpath "com.android.tools.build:gradle:7.2.1"
15+
// noinspection DifferentKotlinGradleVersion
16+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1217
}
1318
}
1419

@@ -33,11 +38,17 @@ static def findNodeModules(baseDir) {
3338

3439
def nodeModules = findNodeModules(projectDir)
3540

41+
def reactNativeArchitectures() {
42+
def value = rootProject.getProperties().get("reactNativeArchitectures")
43+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
44+
}
45+
3646
def isNewArchitectureEnabled() {
3747
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
3848
}
3949

4050
apply plugin: "com.android.library"
51+
apply plugin: "kotlin-android"
4152

4253
if (isNewArchitectureEnabled()) {
4354
apply plugin: "com.facebook.react"
@@ -77,11 +88,13 @@ android {
7788
defaultConfig {
7889
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
7990
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
91+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
8092

8193
externalNativeBuild {
8294
cmake {
8395
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
8496
arguments "-DNODE_MODULES_DIR=${nodeModules}"
97+
abiFilters (*reactNativeArchitectures())
8598
}
8699
}
87100
}
@@ -92,6 +105,10 @@ android {
92105
}
93106
}
94107

108+
buildFeatures {
109+
buildConfig true
110+
}
111+
95112
buildTypes {
96113
release {
97114
minifyEnabled false
@@ -106,18 +123,38 @@ android {
106123
sourceCompatibility JavaVersion.VERSION_1_8
107124
targetCompatibility JavaVersion.VERSION_1_8
108125
}
126+
127+
sourceSets {
128+
main {
129+
if (isNewArchitectureEnabled()) {
130+
java.srcDirs += [
131+
"generated/java",
132+
"generated/jni"
133+
]
134+
}
135+
}
136+
}
109137
}
110138

111139
repositories {
112140
mavenCentral()
113141
google()
114142
}
115143

144+
def kotlin_version = getExtOrDefault("kotlinVersion")
116145

117146
dependencies {
118147
// For < 0.71, this will be from the local maven repo
119148
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
120149
//noinspection GradleDynamicVersion
121150
implementation "com.facebook.react:react-native:+"
151+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
122152
}
123153

154+
if (isNewArchitectureEnabled()) {
155+
react {
156+
jsRootDir = file("../src/")
157+
libraryName = "PkceChallenge"
158+
codegenJavaPackageName = "com.pkcechallenge"
159+
}
160+
}

android/cpp-adapter.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
#include "react-native-pkce-challenge.h"
33

44
extern "C"
5-
JNIEXPORT void JNICALL
6-
Java_com_pkcechallenge_PkceChallengeModule_nativeInstall(JNIEnv *env, jobject thiz, jlong jsi) {
7-
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsi);
5+
JNIEXPORT jstring JNICALL
6+
Java_com_pkcechallenge_PkceChallengeModule_nativeGetRandomBase64String(JNIEnv *env, jclass type, jdouble byte_length) {
7+
std::string encoded_data = pkcechallenge::getRandomBase64String(byte_length);
88

9-
if (runtime)
10-
{
11-
pkcechallenge::install(*runtime);
12-
}
9+
return env->NewStringUTF(encoded_data.c_str());
1310
}

android/src/main/java/com/pkcechallenge/PkceChallengeModule.java

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.pkcechallenge
2+
3+
import com.facebook.react.bridge.ReactApplicationContext
4+
import com.facebook.react.module.annotations.ReactModule
5+
6+
import java.security.SecureRandom
7+
import android.util.Base64
8+
9+
@ReactModule(name = PkceChallengeModule.NAME)
10+
class PkceChallengeModule(reactContext: ReactApplicationContext) :
11+
NativePkceChallengeSpec(reactContext) {
12+
13+
override fun getName(): String {
14+
return NAME
15+
}
16+
17+
// Example method
18+
// See https://reactnative.dev/docs/native-modules-android
19+
override fun getRandomBase64String(byteLength: Double): String {
20+
val bytes = ByteArray(byteLength.toInt())
21+
val secureRandom = SecureRandom()
22+
secureRandom.nextBytes(bytes)
23+
24+
return Base64.encodeToString(bytes, Base64.NO_WRAP)
25+
}
26+
27+
companion object {
28+
const val NAME = "PkceChallenge"
29+
}
30+
}

android/src/main/java/com/pkcechallenge/PkceChallengePackage.java

-28
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.pkcechallenge
2+
3+
import com.facebook.react.TurboReactPackage
4+
import com.facebook.react.bridge.NativeModule
5+
import com.facebook.react.bridge.ReactApplicationContext
6+
import com.facebook.react.module.model.ReactModuleInfo
7+
import com.facebook.react.module.model.ReactModuleInfoProvider
8+
import java.util.HashMap
9+
10+
class PkceChallengePackage : TurboReactPackage() {
11+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12+
return if (name == PkceChallengeModule.NAME) {
13+
PkceChallengeModule(reactContext)
14+
} else {
15+
null
16+
}
17+
}
18+
19+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
20+
return ReactModuleInfoProvider {
21+
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
22+
moduleInfos[PkceChallengeModule.NAME] = ReactModuleInfo(
23+
PkceChallengeModule.NAME,
24+
PkceChallengeModule.NAME,
25+
false, // canOverrideExistingModule
26+
false, // needsEagerInit
27+
true, // hasConstants
28+
false, // isCxxModule
29+
true // isTurboModule
30+
)
31+
moduleInfos
32+
}
33+
}
34+
}

babel.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
presets: ['module:metro-react-native-babel-preset'],
2+
presets: [['module:react-native-builder-bob/babel-preset', { modules: 'commonjs' }]],
33
};

cpp/react-native-pkce-challenge.cpp

+5-29
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,12 @@
44
#include <vector>
55
#include <stdlib.h>
66

7-
using namespace facebook::jsi;
8-
using namespace std;
9-
107
namespace pkcechallenge {
11-
void install(Runtime &jsiRuntime)
12-
{
13-
auto getRandomBase64String = Function::createFromHostFunction(jsiRuntime,
14-
PropNameID::forAscii(jsiRuntime,
15-
"getRandomBase64String"),
16-
0,
17-
[](Runtime &runtime,
18-
const Value &thisValue,
19-
const Value *arguments,
20-
size_t count) -> Value
21-
{
22-
int byteLength = arguments[0].getNumber();
23-
vector<uint8_t> buffer(byteLength, 0);
24-
arc4random_buf(&buffer[0],
25-
buffer.size());
26-
27-
string encodedData = base64_encode(&buffer[0],
28-
buffer.size());
29-
30-
return Value(runtime,
31-
String::createFromUtf8(runtime,
32-
encodedData));
33-
});
8+
std::string getRandomBase64String(double byte_length) {
9+
std::vector<uint8_t> buffer(byte_length, 0);
10+
arc4random_buf(&buffer[0], buffer.size());
11+
std::string encoded_data = base64_encode(&buffer[0], buffer.size());
3412

35-
Object RNPkceChallenge = Object(jsiRuntime);
36-
RNPkceChallenge.setProperty(jsiRuntime, "getRandomBase64String", move(getRandomBase64String));
37-
jsiRuntime.global().setProperty(jsiRuntime, "PkceChallenge", move(RNPkceChallenge));
13+
return encoded_data;
3814
}
3915
}

cpp/react-native-pkce-challenge.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#define PKCECHALLENGE_H
66

77
namespace pkcechallenge {
8-
void install(facebook::jsi::Runtime &jsiRuntime);
8+
std::string getRandomBase64String(double byte_length);
99
}
1010

1111
#endif /* PKCECHALLENGE_H */

example/Gemfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ source 'https://rubygems.org'
33
# You may use http://rbenv.org/ or https://rvm.io/ to install and use this version
44
ruby ">= 2.6.10"
55

6-
gem 'cocoapods', '~> 1.13'
7-
gem 'activesupport', '>= 6.1.7.3', '< 7.1.0'
6+
# Exclude problematic versions of cocoapods and activesupport that causes build failures.
7+
gem 'cocoapods', '>= 1.13', '!= 1.15.0', '!= 1.15.1'
8+
gem 'activesupport', '>= 6.1.7.5', '!= 7.1.0'
9+
gem 'xcodeproj', '< 1.26.0'

0 commit comments

Comments
 (0)