Skip to content

Commit

Permalink
Merge pull request #63 from zabojad/@thomasfetiveau/new-architecture
Browse files Browse the repository at this point in the history
Add support for both old and new architecture
  • Loading branch information
wumke authored Jun 27, 2023
2 parents ec9f998 + 233a3ad commit b11ba88
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 89 deletions.
8 changes: 8 additions & 0 deletions NativeRNExitApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
exitApp: () => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('RNExitApp');
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ Exit / Close / Kill / shutdown your react native app. Does not invoke a crash no
NOTICE:
- for React Native < 0.47 use react-native-exit-app <1.x.x
- for React Native > 0.47 use react-native-exit-app >=1.x.x
- React Native with **new architecture** enabled use react-native-exit-app >=2.x.x (**compatible with old architecture**)

## Setup

NOTICE:
Installation steps can be skipped in new architecture and newer React Native versions, just
```bash
npm install react-native-exit-app --save
```

Fast and easy:
```bash
npm install react-native-exit-app --save
Expand Down
19 changes: 18 additions & 1 deletion RNExitApp.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,24 @@ Pod::Spec.new do |s|
s.source = { :git => repository, :tag => version }
s.platforms = { :ios => "9.0", :tvos => "11.0" }
s.preserve_paths = 'README.md', 'package.json', '*.js'
s.source_files = 'ios/RNExitApp/**/*.{h,m}'
s.source_files = 'ios/RNExitApp/**/*.{h,m,mm}'

s.dependency 'React-Core'

# Don't install the dependencies when we run `pod install` in the old architecture.
if ENV["RCT_NEW_ARCH_ENABLED"] == "1"
s.compiler_flags = folly_flags + " -DRCT_NEW_ARCH_ENABLED=1"
s.pod_target_xcconfig = {
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
}

s.dependency "React-Codegen"
s.dependency "React-RCTFabric"
s.dependency "RCT-Folly"
s.dependency "RCTRequired"
s.dependency "RCTTypeSafety"
s.dependency "ReactCommon/turbomodule/core"
end
end
20 changes: 18 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def isNewArchitectureEnabled() {
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

apply plugin: 'com.android.library'
if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}

android {
compileSdkVersion safeExtGet('compileSdkVersion', 23)
Expand All @@ -28,15 +35,24 @@ android {
defaultConfig {
minSdkVersion safeExtGet('minSdkVersion',16)
targetSdkVersion safeExtGet('targetSdkVersion',22)
versionCode 2
versionName "1.1"
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
lintOptions {
warning 'InvalidPackage'
}

sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += ['src/newarch']
} else {
java.srcDirs += ['src/oldarch']
}
}
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.wumke.RNExitApp;

import com.facebook.react.bridge.ReactApplicationContext;

class RNExitAppImpl {

public static final String NAME = "RNExitApp";

ReactApplicationContext RCTContext;

public RNExitAppImpl(ReactApplicationContext reactContext) {
RCTContext = reactContext;
}

public void exitApp() {
android.os.Process.killProcess(android.os.Process.myPid());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
package com.github.wumke.RNExitApp;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import androidx.annotation.Nullable;

import com.facebook.react.ReactPackage;
import com.facebook.react.TurboReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;

public class RNExitAppPackage implements ReactPackage {
import java.util.HashMap;
import java.util.Map;

@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();

modules.add(new RNExitAppModule(reactContext));

return modules;
}
public class RNExitAppPackage extends TurboReactPackage {

// override removed to be compatible with rn0.47+
//@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {

return Collections.emptyList();
@Nullable
@Override
public NativeModule getModule(String name, ReactApplicationContext reactContext) {
if (name.equals(RNExitAppImpl.NAME)) {
return new RNExitApp(reactContext);
} else {
return null;
}
}

@Override
public List<ViewManager> createViewManagers(
ReactApplicationContext reactContext) {
return Collections.emptyList();
public ReactModuleInfoProvider getReactModuleInfoProvider() {
return () -> {
final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
boolean isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
moduleInfos.put(
RNExitAppImpl.NAME,
new ReactModuleInfo(
RNExitAppImpl.NAME,
RNExitAppImpl.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
false, // hasConstants
false, // isCxxModule
isTurboModule // isTurboModule
));
return moduleInfos;
};
}

}
28 changes: 28 additions & 0 deletions android/src/newarch/RNExitApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.wumke.RNExitApp;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.ReactApplicationContext;

import com.github.wumke.RNExitApp.NativeRNExitAppSpec;

public class RNExitApp extends NativeRNExitAppSpec {

private final RNExitAppImpl delegate;

public RNExitApp(ReactApplicationContext reactContext) {
super(reactContext);
delegate = new RNExitAppImpl(reactContext);
}

@NonNull
@Override
public String getName() {
return RNExitAppImpl.NAME;
}

@Override
public void exitApp() {
delegate.exitApp();
}
}
27 changes: 27 additions & 0 deletions android/src/oldarch/RNExitApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.wumke.RNExitApp;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import android.util.Log;

public class RNExitApp extends ReactContextBaseJavaModule {

private final RNExitAppImpl delegate;

public RNExitApp(ReactApplicationContext reactContext) {
super(reactContext);
delegate = new RNExitAppImpl(reactContext);
}

@Override
public String getName() {
return RNExitAppImpl.NAME;
}

@ReactMethod
public void exitApp() {
delegate.exitApp();
}
}
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {NativeModules} from 'react-native';
import NativeRNExitApp from "./NativeRNExitApp";

var RNExitApp = {
exitApp: function() {
NativeModules.RNExitApp.exitApp();
NativeRNExitApp.exitApp();
}
};

Expand Down
19 changes: 19 additions & 0 deletions ios/RNExitApp/RNExitApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#if __has_include(<React/RCTBridgeModule.h>)
#import <React/RCTBridgeModule.h>
#elif __has_include("RCTBridgeModule.h")
#import "RCTBridgeModule.h"
#else
#import "React/RCTBridgeModule.h"
#endif

#if RCT_NEW_ARCH_ENABLED
#import <React-Codegen/RNExitAppSpec/RNExitAppSpec.h>
#endif

@interface RNExitApp : NSObject <RCTBridgeModule>
@end

#if RCT_NEW_ARCH_ENABLED
@interface RNExitApp () <NativeRNExitAppSpec>
@end
#endif
17 changes: 0 additions & 17 deletions ios/RNExitApp/RNExitApp.m

This file was deleted.

30 changes: 30 additions & 0 deletions ios/RNExitApp/RNExitApp.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#import <UIKit/UIKit.h>

#import "RNExitApp.h"

#if RCT_NEW_ARCH_ENABLED
#import <RNExitAppSpec/RNExitAppSpec.h>
#endif

@implementation RNExitApp

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(exitApp)
{
exit(0);
};

# pragma mark - New Architecture

#if RCT_NEW_ARCH_ENABLED

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeRNExitAppSpecJSI>(params);
}

#endif

@end
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-exit-app",
"version": "1.1.0",
"version": "2.0.0",
"description": "Exit,close,kill,shutdown app completely for React Native on iOS and Android.",
"main": "index.js",
"scripts": {
Expand All @@ -26,5 +26,13 @@
"bugs": {
"url": "https://github.com/wumke/react-native-exit-app/issues"
},
"homepage": "http://watchwimswork.be"
"homepage": "https://github.com/wumke/react-native-exit-app",
"codegenConfig": {
"name": "RNExitAppSpec",
"type": "modules",
"jsSrcsDir": ".",
"android": {
"javaPackageName": "com.github.wumke.RNExitApp"
}
}
}

0 comments on commit b11ba88

Please sign in to comment.