Do not store .cpp/.h files inside src/main/java - mapbuffer

Summary:
Currently we expose native code (.h, .cpp) inside the src/main/java folder.

This is making impossible for users on New Architecture to open the project
inside Android Studio.

The problem is that the src/main/java is reserved to Java/Kotlin sources only.
AGP 7.2 also removed support for mixed source roots:
https://developer.android.com/studio/releases/gradle-plugin#duplicate-content-roots

This is essentially forcing users to write Java code without any autocompletion
as all the React Native Java classes are considered C++ files.

I'm addressing this issue folder by folder by moving them
from ReactAndroid/src/main/java/com/facebook/... to ReactAndroid/src/main/jni/react/...

This is the diff for mapbuffer

Changelog:
[Internal] [Changed] - Do not store .cpp/.h files inside src/main/java - mapbuffer

Reviewed By: cipolleschi

Differential Revision: D38699253

fbshipit-source-id: c1c8f8693b6da4e3428f8f280e1ca4d5c5d0f853
This commit is contained in:
Nicola Corti
2022-08-16 03:20:16 -07:00
committed by Facebook GitHub Bot
parent be8fe7a9c1
commit 3d2185203b
12 changed files with 4 additions and 4 deletions
@@ -22,7 +22,7 @@ rn_android_library(
deps = [
FBJNI_TARGET,
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
react_native_target("java/com/facebook/react/common/mapbuffer/jni:jni"),
react_native_target("jni/react/mapbuffer:jni"),
react_native_dep("libraries/fbjni:java"),
react_native_dep("third-party/android/androidx:annotation"),
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
@@ -17,7 +17,7 @@ import javax.annotation.concurrent.NotThreadSafe
/**
* Read-only implementation of the [MapBuffer], imported from C++ environment. Use
* `<react/common/mapbuffer/jni/JReadableMapBuffer.h> to create it.
* `<react/common/mapbuffer/JReadableMapBuffer.h> to create it.
*
* See [MapBuffer] documentation for more details
*/
@@ -1,31 +0,0 @@
load("//tools/build_defs/oss:rn_defs.bzl", "ANDROID", "FBJNI_TARGET", "react_native_xplat_target", "rn_xplat_cxx_library", "subdir_glob")
rn_xplat_cxx_library(
name = "jni",
srcs = glob(["**/*.cpp"]),
headers = glob(["**/*.h"]),
header_namespace = "",
exported_headers = subdir_glob(
[
("react/common/mapbuffer", "*.h"),
],
prefix = "react/common/mapbuffer",
),
fbandroid_allow_jni_merging = True,
labels = [
"pfh:ReactNative_CommonInfrastructurePlaceholder",
"supermodule:xplat/default/public.react_native.infra",
],
platforms = ANDROID,
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
soname = "libmapbufferjni.$(ext)",
visibility = ["PUBLIC"],
deps = [
react_native_xplat_target("react/renderer/mapbuffer:mapbuffer"),
react_native_xplat_target("react/debug:debug"),
FBJNI_TARGET,
],
)
@@ -1,34 +0,0 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)
add_compile_options(-fexceptions -frtti -std=c++17 -Wall -DLOG_TAG=\"Fabric\")
file(GLOB mapbuffer_SRC CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/react/common/mapbuffer/*.cpp)
add_library(mapbufferjni SHARED ${mapbuffer_SRC})
target_include_directories(mapbufferjni
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/react/common/mapbuffer/
)
target_link_libraries(mapbufferjni
fb
fbjni
folly_runtime
glog
glog_init
react_debug
react_render_mapbuffer
react_utils
react_config
yoga
)
@@ -1,3 +0,0 @@
---
InheritParentConfig: true
...
@@ -1,43 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "JReadableMapBuffer.h"
namespace facebook {
namespace react {
void JReadableMapBuffer::registerNatives() {
registerHybrid({
makeNativeMethod(
"importByteBuffer", JReadableMapBuffer::importByteBuffer),
});
}
jni::local_ref<jni::JByteBuffer> JReadableMapBuffer::importByteBuffer() {
// TODO T83483191: Reevaluate what's the best approach here (allocateDirect vs
// DirectByteBuffer).
return jni::JByteBuffer::wrapBytes(
serializedData_.data(), serializedData_.size());
}
std::vector<uint8_t> JReadableMapBuffer::data() const {
return serializedData_;
}
jni::local_ref<JReadableMapBuffer::jhybridobject>
JReadableMapBuffer::createWithContents(MapBuffer &&map) {
return newObjectCxxArgs(std::move(map));
}
JReadableMapBuffer::JReadableMapBuffer(MapBuffer &&map)
: serializedData_(std::move(map.bytes_)) {
react_native_assert(
(serializedData_.size() != 0) && "Error no content in map");
}
} // namespace react
} // namespace facebook
@@ -1,40 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <fbjni/fbjni.h>
#include <react/debug/react_native_assert.h>
#include <react/renderer/mapbuffer/MapBuffer.h>
#include <fbjni/ByteBuffer.h>
namespace facebook {
namespace react {
class JReadableMapBuffer : public jni::HybridClass<JReadableMapBuffer> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/common/mapbuffer/ReadableMapBuffer;";
static void registerNatives();
static jni::local_ref<JReadableMapBuffer::jhybridobject> createWithContents(
MapBuffer &&map);
explicit JReadableMapBuffer(MapBuffer &&map);
jni::local_ref<jni::JByteBuffer> importByteBuffer();
std::vector<uint8_t> data() const;
private:
std::vector<uint8_t> serializedData_;
};
} // namespace react
} // namespace facebook
@@ -1,65 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "JWritableMapBuffer.h"
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
namespace facebook::react {
MapBuffer JWritableMapBuffer::getMapBuffer() {
static const auto getKeys =
javaClassStatic()->getMethod<jni::JArrayInt()>("getKeys");
static const auto getValues =
javaClassStatic()->getMethod<jni::JArrayClass<jni::JObject>()>(
"getValues");
auto keyArray = getKeys(self());
auto values = getValues(self());
auto keys = keyArray->pin();
MapBufferBuilder builder;
auto size = keys.size();
for (int i = 0; i < size; i++) {
auto key = keys[i];
jni::local_ref<jni::JObject> value = values->getElement(i);
static const auto booleanClass = jni::JBoolean::javaClassStatic();
static const auto integerClass = jni::JInteger::javaClassStatic();
static const auto doubleClass = jni::JDouble::javaClassStatic();
static const auto stringClass = jni::JString::javaClassStatic();
static const auto readableMapClass = JReadableMapBuffer::javaClassStatic();
static const auto writableMapClass = JWritableMapBuffer::javaClassStatic();
if (value->isInstanceOf(booleanClass)) {
auto element = jni::static_ref_cast<jni::JBoolean>(value);
builder.putBool(key, element->value());
} else if (value->isInstanceOf(integerClass)) {
auto element = jni::static_ref_cast<jni::JInteger>(value);
builder.putInt(key, element->value());
} else if (value->isInstanceOf(doubleClass)) {
auto element = jni::static_ref_cast<jni::JDouble>(value);
builder.putDouble(key, element->value());
} else if (value->isInstanceOf(stringClass)) {
auto element = jni::static_ref_cast<jni::JString>(value);
builder.putString(key, element->toStdString());
} else if (value->isInstanceOf(readableMapClass)) {
auto element =
jni::static_ref_cast<JReadableMapBuffer::jhybridobject>(value);
builder.putMapBuffer(key, MapBuffer(element->cthis()->data()));
} else if (value->isInstanceOf(writableMapClass)) {
auto element =
jni::static_ref_cast<JWritableMapBuffer::javaobject>(value);
builder.putMapBuffer(key, element->getMapBuffer());
}
}
return builder.build();
}
} // namespace facebook::react
@@ -1,23 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <fbjni/fbjni.h>
#include <react/common/mapbuffer/JReadableMapBuffer.h>
namespace facebook::react {
class JWritableMapBuffer : public jni::JavaClass<JWritableMapBuffer> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/common/mapbuffer/WritableMapBuffer;";
MapBuffer getMapBuffer();
};
} // namespace facebook::react
@@ -1,16 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <fbjni/fbjni.h>
#include "JReadableMapBuffer.h"
#include "JWritableMapBuffer.h"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
return facebook::jni::initialize(
vm, [] { facebook::react::JReadableMapBuffer::registerNatives(); });
}