From eea865862745167de8a4d9109c827c5c11f76996 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 10 Dec 2015 18:34:09 -0800 Subject: [PATCH] put our react module in our aar --- react-native/android/app/build.gradle | 2 + .../app/src/main/java/com/reacttests/API.java | 5 -- .../com/reacttests/RealmReactAndroid.java | 23 +++++++++ src/android/com_reacttests_API.cpp | 2 +- src/android/com_reacttests_API.h | 2 +- .../src/main/java/com/demo/MainActivity.java | 5 +- .../java/com/demo/module/JniToastModule.java | 50 ------------------- tests/react-test-app/index.android.js | 4 +- 8 files changed, 32 insertions(+), 61 deletions(-) delete mode 100644 react-native/android/app/src/main/java/com/reacttests/API.java create mode 100644 react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java delete mode 100644 tests/react-test-app/android/app/src/main/java/com/demo/module/JniToastModule.java diff --git a/react-native/android/app/build.gradle b/react-native/android/app/build.gradle index d1032555..3eef00b9 100644 --- a/react-native/android/app/build.gradle +++ b/react-native/android/app/build.gradle @@ -182,6 +182,8 @@ dependencies { compile 'com.squareup.okhttp:okhttp-ws:2.5.0' compile 'com.squareup.okio:okio:1.6.0' compile 'org.webkit:android-jsc:r174650' + compile "com.facebook.react:react-native:0.16.+" + testCompile "junit:junit:${JUNIT_VERSION}" testCompile "org.powermock:powermock-api-mockito:${POWERMOCK_VERSION}" diff --git a/react-native/android/app/src/main/java/com/reacttests/API.java b/react-native/android/app/src/main/java/com/reacttests/API.java deleted file mode 100644 index 9da2ef19..00000000 --- a/react-native/android/app/src/main/java/com/reacttests/API.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.reacttests; - -public class API { - public static native String getDefaultRealmFileDirectory(); -} \ No newline at end of file diff --git a/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java b/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java new file mode 100644 index 00000000..ccc1c02a --- /dev/null +++ b/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java @@ -0,0 +1,23 @@ +package com.reacttests; + +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; + +import java.util.Map; + +public class RealmReactAndroid extends ReactContextBaseJavaModule { + + public RealmReactAndroid(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + public String getName() { + return "RealmReactAndroid"; + } + + public static native String getDefaultRealmFileDirectory(); +} diff --git a/src/android/com_reacttests_API.cpp b/src/android/com_reacttests_API.cpp index 28a48e73..af20f904 100644 --- a/src/android/com_reacttests_API.cpp +++ b/src/android/com_reacttests_API.cpp @@ -9,7 +9,7 @@ * Method: getDefaultRealmFileDirectory * Signature: ()Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_com_reacttests_API_getDefaultRealmFileDirectory +JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_getDefaultRealmFileDirectory (JNIEnv *env, jclass) { diff --git a/src/android/com_reacttests_API.h b/src/android/com_reacttests_API.h index d868882e..9567b7fe 100644 --- a/src/android/com_reacttests_API.h +++ b/src/android/com_reacttests_API.h @@ -12,7 +12,7 @@ extern "C" { * Method: getDefaultRealmFileDirectory * Signature: ()Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_com_reacttests_API_getDefaultRealmFileDirectory +JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_getDefaultRealmFileDirectory (JNIEnv *, jclass); #ifdef __cplusplus diff --git a/tests/react-test-app/android/app/src/main/java/com/demo/MainActivity.java b/tests/react-test-app/android/app/src/main/java/com/demo/MainActivity.java index 350f89c5..0c50fb44 100644 --- a/tests/react-test-app/android/app/src/main/java/com/demo/MainActivity.java +++ b/tests/react-test-app/android/app/src/main/java/com/demo/MainActivity.java @@ -4,7 +4,6 @@ import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; -import com.demo.module.JniToastModule; import com.facebook.react.LifecycleState; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactPackage; @@ -20,6 +19,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import com.reacttests.RealmReactAndroid; + public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { @@ -93,7 +94,7 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand ReactApplicationContext reactContext) { List modules = new ArrayList<>(); - modules.add(new JniToastModule(reactContext)); + modules.add(new RealmReactAndroid(reactContext)); return modules; } diff --git a/tests/react-test-app/android/app/src/main/java/com/demo/module/JniToastModule.java b/tests/react-test-app/android/app/src/main/java/com/demo/module/JniToastModule.java deleted file mode 100644 index 6bce5b4f..00000000 --- a/tests/react-test-app/android/app/src/main/java/com/demo/module/JniToastModule.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.demo.module; - -import android.widget.Toast; - -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; -import com.facebook.react.bridge.ReactMethod; -import com.reacttests.API; - -import java.util.HashMap; -import java.util.Map; - -/** - * Created by Nabil on 30/11/15. - */ -public class JniToastModule extends ReactContextBaseJavaModule { - private static final String DURATION_SHORT_KEY = "SHORT"; - private static final String DURATION_LONG_KEY = "LONG"; - - public JniToastModule(ReactApplicationContext reactContext) { - super(reactContext); - } - - @Override - public String getName() { - return "JniToastAndroid"; - } - - @Override - public Map getConstants() { - final Map constants = new HashMap<>(); - constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT); - constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG); - return constants; - } - - @ReactMethod - public void show(String message, int duration) { - StringBuilder sb = new StringBuilder("String from Java: ") - .append(message) - .append("\n") - .append("String from JNI: ") - .append(API.getDefaultRealmFileDirectory()); - Toast.makeText(getReactApplicationContext(), sb.toString(), duration).show(); - } - - static { - System.loadLibrary("realmreact"); - } -} diff --git a/tests/react-test-app/index.android.js b/tests/react-test-app/index.android.js index f80186fb..3ada3432 100644 --- a/tests/react-test-app/index.android.js +++ b/tests/react-test-app/index.android.js @@ -13,11 +13,11 @@ var { TouchableNativeFeedback, } = React; -var JniToastAndroid = require('NativeModules').JniToastAndroid; +var RealmReactAndroid = require('NativeModules').RealmReactAndroid; var Demo = React.createClass({ buttonClicked: function() { - JniToastAndroid.show("Hello Zepp", JniToastAndroid.LONG); + RealmReactAndroid.show("Hello Zepp"); }, render: function() { return (