Skip to content

Commit c870e04

Browse files
committed
feat: add android support
1 parent a668206 commit c870e04

File tree

11 files changed

+72
-149
lines changed

11 files changed

+72
-149
lines changed

android/build.gradle

Lines changed: 8 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
// android/build.gradle
22

3-
// based on:
4-
//
5-
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle
6-
// original location:
7-
// - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/build.gradle
8-
//
9-
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/app/build.gradle
10-
// original location:
11-
// - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/app/build.gradle
12-
133
buildscript {
144
// The Android Gradle plugin is only required when opening the android folder stand-alone.
155
// This avoids unnecessary downloads and potential conflicts when the library is included as a
166
// module dependency in an application project.
17-
// ref: https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:build_script_external_dependencies
187
if (project == rootProject) {
198
repositories {
209
google()
@@ -26,11 +15,6 @@ buildscript {
2615
}
2716
}
2817

29-
plugins {
30-
id('com.android.library')
31-
id('maven-publish')
32-
}
33-
3418
def DEFAULT_COMPILE_SDK_VERSION = 28
3519
def DEFAULT_BUILD_TOOLS_VERSION = '28.0.3'
3620
def DEFAULT_MIN_SDK_VERSION = 18
@@ -40,9 +24,15 @@ def safeExtGet(prop, fallback) {
4024
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
4125
}
4226

27+
apply plugin: 'com.android.library'
28+
apply plugin: 'com.facebook.react'
29+
4330
android {
4431
compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
4532
buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
33+
34+
namespace "com.reactnativelauncharguments"
35+
4636
defaultConfig {
4737
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
4838
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
@@ -55,7 +45,6 @@ android {
5545
}
5646

5747
repositories {
58-
// ref: https://www.baeldung.com/maven-local-repository
5948
mavenLocal()
6049
maven {
6150
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
@@ -66,68 +55,10 @@ repositories {
6655
url "$rootDir/../node_modules/jsc-android/dist"
6756
}
6857
google()
58+
mavenCentral()
6959
}
7060

7161
dependencies {
7262
//noinspection GradleDynamicVersion
73-
implementation 'com.facebook.react:react-native:+' // From node_modules
74-
}
75-
76-
def configureReactNativePom(def pom) {
77-
def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)
78-
79-
pom.project {
80-
name packageJson.title
81-
artifactId packageJson.name
82-
version = packageJson.version
83-
group = "com.reactnativelauncharguments"
84-
description packageJson.description
85-
url packageJson.repository.baseUrl
86-
87-
licenses {
88-
license {
89-
name packageJson.license
90-
url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
91-
distribution 'repo'
92-
}
93-
}
94-
95-
developers {
96-
developer {
97-
id packageJson.author.username
98-
name packageJson.author.name
99-
}
100-
}
101-
}
102-
}
103-
104-
afterEvaluate { project ->
105-
// some Gradle build hooks ref:
106-
// https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html
107-
task androidSourcesJar(type: Jar) {
108-
archiveClassifier = 'sources'
109-
from android.sourceSets.main.java.srcDirs
110-
include '**/*.java'
111-
}
112-
113-
android.libraryVariants.all { variant ->
114-
def name = variant.name.capitalize()
115-
def javaCompileTask = variant.javaCompileProvider.get()
116-
117-
task "jar${name}"(type: Jar, dependsOn: javaCompileTask) {
118-
from javaCompileTask.destinationDir
119-
}
120-
}
121-
122-
artifacts {
123-
archives androidSourcesJar
124-
}
125-
126-
publishing {
127-
publications {
128-
maven(MavenPublication) {
129-
artifact androidSourcesJar
130-
}
131-
}
132-
}
63+
implementation 'com.facebook.react:react-native:+'
13364
}

android/src/main/java/com/reactnativelauncharguments/LaunchArgumentsModule.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@
66
import android.content.Intent;
77
import android.os.Bundle;
88

9+
import com.facebook.fbreact.specs.NativeLaunchArgumentsSpec;
910
import com.facebook.react.bridge.ReactApplicationContext;
10-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
11-
import com.facebook.react.bridge.ReactMethod;
1211

13-
import java.io.Serializable;
1412
import java.util.HashMap;
1513
import java.util.List;
1614
import java.util.Map;
1715

18-
import androidx.annotation.NonNull;
19-
import androidx.annotation.Nullable;
20-
21-
public class LaunchArgumentsModule extends ReactContextBaseJavaModule {
16+
public class LaunchArgumentsModule extends NativeLaunchArgumentsSpec {
2217

2318
private static final long ACTIVITY_WAIT_INTERVAL = 100L;
2419
private static final int ACTIVITY_WAIT_TRIES = 200;
@@ -29,15 +24,8 @@ public class LaunchArgumentsModule extends ReactContextBaseJavaModule {
2924
super(context);
3025
}
3126

32-
@NonNull
3327
@Override
34-
public String getName() {
35-
return "LaunchArguments";
36-
}
37-
38-
@Nullable
39-
@Override
40-
public Map<String, Object> getConstants() {
28+
protected Map<String, Object> getTypedExportedConstants() {
4129
// When the app is killed, it doesn't start the activity so no need to wait for it
4230
if (!isAppKilled()) {
4331
// This is work-around for the RN problem described here:
@@ -52,13 +40,6 @@ public Map<String, Object> getConstants() {
5240
}};
5341
}
5442

55-
/**
56-
* Looks like a bug in RN, without it this module is invisible
57-
* in NativeModules.
58-
*/
59-
@ReactMethod
60-
public void foo() {}
61-
6243
private void waitForActivity() {
6344
for (int tries = 0; tries < ACTIVITY_WAIT_TRIES && !isActivityReady(); tries++) {
6445
sleep(ACTIVITY_WAIT_INTERVAL);
Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
package com.reactnativelauncharguments;
22

3-
import com.facebook.react.ReactPackage;
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
5+
6+
import com.facebook.react.TurboReactPackage;
47
import com.facebook.react.bridge.NativeModule;
58
import com.facebook.react.bridge.ReactApplicationContext;
6-
import com.facebook.react.uimanager.ViewManager;
9+
import com.facebook.react.module.model.ReactModuleInfo;
10+
import com.facebook.react.module.model.ReactModuleInfoProvider;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
714

8-
import java.util.Collections;
9-
import java.util.List;
15+
public class LaunchArgumentsPackage extends TurboReactPackage {
1016

11-
public class LaunchArgumentsPackage implements ReactPackage {
12-
17+
@Nullable
1318
@Override
14-
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
15-
return Collections.emptyList();
19+
public NativeModule getModule(@NonNull String name, @NonNull ReactApplicationContext reactContext) {
20+
if (name.equals(LaunchArgumentsModule.NAME)) {
21+
return new LaunchArgumentsModule(reactContext);
22+
} else {
23+
return null;
24+
}
1625
}
1726

1827
@Override
19-
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
20-
final NativeModule nativeModule = new LaunchArgumentsModule(reactContext);
21-
return Collections.singletonList(nativeModule);
28+
public ReactModuleInfoProvider getReactModuleInfoProvider() {
29+
return () -> {
30+
final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
31+
moduleInfos.put(
32+
LaunchArgumentsModule.NAME,
33+
new ReactModuleInfo(
34+
LaunchArgumentsModule.NAME,
35+
LaunchArgumentsModule.NAME,
36+
false, // canOverrideExistingModule
37+
false, // needsEagerInit
38+
true, // hasConstants
39+
false, // isCxxModule
40+
true // isTurboModule
41+
));
42+
return moduleInfos;
43+
};
2244
}
2345
}

ios/LaunchArguments.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#import <React/RCTBridgeModule.h>
22

3-
#ifdef RCT_NEW_ARCH_ENABLED
4-
#import <React/RCTTurboModule.h>
5-
#import "RCTNativeLaunchArgumentsSpec.h"
3+
#import <ReactCommon/RCTTurboModule.h>
4+
#import <RNLaunchArgumentsSpec/RNLaunchArgumentsSpec.h>
65

76
@interface LaunchArguments : NSObject <NativeLaunchArgumentsSpec>
8-
#else
9-
@interface LaunchArguments : NSObject <RCTBridgeModule>
10-
#endif
117

128
@end
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ @implementation LaunchArguments
44

55
RCT_EXPORT_MODULE()
66

7-
#ifdef RCT_NEW_ARCH_ENABLED
87
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
98
(const facebook::react::ObjCTurboModule::InitParams &)params
109
{
1110
return std::make_shared<facebook::react::NativeLaunchArgumentsSpecJSI>(params);
1211
}
13-
#endif
1412

1513
- (dispatch_queue_t)methodQueue
1614
{
@@ -115,12 +113,4 @@ - (NSString *)cleanValue:(NSString *)arg
115113
return [arg stringByReplacingOccurrencesOfString:@"-" withString:@""];
116114
}
117115

118-
#ifdef RCT_NEW_ARCH_ENABLED
119-
- (facebook::react::ModuleConstants<JS::NativeLaunchArguments::Constants>)getConstants {
120-
return facebook::react::typedConstants<JS::NativeLaunchArguments::Constants>({
121-
.value = facebook::react::toDynamic([self argsToDictionary])
122-
});
123-
}
124-
#endif
125-
126116
@end

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"react-native": ">=0.60.0-rc.0 <1.0.x"
3232
},
3333
"devDependencies": {
34-
"react-native": "0.79.6",
3534
"react": "19.0.0",
35+
"react-native": "0.79.6",
3636
"typescript": "5.8.2"
3737
},
3838
"codegenConfig": {

react-native-launch-arguments.podspec

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@ Pod::Spec.new do |s|
1212
s.platforms = { :ios => "9.0" }
1313
s.source = { :git => "#{package["repository"]["baseUrl"]}.git", :tag => "#{s.version}" }
1414

15-
s.source_files = "ios/**/*.{h,m,mm}"
15+
s.source_files = "ios/**/*.{h,m,mm,swift}"
1616
s.requires_arc = true
1717

1818
install_modules_dependencies(s)
1919

20-
if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
21-
s.pod_target_xcconfig = {
22-
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/RCT-Folly\"",
23-
"DEFINES_MODULE" => "YES",
24-
"SWIFT_OBJC_INTERFACE_HEADER_NAME" => "$(SWIFT_MODULE_NAME)-Swift.h"
25-
}
20+
s.pod_target_xcconfig = {
21+
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/RCT-Folly\"",
22+
"DEFINES_MODULE" => "YES",
23+
"SWIFT_OBJC_INTERFACE_HEADER_NAME" => "$(SWIFT_MODULE_NAME)-Swift.h"
24+
}
2625

27-
s.compiler_flags = '-DRCT_NEW_ARCH_ENABLED'
28-
end
26+
s.compiler_flags = '-DRCT_NEW_ARCH_ENABLED'
2927
end

react-native.config.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
module.exports = {
22
dependency: {
33
platforms: {
4-
android: {
5-
sourceDir: '../android/src/main/java/com/reactnativelauncharguments',
6-
packageImportPath: 'import com.reactnativelauncharguments.LaunchArgumentsPackage;',
7-
componentDescriptors: null,
8-
cmakeListsPath: null,
9-
},
10-
ios: {
11-
libraryFolder: '../ios',
12-
sharedLibraries: [],
13-
project: 'LaunchArguments.xcodeproj',
14-
podspecPath: '../react-native-launch-arguments.podspec',
15-
},
4+
android: {},
5+
ios: {},
166
},
177
},
188
};

src/NativeLaunchArguments.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { TurboModule } from 'react-native';
2+
import { TurboModuleRegistry } from 'react-native';
3+
4+
export interface Spec extends TurboModule {
5+
readonly getConstants: () => {
6+
readonly value: { [key: string]: string };
7+
};
8+
}
9+
10+
export default TurboModuleRegistry.getEnforcing<Spec>('LaunchArguments');

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
"strict": true,
2323
"target": "esnext"
2424
},
25-
"include": ["src"]
25+
"include": ["src", "typings/global.d.ts"]
2626
}

0 commit comments

Comments
 (0)