Skip to content

Commit 69d10c5

Browse files
Add Java templates
1 parent f74a179 commit 69d10c5

File tree

7 files changed

+322
-5
lines changed

7 files changed

+322
-5
lines changed

src/create.ts

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

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

7282
type LibraryType =
83+
| 'native'
84+
| 'native-swift'
7385
| 'native-kotlin'
7486
| 'native-kotlin-swift'
87+
| 'native-view'
88+
| 'native-view-swift'
7589
| 'native-view-kotlin'
7690
| 'native-view-kotlin-swift'
7791
| 'cpp'
@@ -116,8 +130,12 @@ export const args: Record<ArgName, yargs.Options> = {
116130
'type': {
117131
description: 'Type package do you want to develop',
118132
choices: [
133+
'native',
134+
'native-swift',
119135
'native-kotlin',
120136
'native-kotlin-swift',
137+
'native-view',
138+
'native-view-swift',
121139
'native-view-kotlin',
122140
'native-view-kotlin-swift',
123141
'cpp',
@@ -237,6 +255,8 @@ export default async function create(argv: yargs.Arguments<any>) {
237255
name: 'type',
238256
message: 'What type of package do you want to develop?',
239257
choices: [
258+
{ title: 'Native module in Java and Objective-C', value: 'native' },
259+
{ title: 'Native module in Java and Swift', value: 'native-swift' },
240260
{
241261
title: 'Native module in Kotlin and Objective-C',
242262
value: 'native-kotlin',
@@ -245,6 +265,15 @@ export default async function create(argv: yargs.Arguments<any>) {
245265
title: 'Native module in Kotlin and Swift',
246266
value: 'native-kotlin-swift',
247267
},
268+
{ title: 'Native module with C++ code', value: 'cpp' },
269+
{
270+
title: 'Native view in Java and Objective-C',
271+
value: 'native-view',
272+
},
273+
{
274+
title: 'Native view in Java and Swift',
275+
value: 'native-view-swift',
276+
},
248277
{
249278
title: 'Native view in Kotlin and Objective-C',
250279
value: 'native-view-kotlin',
@@ -253,7 +282,6 @@ export default async function create(argv: yargs.Arguments<any>) {
253282
title: 'Native view in Kotlin and Swift',
254283
value: 'native-view-kotlin-swift',
255284
},
256-
{ title: 'Native module with C++ code', value: 'cpp' },
257285
{
258286
title: 'JavaScript library with native example',
259287
value: 'js',
@@ -290,7 +318,10 @@ export default async function create(argv: yargs.Arguments<any>) {
290318

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

@@ -310,12 +341,25 @@ export default async function create(argv: yargs.Arguments<any>) {
310341
podspec: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
311342
native:
312343
type === 'cpp' ||
344+
type === 'native' ||
345+
type === 'native-swift' ||
313346
type === 'native-kotlin' ||
314347
type === 'native-kotlin-swift' ||
348+
type === 'native-view' ||
349+
type === 'native-view-swift' ||
315350
type === 'native-view-kotlin' ||
316351
type === 'native-view-kotlin-swift',
317352
cpp: type === 'cpp',
318-
swift: type === 'native-kotlin-swift' || 'native-view-kotlin-swift',
353+
kotlin:
354+
type === 'native-kotlin' ||
355+
type === 'native-kotlin-swift' ||
356+
type === 'native-view-kotlin' ||
357+
type === 'native-view-kotlin-swift',
358+
swift:
359+
type === 'native-swift' ||
360+
type === 'native-kotlin-swift' ||
361+
type === 'native-view-swift' ||
362+
type === 'native-view-kotlin-swift',
319363
module: type !== 'js',
320364
moduleType,
321365
},
@@ -380,12 +424,15 @@ export default async function create(argv: yargs.Arguments<any>) {
380424
}
381425

382426
if (options.project.swift) {
383-
await copyDir(KOTLIN_FILES(moduleType), folder);
384427
await copyDir(SWIFT_FILES(moduleType), folder);
385428
} else {
386-
await copyDir(KOTLIN_FILES(moduleType), folder);
387429
await copyDir(OBJC_FILES(moduleType), folder);
388430
}
431+
if (options.project.kotlin) {
432+
await copyDir(KOTLIN_FILES(moduleType), folder);
433+
} else {
434+
await copyDir(JAVA_FILES(moduleType), folder);
435+
}
389436
}
390437

391438
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+
}
Lines changed: 47 additions & 0 deletions
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+
}
Lines changed: 28 additions & 0 deletions
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)