Skip to content

Commit ded0d34

Browse files
authored
Merge pull request #8 from jonthysell/nmsample
Adding Native Modules Samples
2 parents 421a96e + 9d55131 commit ded0d34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+11774
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Native Module Sample - React Native for Windows
2+
These samples showcases building Native Modules for React Native for Windows vNext. It includes implementations in C# and C++/WinRT.
3+
4+
The official documentation can be found here:
5+
6+
* [Native Modules Setup](https://github.com/microsoft/react-native-windows/blob/master/vnext/docs/NativeModulesSetup.md)
7+
* [Native Modules and React Native Windows](https://github.com/microsoft/react-native-windows/blob/master/vnext/docs/NativeModules.md)
8+
* [Native Modules and React Native Windows (Advanced Topics)](https://github.com/microsoft/react-native-windows/blob/master/vnext/docs/NativeModulesAdvanced.md)
9+
* [Native UI Components](https://github.com/microsoft/react-native-windows/blob/master/vnext/docs/ViewManagers.md)
10+
11+
>**Note: Don't build your own projects directly out of these samples. When you publish a native module (as source), you'll want to create a new project with the correct metadata. This will also make sure that you're using unique identifiers in your project files to avoid conflicts with other native modules.**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# OSX
2+
#
3+
.DS_Store
4+
5+
# node.js
6+
#
7+
node_modules/
8+
npm-debug.log
9+
yarn-error.log
10+
11+
# Xcode
12+
#
13+
build/
14+
*.pbxuser
15+
!default.pbxuser
16+
*.mode1v3
17+
!default.mode1v3
18+
*.mode2v3
19+
!default.mode2v3
20+
*.perspectivev3
21+
!default.perspectivev3
22+
xcuserdata
23+
*.xccheckout
24+
*.moved-aside
25+
DerivedData
26+
*.hmap
27+
*.ipa
28+
*.xcuserstate
29+
project.xcworkspace
30+
31+
# Android/IntelliJ
32+
#
33+
build/
34+
.idea
35+
.gradle
36+
local.properties
37+
*.iml
38+
39+
# BUCK
40+
buck-out/
41+
\.buckd/
42+
*.keystore

samples/NativeModuleSample/cppwinrt/.npmignore

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# react-native-native-module-sample
2+
3+
## Getting started
4+
5+
`$ npm install react-native-native-module-sample --save`
6+
7+
### Mostly automatic installation
8+
9+
`$ react-native link react-native-native-module-sample`
10+
11+
### Manual installation
12+
13+
14+
#### iOS
15+
16+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
17+
2. Go to `node_modules``react-native-native-module-sample` and add `NativeModuleSample.xcodeproj`
18+
3. In XCode, in the project navigator, select your project. Add `libNativeModuleSample.a` to your project's `Build Phases``Link Binary With Libraries`
19+
4. Run your project (`Cmd+R`)<
20+
21+
#### Android
22+
23+
1. Open up `android/app/src/main/java/[...]/MainApplication.java`
24+
- Add `import com.reactlibrary.NativeModuleSamplePackage;` to the imports at the top of the file
25+
- Add `new NativeModuleSamplePackage()` to the list returned by the `getPackages()` method
26+
2. Append the following lines to `android/settings.gradle`:
27+
```
28+
include ':react-native-native-module-sample'
29+
project(':react-native-native-module-sample').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-native-module-sample/android')
30+
```
31+
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
32+
```
33+
compile project(':react-native-native-module-sample')
34+
```
35+
36+
37+
## Usage
38+
```javascript
39+
import NativeModuleSample from 'react-native-native-module-sample';
40+
41+
// TODO: What to do with the module?
42+
NativeModuleSample;
43+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
README
2+
======
3+
4+
If you want to publish the lib as a maven dependency, follow these steps before publishing a new version to npm:
5+
6+
1. Be sure to have the Android [SDK](https://developer.android.com/studio/index.html) and [NDK](https://developer.android.com/ndk/guides/index.html) installed
7+
2. Be sure to have a `local.properties` file in this folder that points to the Android SDK and NDK
8+
```
9+
ndk.dir=/Users/{username}/Library/Android/sdk/ndk-bundle
10+
sdk.dir=/Users/{username}/Library/Android/sdk
11+
```
12+
3. Delete the `maven` folder
13+
4. Run `./gradlew installArchives`
14+
5. Verify that latest set of generated files is in the maven folder with the correct version number
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// android/build.gradle
2+
3+
def safeExtGet(prop, fallback) {
4+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
5+
}
6+
7+
buildscript {
8+
// The Android Gradle plugin is only required when opening the android folder stand-alone.
9+
// This avoids unnecessary downloads and potential conflicts when the library is included as a
10+
// module dependency in an application project.
11+
if (project == rootProject) {
12+
repositories {
13+
google()
14+
jcenter()
15+
}
16+
dependencies {
17+
classpath 'com.android.tools.build:gradle:3.4.1'
18+
}
19+
}
20+
}
21+
22+
apply plugin: 'com.android.library'
23+
apply plugin: 'maven'
24+
25+
// Matches values in recent template from React Native 0.59 / 0.60
26+
// https://github.com/facebook/react-native/blob/0.59-stable/template/android/build.gradle#L5-L9
27+
// https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle#L5-L9
28+
def DEFAULT_COMPILE_SDK_VERSION = 28
29+
def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3"
30+
def DEFAULT_MIN_SDK_VERSION = 16
31+
def DEFAULT_TARGET_SDK_VERSION = 28
32+
33+
android {
34+
compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
35+
buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
36+
defaultConfig {
37+
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
38+
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
39+
versionCode 1
40+
versionName "1.0"
41+
}
42+
lintOptions {
43+
abortOnError false
44+
}
45+
}
46+
47+
repositories {
48+
mavenLocal()
49+
maven {
50+
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
51+
url "$rootDir/../node_modules/react-native/android"
52+
}
53+
maven {
54+
// Android JSC is installed from npm
55+
url "$rootDir/../node_modules/jsc-android/dist"
56+
}
57+
google()
58+
jcenter()
59+
}
60+
61+
dependencies {
62+
// ref:
63+
// https://github.com/facebook/react-native/blob/0.61-stable/template/android/app/build.gradle#L192
64+
//noinspection GradleDynamicVersion
65+
implementation 'com.facebook.react:react-native:+' // From node_modules
66+
}
67+
68+
def configureReactNativePom(def pom) {
69+
def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)
70+
71+
pom.project {
72+
name packageJson.title
73+
artifactId packageJson.name
74+
version = packageJson.version
75+
group = "com.reactlibrary"
76+
description packageJson.description
77+
url packageJson.repository.baseUrl
78+
79+
licenses {
80+
license {
81+
name packageJson.license
82+
url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
83+
distribution 'repo'
84+
}
85+
}
86+
87+
developers {
88+
developer {
89+
id packageJson.author.username
90+
name packageJson.author.name
91+
}
92+
}
93+
}
94+
}
95+
96+
afterEvaluate { project ->
97+
// some Gradle build hooks ref:
98+
// https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html
99+
task androidJavadoc(type: Javadoc) {
100+
source = android.sourceSets.main.java.srcDirs
101+
classpath += files(android.bootClasspath)
102+
classpath += files(project.getConfigurations().getByName('compile').asList())
103+
include '**/*.java'
104+
}
105+
106+
task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
107+
classifier = 'javadoc'
108+
from androidJavadoc.destinationDir
109+
}
110+
111+
task androidSourcesJar(type: Jar) {
112+
classifier = 'sources'
113+
from android.sourceSets.main.java.srcDirs
114+
include '**/*.java'
115+
}
116+
117+
android.libraryVariants.all { variant ->
118+
def name = variant.name.capitalize()
119+
task "jar${name}"(type: Jar, dependsOn: variant.javaCompile) {
120+
from variant.javaCompile.destinationDir
121+
}
122+
}
123+
124+
artifacts {
125+
archives androidSourcesJar
126+
archives androidJavadocJar
127+
}
128+
129+
task installArchives(type: Upload) {
130+
configuration = configurations.archives
131+
repositories.mavenDeployer {
132+
// Deploy to react-native-event-bridge/maven, ready to publish to npm
133+
repository url: "file://${projectDir}/../android/maven"
134+
configureReactNativePom pom
135+
}
136+
}
137+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.reactlibrary">
3+
4+
</manifest>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.reactlibrary;
2+
3+
import com.facebook.react.bridge.ReactApplicationContext;
4+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
5+
import com.facebook.react.bridge.ReactMethod;
6+
import com.facebook.react.bridge.Callback;
7+
8+
public class NativeModuleSampleModule extends ReactContextBaseJavaModule {
9+
10+
private final ReactApplicationContext reactContext;
11+
12+
public NativeModuleSampleModule(ReactApplicationContext reactContext) {
13+
super(reactContext);
14+
this.reactContext = reactContext;
15+
}
16+
17+
@Override
18+
public String getName() {
19+
return "NativeModuleSample";
20+
}
21+
22+
@ReactMethod
23+
public void sampleMethod(String stringArgument, int numberArgument, Callback callback) {
24+
// TODO: Implement some actually useful functionality
25+
callback.invoke("Received numberArgument: " + numberArgument + " stringArgument: " + stringArgument);
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.reactlibrary;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
import com.facebook.react.ReactPackage;
8+
import com.facebook.react.bridge.NativeModule;
9+
import com.facebook.react.bridge.ReactApplicationContext;
10+
import com.facebook.react.uimanager.ViewManager;
11+
import com.facebook.react.bridge.JavaScriptModule;
12+
13+
public class NativeModuleSamplePackage implements ReactPackage {
14+
@Override
15+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
16+
return Arrays.<NativeModule>asList(new NativeModuleSampleModule(reactContext));
17+
}
18+
19+
@Override
20+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
21+
return Collections.emptyList();
22+
}
23+
}

0 commit comments

Comments
 (0)