├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── android ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── io │ └── cmichel │ └── boilerplate │ ├── Module.java │ └── Package.java ├── index.android.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | build/ 3 | npm-debug.log 4 | system-notification.iml 5 | react-native-system-notification.iml 6 | .DS_Store 7 | .idea/ 8 | android/.idea/ 9 | *.iml -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | android/build 3 | android/build/* 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## React Native Android Library Boilerplate 2 | This project serves as a boilerplate to create custom React Native native modules that can later be installed through NPM and easily be used in production. 3 | 4 | ## Getting started 5 | 1. Clone the project 6 | 2. Customize the project name by doing the following: 7 | * Edit `author` and `name` in `package.json` 8 | * Customize the Java package name (`com.domain.package`) as follows: 9 | 1. Modify it in `android/src/main/AndroidManifest.xml`. 10 | 2. Rename the folders starting from `android/src/main/java` to match your package name. 11 | 3. Adjust `package io.cmichel.boilerplate;` in the top of the `Module.java` and `Package.java` files in `android/src/main//java/package/path` to match it. 12 | * Edit the name of your module in 13 | 14 | ```java 15 | @Override 16 | public String getName() { 17 | return "Boilerplate"; 18 | } 19 | ``` 20 | 21 | and adjust it in `index.android.js` 22 | 3. Modify/Build the Project in Android Studio 23 | * Start `Android Studio` and select `File -> New -> Import Project` and select the **android** folder of this package. 24 | * If you get a `Plugin with id 'android-library' not found` Error, install `android support repository`. 25 | * If you get asked to upgrade _gradle_ to a new version, you can skip it. 26 | 27 | ## Installing it as a library in your main project 28 | There are many ways to do this, here's the way I do it: 29 | 30 | 1. Push it to **GitHub**. 31 | 2. Do `npm install --save git+https://github.com/MrToph/react-native-android-library-boilerplate.git` in your main project. 32 | 3. Link the library: 33 | * Add the following to `android/settings.gradle`: 34 | ``` 35 | include ':react-native-android-library-boilerplate' 36 | project(':react-native-android-library-boilerplate').projectDir = new File(settingsDir, '../node_modules/react-native-android-library-boilerplate/android') 37 | ``` 38 | 39 | * Add the following to `android/app/build.gradle`: 40 | ```xml 41 | ... 42 | 43 | dependencies { 44 | ... 45 | compile project(':react-native-android-library-boilerplate') 46 | } 47 | ``` 48 | * Add the following to `android/app/src/main/java/**/MainApplication.java`: 49 | ```java 50 | package com.motivation; 51 | 52 | import io.cmichel.boilerplate.Package; // add this for react-native-android-library-boilerplate 53 | 54 | public class MainApplication extends Application implements ReactApplication { 55 | 56 | @Override 57 | protected List getPackages() { 58 | return Arrays.asList( 59 | new MainReactPackage(), 60 | new Package() // add this for react-native-android-library-boilerplate 61 | ); 62 | } 63 | } 64 | ``` 65 | 4. Simply `import/require` it by the name defined in your library's `package.json`: 66 | 67 | ```javascript 68 | import Boilerplate from 'react-native-android-library-boilerplate' 69 | Boilerplate.show('Boilerplate runs fine', Boilerplate.LONG) 70 | ``` 71 | 5. You can test and develop your library by importing the `node_modules` library into **Android Studio** if you don't want to install it from _git_ all the time. -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | import groovy.json.JsonSlurper 2 | 3 | def computeVersionName() { 4 | // dynamically retrieve version from package.json 5 | def slurper = new JsonSlurper() 6 | def json = slurper.parse(file('../package.json'), "utf-8") 7 | return json.version 8 | } 9 | 10 | buildscript { 11 | repositories { 12 | jcenter() 13 | } 14 | 15 | dependencies { 16 | classpath 'com.android.tools.build:gradle:1.5.0' 17 | } 18 | } 19 | 20 | apply plugin: 'com.android.library' 21 | 22 | android { 23 | compileSdkVersion 23 24 | buildToolsVersion "23.0.1" 25 | 26 | defaultConfig { 27 | minSdkVersion 16 28 | targetSdkVersion 22 29 | versionCode 1 30 | // get version name from package.json version 31 | versionName computeVersionName() 32 | } 33 | lintOptions { 34 | abortOnError false 35 | } 36 | } 37 | 38 | repositories { 39 | mavenCentral() 40 | } 41 | 42 | dependencies { 43 | compile 'com.facebook.react:react-native:+' 44 | } 45 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /android/src/main/java/io/cmichel/boilerplate/Module.java: -------------------------------------------------------------------------------- 1 | package io.cmichel.boilerplate; 2 | 3 | import android.widget.Toast; 4 | 5 | import com.facebook.react.bridge.ReactApplicationContext; 6 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 7 | import com.facebook.react.bridge.ReactMethod; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | public class Module extends ReactContextBaseJavaModule { 13 | 14 | private static final String DURATION_SHORT_KEY = "SHORT"; 15 | private static final String DURATION_LONG_KEY = "LONG"; 16 | 17 | public Module(ReactApplicationContext reactContext) { 18 | super(reactContext); 19 | } 20 | 21 | @Override 22 | public String getName() { 23 | return "Boilerplate"; 24 | } 25 | 26 | @Override 27 | public Map getConstants() { 28 | final Map constants = new HashMap<>(); 29 | constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT); 30 | constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG); 31 | return constants; 32 | } 33 | 34 | @ReactMethod 35 | public void show(String message, int duration) { 36 | Toast.makeText(getReactApplicationContext(), message, duration).show(); 37 | } 38 | } -------------------------------------------------------------------------------- /android/src/main/java/io/cmichel/boilerplate/Package.java: -------------------------------------------------------------------------------- 1 | package io.cmichel.boilerplate; 2 | 3 | import com.facebook.react.ReactPackage; 4 | import com.facebook.react.bridge.JavaScriptModule; 5 | import com.facebook.react.bridge.NativeModule; 6 | import com.facebook.react.bridge.ReactApplicationContext; 7 | import com.facebook.react.uimanager.ViewManager; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.List; 12 | 13 | public class Package implements ReactPackage { 14 | 15 | @Override 16 | public List> createJSModules() { 17 | return Collections.emptyList(); 18 | } 19 | 20 | @Override 21 | public List createViewManagers(ReactApplicationContext reactContext) { 22 | return Collections.emptyList(); 23 | } 24 | 25 | @Override 26 | public List createNativeModules( 27 | ReactApplicationContext reactContext) { 28 | List modules = new ArrayList<>(); 29 | 30 | modules.add(new Module(reactContext)); 31 | 32 | return modules; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /index.android.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | import { NativeModules } from 'react-native' 4 | // name as defined via ReactContextBaseJavaModule's getName 5 | module.exports = NativeModules.Boilerplate 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-android-library-boilerplate", 3 | "version": "0.0.1", 4 | "author": "Christoph Michel http://cmichel.io", 5 | "private": false, 6 | "license": "Unlicense", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/MrToph/react-native-android-library-boilerplate.git" 10 | }, 11 | "bugs": { 12 | "url": "https://github.com/MrToph/react-native-android-library-boilerplate/issues" 13 | }, 14 | "homepage": "https://github.com/MrToph/react-native-android-library-boilerplate", 15 | "keywords": [ 16 | "react-native", 17 | "android", 18 | "library" 19 | ], 20 | "scripts": { 21 | "start": "node node_modules/react-native/local-cli/cli.js start", 22 | "test": "jest" 23 | }, 24 | "peerDependencies": { 25 | "react-native": ">=0.38.0" 26 | } 27 | } 28 | --------------------------------------------------------------------------------