Skip to content

Commit a421407

Browse files
friederbluemleokwasniewski
authored andcommitted
feat: add support for Java templates (#129)
1 parent dc4919e commit a421407

File tree

26 files changed

+386
-392
lines changed

26 files changed

+386
-392
lines changed

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

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ const JS_FILES = path.resolve(__dirname, '../templates/js-library');
1616
const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library');
1717
const CPP_FILES = path.resolve(__dirname, '../templates/cpp-library');
1818
const EXAMPLE_FILES = path.resolve(__dirname, '../templates/example');
19+
const NATIVE_COMMON_FILES = path.resolve(
20+
__dirname,
21+
'../templates/native-common'
22+
);
1923

20-
// Android
21-
const NATIVE_FILES = (moduleType: ModuleType) => {
24+
// Java
25+
const JAVA_FILES = (moduleType: ModuleType) => {
2226
switch (moduleType) {
2327
case 'module':
24-
return path.resolve(__dirname, '../templates/native-library');
28+
return path.resolve(__dirname, '../templates/java-library');
2529
case 'view':
26-
return path.resolve(__dirname, '../templates/native-view-library');
30+
return path.resolve(__dirname, '../templates/java-view-library');
2731
}
2832
};
2933

@@ -37,6 +41,16 @@ const OBJC_FILES = (moduleType: ModuleType) => {
3741
}
3842
};
3943

44+
// Kotlin
45+
const KOTLIN_FILES = (moduleType: ModuleType) => {
46+
switch (moduleType) {
47+
case 'module':
48+
return path.resolve(__dirname, '../templates/kotlin-library');
49+
case 'view':
50+
return path.resolve(__dirname, '../templates/kotlin-view-library');
51+
}
52+
};
53+
4054
// Swift
4155
const SWIFT_FILES = (moduleType: ModuleType) => {
4256
switch (moduleType) {
@@ -59,12 +73,16 @@ type ArgName =
5973
type ModuleType = 'module' | 'view';
6074

6175
type LibraryType =
62-
| 'native-view'
63-
| 'native-view-swift'
6476
| 'native'
6577
| 'native-swift'
66-
| 'js'
78+
| 'native-kotlin'
79+
| 'native-kotlin-swift'
80+
| 'native-view'
81+
| 'native-view-swift'
82+
| 'native-view-kotlin'
83+
| 'native-view-kotlin-swift'
6784
| 'cpp'
85+
| 'js'
6886
| 'expo';
6987

7088
type Answers = {
@@ -104,7 +122,19 @@ const args: Record<ArgName, yargs.Options> = {
104122
},
105123
'type': {
106124
description: 'Type package do you want to develop',
107-
choices: ['native', 'native-swift', 'js', 'cpp', 'expo'],
125+
choices: [
126+
'native',
127+
'native-swift',
128+
'native-kotlin',
129+
'native-kotlin-swift',
130+
'native-view',
131+
'native-view-swift',
132+
'native-view-kotlin',
133+
'native-view-kotlin-swift',
134+
'cpp',
135+
'js',
136+
'expo',
137+
],
108138
},
109139
};
110140

@@ -218,17 +248,33 @@ async function create(argv: yargs.Arguments<any>) {
218248
name: 'type',
219249
message: 'What type of package do you want to develop?',
220250
choices: [
221-
{ title: 'Native module in Kotlin and Objective-C', value: 'native' },
222-
{ title: 'Native module in Kotlin and Swift', value: 'native-swift' },
251+
{ title: 'Native module in Java and Objective-C', value: 'native' },
252+
{ title: 'Native module in Java and Swift', value: 'native-swift' },
253+
{
254+
title: 'Native module in Kotlin and Objective-C',
255+
value: 'native-kotlin',
256+
},
257+
{
258+
title: 'Native module in Kotlin and Swift',
259+
value: 'native-kotlin-swift',
260+
},
223261
{ title: 'Native module with C++ code', value: 'cpp' },
224262
{
225-
title: 'Native view in Kotlin and Objective-C',
263+
title: 'Native view in Java and Objective-C',
226264
value: 'native-view',
227265
},
228266
{
229-
title: 'Native view in Kotlin and Swift',
267+
title: 'Native view in Java and Swift',
230268
value: 'native-view-swift',
231269
},
270+
{
271+
title: 'Native view in Kotlin and Objective-C',
272+
value: 'native-view-kotlin',
273+
},
274+
{
275+
title: 'Native view in Kotlin and Swift',
276+
value: 'native-view-kotlin-swift',
277+
},
232278
{
233279
title: 'JavaScript library with native example',
234280
value: 'js',
@@ -265,7 +311,12 @@ async function create(argv: yargs.Arguments<any>) {
265311

266312
const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');
267313
const moduleType: ModuleType =
268-
type === 'native-view' || type === 'native-view-swift' ? 'view' : 'module';
314+
type === 'native-view' ||
315+
type === 'native-view-swift' ||
316+
type === 'native-view-kotlin' ||
317+
type === 'native-view-kotlin-swift'
318+
? 'view'
319+
: 'module';
269320

270321
// Get latest version of Bob from NPM
271322
let version: string;
@@ -308,13 +359,26 @@ async function create(argv: yargs.Arguments<any>) {
308359
package: slug.replace(/[^a-z0-9]/g, '').toLowerCase(),
309360
podspec: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
310361
native:
311-
type === 'native' ||
312362
type === 'cpp' ||
313-
'native-swift' ||
314-
'native-view' ||
315-
'native-view-swift',
363+
type === 'native' ||
364+
type === 'native-swift' ||
365+
type === 'native-kotlin' ||
366+
type === 'native-kotlin-swift' ||
367+
type === 'native-view' ||
368+
type === 'native-view-swift' ||
369+
type === 'native-view-kotlin' ||
370+
type === 'native-view-kotlin-swift',
316371
cpp: type === 'cpp',
317-
swift: type === 'native-swift' || 'native-view-swift',
372+
kotlin:
373+
type === 'native-kotlin' ||
374+
type === 'native-kotlin-swift' ||
375+
type === 'native-view-kotlin' ||
376+
type === 'native-view-kotlin-swift',
377+
swift:
378+
type === 'native-swift' ||
379+
type === 'native-kotlin-swift' ||
380+
type === 'native-view-swift' ||
381+
type === 'native-view-kotlin-swift',
318382
module: type !== 'js',
319383
moduleType,
320384
},
@@ -372,15 +436,22 @@ async function create(argv: yargs.Arguments<any>) {
372436
path.join(folder, 'example')
373437
);
374438

375-
await copyDir(NATIVE_FILES(moduleType), folder);
439+
await copyDir(NATIVE_COMMON_FILES, folder);
376440

377-
if (type === 'cpp') {
441+
if (options.project.cpp) {
378442
await copyDir(CPP_FILES, folder);
379-
} else if (type === 'native-swift') {
443+
}
444+
445+
if (options.project.swift) {
380446
await copyDir(SWIFT_FILES(moduleType), folder);
381447
} else {
382448
await copyDir(OBJC_FILES(moduleType), folder);
383449
}
450+
if (options.project.kotlin) {
451+
await copyDir(KOTLIN_FILES(moduleType), folder);
452+
} else {
453+
await copyDir(JAVA_FILES(moduleType), folder);
454+
}
384455
}
385456

386457
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+
}

0 commit comments

Comments
 (0)