Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Add Android support #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ buck-out/

# Bundle artifact
*.jsbundle
android/app/.externalNativeBuild
45 changes: 45 additions & 0 deletions android/app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

# Needed to locate double-conversion src correctly for folly includes
execute_process (COMMAND ln "-s" "src" "../../node_modules/react-native/third-party/double-conversion-1.1.6/double-conversion")

include_directories(
../../node_modules/react-native/React
../../node_modules/react-native/React/Base
../../node_modules/react-native/ReactCommon
../../node_modules/react-native/third-party/folly-2018.10.22.00
../../node_modules/react-native/third-party/double-conversion-1.1.6
../../node_modules/react-native/third-party/boost_1_63_0
../../node_modules/react-native/third-party/glog-0.3.5/src
)

add_definitions(
-DFOLLY_USE_LIBCPP=1
-DFOLLY_NO_CONFIG=1
-DFOLLY_HAVE_MEMRCHR=1
)

add_library( # Sets the name of the library.
test_module_jni

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
../../node_modules/react-native/ReactCommon/jsi/jsi.cpp
../../c++/Test.cpp
../../c++/TestBinding.cpp)

target_link_libraries(test_module_jni
android
log)
5 changes: 5 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ android {
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
Expand Down
26 changes: 25 additions & 1 deletion android/app/src/main/java/com/testmodule/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.testmodule;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;

public class MainActivity extends ReactActivity {
public class MainActivity extends ReactActivity implements ReactInstanceManager.ReactInstanceEventListener {
static {
System.loadLibrary("test_module_jni"); // this loads the library when the class is loaded
}

/**
* Returns the name of the main component registered from JavaScript.
Expand All @@ -12,4 +17,23 @@ public class MainActivity extends ReactActivity {
protected String getMainComponentName() {
return "TestModule";
}

@Override
public void onResume() {
super.onResume();
getReactInstanceManager().addReactInstanceEventListener(this);
}

@Override
public void onPause() {
super.onPause();
getReactInstanceManager().removeReactInstanceEventListener(this);
}

@Override
public void onReactContextInitialized(ReactContext context) {
install(context.getJavaScriptContextHolder().get());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internally also, this is where we install the JSI Modules.

If you are just exposing native methods, why not use React Native's TurboModuleManager - whose intenet is pretty much that ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I wasn’t sure if TurboModules were ready to use or not! If they are it would be awesome to see an example of how to use them :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@axemclion could you please provide an example on how to use turbo modules in that case?

}

public native void install(long jsContextNativePointer);
}
File renamed without changes.
File renamed without changes.
17 changes: 15 additions & 2 deletions ios/TestBinding.cpp → c++/TestBinding.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
// Copyright 2004-present Facebook. All Rights Reserved.

#include "TestBinding.h"

#include "Test.h"
#include <jsi/JSIDynamic.h>

#if ANDROID
extern "C"
{
JNIEXPORT void JNICALL
Java_com_testmodule_MainActivity_install(JNIEnv* env, jobject thiz, jlong runtimePtr)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In React native's case, you can use fbjni, which helps out with types i nJNI. Check out TurboModuleManager.cpp.

This code is similar to what we do there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is where I copied it from, I think – or are you suggesting using TurboModuleManager directly?

{
auto test = std::make_unique<facebook::react::Test>();
auto testBinding = std::make_shared<facebook::react::TestBinding>(std::move(test));
jsi::Runtime* runtime = (jsi::Runtime*)runtimePtr;

example::TestBinding::install(*runtime, testBinding);
}
}
#endif

namespace example {

static jsi::Object getModule(
Expand Down
11 changes: 11 additions & 0 deletions ios/TestBinding.h → c++/TestBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@
#include "Test.h"
#include <jsi/jsi.h>

#if ANDROID
#include <jni.h>
#endif

using namespace facebook;

#if ANDROID
extern "C" {
JNIEXPORT void JNICALL
Java_com_testmodule_MainActivity_install(JNIEnv* env, jobject thiz, jlong runtimePtr);
}
#endif

namespace example {

/*
Expand Down
35 changes: 23 additions & 12 deletions ios/TestModule.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
2D16E6881FA4F8E400B85C8A /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2D16E6891FA4F8E400B85C8A /* libReact.a */; };
2DCD954D1E0B4F2C00145EB5 /* TestModuleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* TestModuleTests.m */; };
2DF0FFEE2056DD460020B375 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3DAD3EA31DF850E9000B6D8A /* libReact.a */; };
68E307DC2262762A00B130DF /* TestBinding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 68E307DA2262762A00B130DF /* TestBinding.cpp */; };
68E3080C2262781F00B130DF /* Test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 68E3080A2262781F00B130DF /* Test.cpp */; };
736B0C4F228AB89E000DC273 /* TestBinding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 736B0C4B228AB89D000DC273 /* TestBinding.cpp */; };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also curious why the xcode project changed here ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file moved: ios/TestBinding.cpp → c++/TestBinding.cpp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, sorry I missed your message @axemclion!

736B0C50228AB89E000DC273 /* Test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 736B0C4D228AB89D000DC273 /* Test.cpp */; };
832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 832341B51AAA6A8300B99B32 /* libRCTText.a */; };
ADBDB9381DFEBF1600ED6528 /* libRCTBlob.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ADBDB9271DFEBF0700ED6528 /* libRCTBlob.a */; };
ED297163215061F000B7C4FE /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ED297162215061F000B7C4FE /* JavaScriptCore.framework */; };
Expand Down Expand Up @@ -344,10 +344,10 @@
2D02E4901E0B4A5D006451C7 /* TestModule-tvOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "TestModule-tvOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
2D16E6891FA4F8E400B85C8A /* libReact.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libReact.a; sourceTree = BUILT_PRODUCTS_DIR; };
5E91572D1DD0AC6500FF2AA8 /* RCTAnimation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTAnimation.xcodeproj; path = "../node_modules/react-native/Libraries/NativeAnimation/RCTAnimation.xcodeproj"; sourceTree = "<group>"; };
68E307DA2262762A00B130DF /* TestBinding.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = TestBinding.cpp; sourceTree = "<group>"; };
68E307DB2262762A00B130DF /* TestBinding.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestBinding.h; sourceTree = "<group>"; };
68E3080A2262781F00B130DF /* Test.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Test.cpp; sourceTree = "<group>"; };
68E3080B2262781F00B130DF /* Test.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Test.h; sourceTree = "<group>"; };
736B0C4B228AB89D000DC273 /* TestBinding.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestBinding.cpp; sourceTree = "<group>"; };
736B0C4C228AB89D000DC273 /* TestBinding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestBinding.h; sourceTree = "<group>"; };
736B0C4D228AB89D000DC273 /* Test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Test.cpp; sourceTree = "<group>"; };
736B0C4E228AB89D000DC273 /* Test.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Test.h; sourceTree = "<group>"; };
78C398B01ACF4ADC00677621 /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = "../node_modules/react-native/Libraries/LinkingIOS/RCTLinking.xcodeproj"; sourceTree = "<group>"; };
832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = "../node_modules/react-native/Libraries/Text/RCTText.xcodeproj"; sourceTree = "<group>"; };
ADBDB91F1DFEBF0600ED6528 /* RCTBlob.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTBlob.xcodeproj; path = "../node_modules/react-native/Libraries/Blob/RCTBlob.xcodeproj"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -493,17 +493,14 @@
13B07FAE1A68108700A75B9A /* TestModule */ = {
isa = PBXGroup;
children = (
736B0C4A228AB89D000DC273 /* c++ */,
008F07F21AC5B25A0029DE68 /* main.jsbundle */,
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
13B07FB01A68108700A75B9A /* AppDelegate.m */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
13B07FB61A68108700A75B9A /* Info.plist */,
13B07FB11A68108700A75B9A /* LaunchScreen.xib */,
13B07FB71A68108700A75B9A /* main.m */,
68E307DA2262762A00B130DF /* TestBinding.cpp */,
68E307DB2262762A00B130DF /* TestBinding.h */,
68E3080A2262781F00B130DF /* Test.cpp */,
68E3080B2262781F00B130DF /* Test.h */,
);
name = TestModule;
sourceTree = "<group>";
Expand Down Expand Up @@ -550,6 +547,18 @@
name = Products;
sourceTree = "<group>";
};
736B0C4A228AB89D000DC273 /* c++ */ = {
isa = PBXGroup;
children = (
736B0C4B228AB89D000DC273 /* TestBinding.cpp */,
736B0C4C228AB89D000DC273 /* TestBinding.h */,
736B0C4D228AB89D000DC273 /* Test.cpp */,
736B0C4E228AB89D000DC273 /* Test.h */,
);
name = "c++";
path = "../c++";
sourceTree = "<group>";
};
78C398B11ACF4ADC00677621 /* Products */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -709,6 +718,8 @@
CreatedOnToolsVersion = 6.2;
TestTargetID = 13B07F861A680F5B00A75B9A;
};
13B07F861A680F5B00A75B9A = {
};
2D02E47A1E0B4A5D006451C7 = {
CreatedOnToolsVersion = 8.2.1;
ProvisioningStyle = Automatic;
Expand Down Expand Up @@ -1132,8 +1143,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
68E307DC2262762A00B130DF /* TestBinding.cpp in Sources */,
68E3080C2262781F00B130DF /* Test.cpp in Sources */,
736B0C4F228AB89E000DC273 /* TestBinding.cpp in Sources */,
736B0C50228AB89E000DC273 /* Test.cpp in Sources */,
13B07FBC1A68108700A75B9A /* AppDelegate.m in Sources */,
13B07FC11A68108700A75B9A /* main.m in Sources */,
);
Expand Down