Skip to content

Commit f542741

Browse files
Add Java templates
1 parent 7be7513 commit f542741

File tree

7 files changed

+322
-5
lines changed

7 files changed

+322
-5
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ const NATIVE_FILES = (moduleType: ModuleType) => {
2727
}
2828
};
2929

30+
// Java
31+
const JAVA_FILES = (moduleType: ModuleType) => {
32+
switch (moduleType) {
33+
case 'module':
34+
return path.resolve(__dirname, '../templates/java-library');
35+
case 'view':
36+
return path.resolve(__dirname, '../templates/java-view-library');
37+
}
38+
};
39+
3040
// Objc
3141
const OBJC_FILES = (moduleType: ModuleType) => {
3242
switch (moduleType) {
@@ -69,8 +79,12 @@ type ArgName =
6979
type ModuleType = 'module' | 'view';
7080

7181
type LibraryType =
82+
| 'native'
83+
| 'native-swift'
7284
| 'native-kotlin'
7385
| 'native-kotlin-swift'
86+
| 'native-view'
87+
| 'native-view-swift'
7488
| 'native-view-kotlin'
7589
| 'native-view-kotlin-swift'
7690
| 'cpp'
@@ -115,8 +129,12 @@ const args: Record<ArgName, yargs.Options> = {
115129
'type': {
116130
description: 'Type package do you want to develop',
117131
choices: [
132+
'native',
133+
'native-swift',
118134
'native-kotlin',
119135
'native-kotlin-swift',
136+
'native-view',
137+
'native-view-swift',
120138
'native-view-kotlin',
121139
'native-view-kotlin-swift',
122140
'cpp',
@@ -236,6 +254,8 @@ async function create(argv: yargs.Arguments<any>) {
236254
name: 'type',
237255
message: 'What type of package do you want to develop?',
238256
choices: [
257+
{ title: 'Native module in Java and Objective-C', value: 'native' },
258+
{ title: 'Native module in Java and Swift', value: 'native-swift' },
239259
{
240260
title: 'Native module in Kotlin and Objective-C',
241261
value: 'native-kotlin',
@@ -244,6 +264,15 @@ async function create(argv: yargs.Arguments<any>) {
244264
title: 'Native module in Kotlin and Swift',
245265
value: 'native-kotlin-swift',
246266
},
267+
{ title: 'Native module with C++ code', value: 'cpp' },
268+
{
269+
title: 'Native view in Java and Objective-C',
270+
value: 'native-view',
271+
},
272+
{
273+
title: 'Native view in Java and Swift',
274+
value: 'native-view-swift',
275+
},
247276
{
248277
title: 'Native view in Kotlin and Objective-C',
249278
value: 'native-view-kotlin',
@@ -252,7 +281,6 @@ async function create(argv: yargs.Arguments<any>) {
252281
title: 'Native view in Kotlin and Swift',
253282
value: 'native-view-kotlin-swift',
254283
},
255-
{ title: 'Native module with C++ code', value: 'cpp' },
256284
{
257285
title: 'JavaScript library with native example',
258286
value: 'js',
@@ -289,7 +317,10 @@ async function create(argv: yargs.Arguments<any>) {
289317

290318
const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');
291319
const moduleType: ModuleType =
292-
type === 'native-view-kotlin' || type === 'native-view-kotlin-swift'
320+
type === 'native-view' ||
321+
type === 'native-view-swift' ||
322+
type === 'native-view-kotlin' ||
323+
type === 'native-view-kotlin-swift'
293324
? 'view'
294325
: 'module';
295326

@@ -335,12 +366,25 @@ async function create(argv: yargs.Arguments<any>) {
335366
podspec: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
336367
native:
337368
type === 'cpp' ||
369+
type === 'native' ||
370+
type === 'native-swift' ||
338371
type === 'native-kotlin' ||
339372
type === 'native-kotlin-swift' ||
373+
type === 'native-view' ||
374+
type === 'native-view-swift' ||
340375
type === 'native-view-kotlin' ||
341376
type === 'native-view-kotlin-swift',
342377
cpp: type === 'cpp',
343-
swift: type === 'native-kotlin-swift' || 'native-view-kotlin-swift',
378+
kotlin:
379+
type === 'native-kotlin' ||
380+
type === 'native-kotlin-swift' ||
381+
type === 'native-view-kotlin' ||
382+
type === 'native-view-kotlin-swift',
383+
swift:
384+
type === 'native-swift' ||
385+
type === 'native-kotlin-swift' ||
386+
type === 'native-view-swift' ||
387+
type === 'native-view-kotlin-swift',
344388
module: type !== 'js',
345389
moduleType,
346390
},
@@ -405,12 +449,15 @@ async function create(argv: yargs.Arguments<any>) {
405449
}
406450

407451
if (options.project.swift) {
408-
await copyDir(KOTLIN_FILES(moduleType), folder);
409452
await copyDir(SWIFT_FILES(moduleType), folder);
410453
} else {
411-
await copyDir(KOTLIN_FILES(moduleType), folder);
412454
await copyDir(OBJC_FILES(moduleType), folder);
413455
}
456+
if (options.project.kotlin) {
457+
await copyDir(KOTLIN_FILES(moduleType), folder);
458+
} else {
459+
await copyDir(JAVA_FILES(moduleType), folder);
460+
}
414461
}
415462

416463
try {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
buildscript {
2+
if (project == rootProject) {
3+
repositories {
4+
google()
5+
jcenter()
6+
}
7+
8+
dependencies {
9+
classpath 'com.android.tools.build:gradle:3.5.3'
10+
}
11+
}
12+
}
13+
14+
apply plugin: 'com.android.library'
15+
16+
def safeExtGet(prop, fallback) {
17+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
18+
}
19+
20+
android {
21+
compileSdkVersion safeExtGet('<%- project.name %>_compileSdkVersion', 29)
22+
buildToolsVersion safeExtGet('<%- project.name %>_buildToolsVersion', '29.0.2')
23+
defaultConfig {
24+
minSdkVersion safeExtGet('<%- project.name %>_minSdkVersion', 16)
25+
targetSdkVersion safeExtGet('<%- project.name %>_targetSdkVersion', 29)
26+
versionCode 1
27+
versionName "1.0"
28+
<% if (project.cpp) { %>
29+
externalNativeBuild {
30+
cmake {
31+
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
32+
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
33+
}
34+
}
35+
<% } %>
36+
}
37+
<% if (project.cpp) { %>
38+
externalNativeBuild {
39+
cmake {
40+
path "CMakeLists.txt"
41+
}
42+
}
43+
<% } %>
44+
buildTypes {
45+
release {
46+
minifyEnabled false
47+
}
48+
}
49+
lintOptions {
50+
disable 'GradleCompatible'
51+
}
52+
compileOptions {
53+
sourceCompatibility JavaVersion.VERSION_1_8
54+
targetCompatibility JavaVersion.VERSION_1_8
55+
}
56+
}
57+
58+
repositories {
59+
mavenLocal()
60+
maven {
61+
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
62+
url("$rootDir/../node_modules/react-native/android")
63+
}
64+
google()
65+
jcenter()
66+
}
67+
68+
dependencies {
69+
//noinspection GradleDynamicVersion
70+
implementation "com.facebook.react:react-native:+" // From node_modules
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.<%- project.package %>;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import com.facebook.react.bridge.Promise;
6+
import com.facebook.react.bridge.ReactApplicationContext;
7+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
8+
import com.facebook.react.bridge.ReactMethod;
9+
import com.facebook.react.module.annotations.ReactModule;
10+
11+
@ReactModule(name = <%- project.name %>Module.NAME)
12+
public class <%- project.name %>Module extends ReactContextBaseJavaModule {
13+
public static final String NAME = "<%- project.name %>";
14+
15+
public <%- project.name %>Module(ReactApplicationContext reactContext) {
16+
super(reactContext);
17+
}
18+
19+
@Override
20+
@NonNull
21+
public String getName() {
22+
return NAME;
23+
}
24+
25+
<% if (project.cpp) { -%>
26+
static {
27+
try {
28+
// Used to load the 'native-lib' library on application startup.
29+
System.loadLibrary("cpp");
30+
} catch (Exception ignored) {
31+
}
32+
}
33+
<% } -%>
34+
35+
// Example method
36+
// See https://reactnative.dev/docs/native-modules-android
37+
@ReactMethod
38+
public void multiply(int a, int b, Promise promise) {
39+
<% if (project.cpp) { -%>
40+
promise.resolve(nativeMultiply(a, b));
41+
<% } else { -%>
42+
promise.resolve(a * b);
43+
<% } -%>
44+
}
45+
46+
public static native int nativeMultiply(int a, int b);
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.<%- project.package %>;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import com.facebook.react.ReactPackage;
6+
import com.facebook.react.bridge.NativeModule;
7+
import com.facebook.react.bridge.ReactApplicationContext;
8+
import com.facebook.react.uimanager.ViewManager;
9+
10+
import java.util.ArrayList;
11+
import java.util.Collections;
12+
import java.util.List;
13+
14+
public class <%- project.name %>Package implements ReactPackage {
15+
@NonNull
16+
@Override
17+
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
18+
List<NativeModule> modules = new ArrayList<>();
19+
modules.add(new <%- project.name %>Module(reactContext));
20+
return modules;
21+
}
22+
23+
@NonNull
24+
@Override
25+
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
26+
return Collections.emptyList();
27+
}
28+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
buildscript {
2+
if (project == rootProject) {
3+
repositories {
4+
google()
5+
jcenter()
6+
}
7+
8+
dependencies {
9+
classpath 'com.android.tools.build:gradle:3.5.3'
10+
}
11+
}
12+
}
13+
14+
apply plugin: 'com.android.library'
15+
16+
def safeExtGet(prop, fallback) {
17+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
18+
}
19+
20+
android {
21+
compileSdkVersion safeExtGet('<%- project.name %>_compileSdkVersion', 29)
22+
buildToolsVersion safeExtGet('<%- project.name %>_buildToolsVersion', '29.0.2')
23+
defaultConfig {
24+
minSdkVersion safeExtGet('<%- project.name %>_minSdkVersion', 16)
25+
targetSdkVersion safeExtGet('<%- project.name %>_targetSdkVersion', 29)
26+
versionCode 1
27+
versionName "1.0"
28+
<% if (project.cpp) { %>
29+
externalNativeBuild {
30+
cmake {
31+
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
32+
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
33+
}
34+
}
35+
<% } %>
36+
}
37+
<% if (project.cpp) { %>
38+
externalNativeBuild {
39+
cmake {
40+
path "CMakeLists.txt"
41+
}
42+
}
43+
<% } %>
44+
buildTypes {
45+
release {
46+
minifyEnabled false
47+
}
48+
}
49+
lintOptions {
50+
disable 'GradleCompatible'
51+
}
52+
compileOptions {
53+
sourceCompatibility JavaVersion.VERSION_1_8
54+
targetCompatibility JavaVersion.VERSION_1_8
55+
}
56+
}
57+
58+
repositories {
59+
mavenLocal()
60+
maven {
61+
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
62+
url("$rootDir/../node_modules/react-native/android")
63+
}
64+
google()
65+
jcenter()
66+
}
67+
68+
dependencies {
69+
//noinspection GradleDynamicVersion
70+
implementation "com.facebook.react:react-native:+" // From node_modules
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.<%- project.package %>;
2+
3+
import com.facebook.react.ReactPackage;
4+
import com.facebook.react.bridge.NativeModule;
5+
import com.facebook.react.bridge.ReactApplicationContext;
6+
import com.facebook.react.uimanager.ViewManager;
7+
8+
import java.util.Arrays;
9+
import java.util.Collections;
10+
import java.util.List;
11+
12+
public class <%- project.name %>Package implements ReactPackage {
13+
@Override
14+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
15+
return Collections.emptyList();
16+
}
17+
18+
@Override
19+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
20+
return Arrays.<ViewManager>asList(new <%- project.name %>ViewManager());
21+
}
22+
}

0 commit comments

Comments
 (0)