From 7ebcf62ba5e00850b1141402154eab115e5b2ba7 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Wed, 15 Feb 2023 17:54:59 +0100 Subject: [PATCH 1/4] feat: publish `rnskia` prefab to allow RN Skia to be used in third party libraries --- package/android/build.gradle | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/package/android/build.gradle b/package/android/build.gradle index 29c1f7ab57..2e95a0c251 100644 --- a/package/android/build.gradle +++ b/package/android/build.gradle @@ -132,14 +132,10 @@ android { externalNativeBuild { cmake { - path file('CMakeLists.txt') + path file('CMakeLists.txt') } } - buildFeatures { - prefab true - } - packagingOptions { excludes = [ "**/libc++_shared.so", @@ -150,6 +146,17 @@ android { ] } + buildFeatures { + prefab true + prefabPublishing true + } + + prefab { + rnskia { + headers "${project.buildDir}/headers/rnskia/" + } + } + // Create new configurations that can be referred to in dependencies. // The Android Gradle Plugin 3.* does not allow hooking into existing // configurations like `implementation`. @@ -255,6 +262,14 @@ task extractJNIFiles { extractJNIFiles.mustRunAfter extractAARHeaders +task prepareHeaders(type: Copy) { + from fileTree('../cpp').filter { it.isFile() } + into "${project.buildDir}/headers/rnskia/" + includeEmptyDirs = false +} + +preBuild.dependsOn(prepareHeaders) + def nativeBuildDependsOn(dependsOnTask, variant) { def buildTasks = tasks.findAll({ task -> !task.name.contains("Clean") && (task.name.contains("externalNative") || task.name.contains("CMake")) }) @@ -266,5 +281,5 @@ def nativeBuildDependsOn(dependsOnTask, variant) { afterEvaluate { nativeBuildDependsOn(extractAARHeaders, null) - nativeBuildDependsOn(extractJNIFiles, null) -} \ No newline at end of file + nativeBuildDependsOn(extractJNIFiles, null) +} From 20e98b424847af8e68c24536f252d47f3adeb060 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 17 Feb 2023 12:03:24 +0100 Subject: [PATCH 2/4] Rename lib from `reactskia` to `rnskia` (CMake has two names) --- docs/docs/getting-started/bundle-size.md | 2 +- package/android/CMakeLists.txt | 10 +++++----- .../com/shopify/reactnative/skia/RNSkiaModule.java | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/docs/getting-started/bundle-size.md b/docs/docs/getting-started/bundle-size.md index cd83a59ac3..28ffeb2014 100644 --- a/docs/docs/getting-started/bundle-size.md +++ b/docs/docs/getting-started/bundle-size.md @@ -25,7 +25,7 @@ _On *Android* you should use [App Bundles](https://developer.android.com/guide/a When building an APK in release mode, you will see an increase of 41.3 MB after adding React Native Skia. This is because the library is built for different target architectures. -If we take `arm-64-bit` for instance, the `libreactskia.so` library file is only around 3,8 MB. +If we take `arm-64-bit` for instance, the `librnskia.so` library file is only around 3,8 MB. This implies that if you distribute your apps using [App Bundles](https://developer.android.com/guide/app-bundle), the increase in download size should be around 4 MB on Android devices when distributed (including an increase of 220 KB to the Javascript Bundle). diff --git a/package/android/CMakeLists.txt b/package/android/CMakeLists.txt index 1eb4de9cfa..39a86e9876 100644 --- a/package/android/CMakeLists.txt +++ b/package/android/CMakeLists.txt @@ -5,7 +5,7 @@ set (CMAKE_VERBOSE_MAKEFILE ON) set (CMAKE_CXX_STANDARD 17) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSK_GL -DSK_BUILD_FOR_ANDROID -DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID") -set (PACKAGE_NAME "reactskia") +set (PACKAGE_NAME "rnskia") set (SKIA_LIB "skia") set (SKIA_SVG_LIB "svg") set (SKIA_SKSHAPER_LIB "skshaper") @@ -31,7 +31,7 @@ link_directories(../libs/android/${ANDROID_ABI}/) if(${REACT_NATIVE_VERSION} LESS 66) file( - TO_CMAKE_PATH + TO_CMAKE_PATH "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp" INCLUDE_JSI_CPP ) @@ -59,7 +59,7 @@ add_library( "${PROJECT_SOURCE_DIR}/cpp/rnskia/dom/base/ConcatablePaint.cpp" "${PROJECT_SOURCE_DIR}/cpp/api/third_party/CSSColorParser.cpp" - + ) target_include_directories( @@ -92,7 +92,7 @@ target_include_directories( cpp/rnskia/dom/base cpp/rnskia/dom/nodes cpp/rnskia/dom/props - cpp/utils + cpp/utils ${libfbjni_include_DIRS} ) @@ -205,4 +205,4 @@ target_link_libraries( -lGLESv2 -lEGL -landroid - ) \ No newline at end of file + ) diff --git a/package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaModule.java b/package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaModule.java index 0521f0534e..3cb5dae947 100644 --- a/package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaModule.java +++ b/package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaModule.java @@ -57,7 +57,7 @@ public boolean install() { } try { - System.loadLibrary("reactskia"); + System.loadLibrary("rnskia"); ReactApplicationContext context = weakReactContext.get(); if (context == null) { Log.e(NAME, "React Application Context was null!"); From 533f3354729d3c38e6621bc8b6f027ec0cba2f7a Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 17 Feb 2023 16:25:42 +0100 Subject: [PATCH 3/4] fix: Only ship prefab on RN > 68 --- package/android/build.gradle | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/package/android/build.gradle b/package/android/build.gradle index 2e95a0c251..88fcfe0222 100644 --- a/package/android/build.gradle +++ b/package/android/build.gradle @@ -21,6 +21,7 @@ def DEFAULT_COMPILE_SDK_VERSION = 28 def DEFAULT_BUILD_TOOLS_VERSION = '28.0.3' def DEFAULT_MIN_SDK_VERSION = 16 def DEFAULT_TARGET_SDK_VERSION = 28 +def ENABLE_PREFAB = REACT_NATIVE_VERSION > 68 def safeExtGet(prop, fallback) { rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback @@ -87,6 +88,7 @@ logger.warn("react-native-skia: PrebuiltDir: ${prebuiltDir}") logger.warn("react-native-skia: buildType: ${buildType}") logger.warn("react-native-skia: buildDir: ${buildDir}") logger.warn("react-native-skia: node_modules: ${nodeModules}") +logger.warn("react-native-skia: Enable Prefab: ${ENABLE_PREFAB}") buildscript { // The Android Gradle plugin is only required when opening the android folder stand-alone. @@ -146,14 +148,15 @@ android { ] } - buildFeatures { - prefab true - prefabPublishing true - } - - prefab { - rnskia { - headers "${project.buildDir}/headers/rnskia/" + if (ENABLE_PREFAB) { + buildFeatures { + prefab true + prefabPublishing true + } + prefab { + rnskia { + headers "${project.buildDir}/headers/rnskia/" + } } } @@ -262,14 +265,15 @@ task extractJNIFiles { extractJNIFiles.mustRunAfter extractAARHeaders -task prepareHeaders(type: Copy) { - from fileTree('../cpp').filter { it.isFile() } - into "${project.buildDir}/headers/rnskia/" - includeEmptyDirs = false +if (ENABLE_PREFAB) { + task prepareHeaders(type: Copy) { + from fileTree('../cpp').filter { it.isFile() } + into "${project.buildDir}/headers/rnskia/" + includeEmptyDirs = false + } + preBuild.dependsOn(prepareHeaders) } -preBuild.dependsOn(prepareHeaders) - def nativeBuildDependsOn(dependsOnTask, variant) { def buildTasks = tasks.findAll({ task -> !task.name.contains("Clean") && (task.name.contains("externalNative") || task.name.contains("CMake")) }) From 8d0daee52a90a26077cb2b2ad3525ba1a1371ef2 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 17 Feb 2023 16:31:01 +0100 Subject: [PATCH 4/4] fix: Move down in build.gradle --- package/android/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/android/build.gradle b/package/android/build.gradle index 88fcfe0222..237099bc33 100644 --- a/package/android/build.gradle +++ b/package/android/build.gradle @@ -21,7 +21,6 @@ def DEFAULT_COMPILE_SDK_VERSION = 28 def DEFAULT_BUILD_TOOLS_VERSION = '28.0.3' def DEFAULT_MIN_SDK_VERSION = 16 def DEFAULT_TARGET_SDK_VERSION = 28 -def ENABLE_PREFAB = REACT_NATIVE_VERSION > 68 def safeExtGet(prop, fallback) { rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback @@ -81,6 +80,7 @@ def reactProperties = new Properties() file("$nodeModules/react-native/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) } def FULL_RN_VERSION = (System.getenv("REACT_NATIVE_OVERRIDE_VERSION") ?: reactProperties.getProperty("VERSION_NAME")) def REACT_NATIVE_VERSION = FULL_RN_VERSION.split("\\.")[1].toInteger() +def ENABLE_PREFAB = REACT_NATIVE_VERSION > 68 logger.warn("react-native-skia: RN Version: ${REACT_NATIVE_VERSION} / ${FULL_RN_VERSION}") logger.warn("react-native-skia: isSourceBuild: ${sourceBuild}")