Skip to content

Commit

Permalink
android端集成hostobject
Browse files Browse the repository at this point in the history
  • Loading branch information
drafish committed Jan 30, 2020
1 parent 54d83aa commit f70e5b0
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 26 deletions.
10 changes: 7 additions & 3 deletions App.js
Expand Up @@ -34,11 +34,15 @@ import TestExample from "./TestExample";
// console.warn(res);
// });

TestExample.foo(1, 2, (res) => {
console.warn(res)
})
// TestExample.foo(1, 2, (res) => {
// console.warn(res)
// })


const App: () => React$Node = () => {

console.warn(TestExample.runTest(2, 2));

return (
<>
<StatusBar barStyle="dark-content" />
Expand Down
9 changes: 5 additions & 4 deletions TestExample.js
Expand Up @@ -7,7 +7,8 @@
* 2. int duration: The duration of the toast. May be ToastExample.SHORT or
* ToastExample.LONG
*/
import { NativeModules } from "react-native";
// 下一句中的ToastExample即对应上文
// public String getName()中返回的字符串
export default NativeModules.TestExample;
// import { NativeModules } from "react-native";
// // 下一句中的ToastExample即对应上文
// // public String getName()中返回的字符串
// export default NativeModules.TestExample;
export default global.nativeTest;
27 changes: 14 additions & 13 deletions android/app/build.gradle
Expand Up @@ -133,11 +133,11 @@ android {
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
externalNativeBuild {
ndkBuild {
arguments "NDK_DEBUG=0"
}
}
// externalNativeBuild {
// ndkBuild {
// arguments "NDK_DEBUG=0"
// }
// }
}
splits {
abi {
Expand Down Expand Up @@ -168,13 +168,14 @@ android {
}
}
externalNativeBuild {
// cmake {
// version '3.10.2'
// path "../../cpp/jni/CMakeLists.txt"
// }
ndkBuild {
path file('../../cpp/cxxmodule/Android.mk')
cmake {
version '3.10.2'
// path "../../cpp/jni/CMakeLists.txt"
path "../../cpp/hostobject/CMakeLists.txt"
}
// ndkBuild {
// path file('../../cpp/cxxmodule/Android.mk')
// }
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
Expand All @@ -194,8 +195,8 @@ android {

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
// implementation "com.facebook.react:react-native:+" // From node_modules
implementation project(':ReactAndroid') // Build from RN source
implementation "com.facebook.react:react-native:+" // From node_modules
// implementation project(':ReactAndroid') // Build from RN source

if (enableHermes) {
def hermesPath = "../../node_modules/hermes-engine/android/";
Expand Down
@@ -1,8 +1,13 @@
package com.reactnativecppdemo;

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("cpp_module_jni"); // this loads the library when the class is loaded
}

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

@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());
}

public native void install(long jsContextNativePointer);
}
Expand Up @@ -11,7 +11,7 @@
import java.util.List;

// import com.example.jni.TestPackage; // <-- 引入你自己的包
import com.example.cxxmodule.HelloCxxPackage;
// import com.example.cxxmodule.HelloCxxPackage;

public class MainApplication extends Application implements ReactApplication {

Expand All @@ -29,7 +29,7 @@ protected List<ReactPackage> getPackages() {
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
// packages.add(new TestPackage()); // <-- 添加这一行,类名替换成你的Package类的名字 name.
packages.add(new HelloCxxPackage());
// packages.add(new HelloCxxPackage());
return packages;
}

Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Expand Up @@ -13,7 +13,7 @@ buildscript {
}
dependencies {
classpath("com.android.tools.build:gradle:3.4.2")
classpath("de.undercouch:gradle-download-task:3.4.3")
// classpath("de.undercouch:gradle-download-task:3.4.3")

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions android/settings.gradle
Expand Up @@ -2,5 +2,5 @@ rootProject.name = 'ReactNativeCppDemo'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':app'

include ':ReactAndroid'
project(':ReactAndroid').projectDir = new File(rootProject.projectDir, '../node_modules/react-native/ReactAndroid')
// include ':ReactAndroid'
// project(':ReactAndroid').projectDir = new File(rootProject.projectDir, '../node_modules/react-native/ReactAndroid')
45 changes: 45 additions & 0 deletions cpp/hostobject/CMakeLists.txt
@@ -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

include_directories(
../../node_modules/react-native/React
../../node_modules/react-native/React/Base
../../node_modules/react-native/ReactCommon/jsi
../../ios/Pods/Folly
../../ios/Pods/DoubleConversion
../../ios/Pods/boost-for-react-native
../../ios/Pods/glog/src
)

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

add_library( # Sets the name of the library.
cpp_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/jsi.cpp
../src/Test.cpp
TestBinding.cpp
jni.cpp)

target_link_libraries(cpp_module_jni
android
log)
16 changes: 16 additions & 0 deletions cpp/hostobject/jni.cpp
@@ -0,0 +1,16 @@
#include <jni.h>
#include <jsi/jsi.h>
#include "TestBinding.h"

extern "C"
{
JNIEXPORT void JNICALL
Java_com_reactnativecppdemo_MainActivity_install(JNIEnv* env, jobject thiz, jlong runtimePtr)
{
auto test = std::make_unique<example::Test>();
auto testBinding = std::make_shared<example::TestBinding>(std::move(test));
jsi::Runtime* runtime = (jsi::Runtime*)runtimePtr;

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

0 comments on commit f70e5b0

Please sign in to comment.