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.
Customize the project name by doing the following:
author
and name
in package.json
com.domain.package
) as follows:
android/src/main/AndroidManifest.xml
.android/src/main/java
to match your package name.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.Edit the name of your module in
@Override
public String getName() {
return "Boilerplate";
}
and adjust it in index.android.js
Android Studio
and select File -> New -> Import Project
and select the android folder of this package.Plugin with id 'android-library' not found
Error, install android support repository
.There are many ways to do this, here's the way I do it:
npm install --save git+https://github.com/MrToph/react-native-android-library-boilerplate.git
in your main project.Link the library:
Add the following to android/settings.gradle
:
include ':react-native-android-library-boilerplate'
project(':react-native-android-library-boilerplate').projectDir = new File(settingsDir, '../node_modules/react-native-android-library-boilerplate/android')
Add the following to android/app/build.gradle
:
...
dependencies {
...
compile project(':react-native-android-library-boilerplate')
}
Add the following to android/app/src/main/java/**/MainApplication.java
:
package com.motivation;
import io.cmichel.boilerplate.Package; // add this for react-native-android-library-boilerplate
public class MainApplication extends Application implements ReactApplication {
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new Package() // add this for react-native-android-library-boilerplate
);
}
}
Simply import/require
it by the name defined in your library's package.json
:
import Boilerplate from 'react-native-android-library-boilerplate'
Boilerplate.show('Boilerplate runs fine', Boilerplate.LONG)
node_modules
library into Android Studio if you don't want to install it from git all the time.