Skip to content

Commit

Permalink
Support Fabric on React Native 0.72 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
oblador authored Oct 25, 2023
1 parent bc4fec5 commit 14fa21e
Show file tree
Hide file tree
Showing 71 changed files with 5,026 additions and 6,827 deletions.
127 changes: 22 additions & 105 deletions android/build.gradle
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,139 +1,56 @@
import java.nio.file.Paths

buildscript {
if (project == rootProject) {
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:7.2.1")
}
ext.safeExtGet = {prop, fallback ->
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
}

static def findNodeModules(baseDir) {
def basePath = baseDir.toPath().normalize()
// Node's module resolution algorithm searches up to the root directory,
// after which the base path will be null
while (basePath) {
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
return nodeModulesPath.toString()
}
basePath = basePath.getParent()
repositories {
google()
gradlePluginPortal()
}
dependencies {
classpath("com.android.tools.build:gradle:7.2.0")
}
throw new GradleException("Unable to locate node_modules")
}

def isNewArchitectureEnabled() {
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

apply plugin: 'com.android.library'

if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def nodeModules = findNodeModules(projectDir)

android {
namespace "com.klarna.vectordrawable"
compileSdkVersion safeExtGet('compileSdkVersion', 30)
buildToolsVersion safeExtGet('buildToolsVersion', "30.0.2")

// Used to override the NDK path/version on internal CI or by allowing
// users to customize the NDK path/version from their root project (e.g. for M1 support)
if (rootProject.hasProperty("ndkPath")) {
ndkPath rootProject.ext.ndkPath
}
if (rootProject.hasProperty("ndkVersion")) {
ndkVersion rootProject.ext.ndkVersion
}
compileSdkVersion safeExtGet('compileSdkVersion', 31)

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 18)
targetSdkVersion safeExtGet('targetSdkVersion', 30)
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

if (isNewArchitectureEnabled()) {
var appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
externalNativeBuild {
ndkBuild {
arguments "APP_PLATFORM=android-21",
"APP_STL=c++_shared",
"NDK_TOOLCHAIN_VERSION=clang",
"GENERATED_SRC_DIR=${appProject.buildDir}/generated/source",
"PROJECT_BUILD_DIR=${appProject.buildDir}",
"REACT_ANDROID_DIR=${nodeModules}/react-native/ReactAndroid",
"REACT_ANDROID_BUILD_DIR=${nodeModules}/react-native/ReactAndroid/build"
cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
cppFlags "-std=c++17"
targets "vectordrawable_modules"
}
}
}
minSdkVersion safeExtGet('minSdkVersion', 21)
targetSdkVersion safeExtGet('targetSdkVersion', 31)
buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
}

if (isNewArchitectureEnabled()) {
externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
}
packagingOptions {
// For some reason gradle only complains about the duplicated version of libreact_render libraries
// while there are more libraries copied in intermediates folder of the lib build directory, we exlude
// only the ones that make the build fail (ideally we should only include librnscreens_modules but we
// are only allowed to specify exlude patterns)
// See: https://github.com/software-mansion/react-native-screens/blob/main/android/build.gradle#L90
exclude "**/libreact_render*.so"
}
sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += ['src/fabric/java']
java.srcDirs += ['src/newarch']
} else {
java.srcDirs += ['src/paper/java', 'build/generated/source/codegen/java']
java.srcDirs += ['src/oldarch']
}
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

repositories {
if (project == rootProject) {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
mavenCentral()
google()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$projectDir/../node_modules/react-native/android"
}
mavenCentral()
google()
}

dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation 'androidx.appcompat:appcompat:1.2.0'
}

if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = "vectordrawable"
codegenJavaPackageName = "com.klarna"
reactNativeDir = rootProject.file("../node_modules/react-native/")
codegenDir = rootProject.file("../node_modules/react-native-codegen/")
}
implementation 'com.facebook.react:react-native:+'
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.annotation.Nullable;

public class VectorDrawableManagerImpl {

public static final String NAME = "RNVectorDrawable";

public static ImageView createViewInstance(ThemedReactContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
package com.klarna.vectordrawable;

import androidx.annotation.NonNull;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.soloader.SoLoader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@SuppressWarnings("unused")
public class VectorDrawablePackage implements ReactPackage {

@Override
@NonNull
public List<NativeModule> createNativeModules(@NonNull final ReactApplicationContext reactContext) {
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// TODO: Remove when auto-linking will support Fabric modules
// See: https://github.com/software-mansion/react-native-screens/blob/main/android/src/main/java/com/swmansion/rnscreens/RNScreensPackage.kt#L16
SoLoader.loadLibrary("vectordrawable_modules");
}
return Collections.emptyList();
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
List<ViewManager> viewManagers = new ArrayList<>();
viewManagers.add(new VectorDrawableManager(reactContext));
return viewManagers;
}

@Override
@NonNull
public List<ViewManager> createViewManagers(@NonNull final ReactApplicationContext reactContext) {
return Collections.singletonList(new VectorDrawableManager(reactContext));
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Collections.emptyList();
}

}
37 changes: 0 additions & 37 deletions android/src/main/jni/Android.mk

This file was deleted.

9 changes: 0 additions & 9 deletions android/src/main/jni/OnInit.cpp

This file was deleted.

54 changes: 0 additions & 54 deletions android/src/main/jni/VectorDrawableComponentsRegistry.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions android/src/main/jni/VectorDrawableComponentsRegistry.h

This file was deleted.

6 changes: 0 additions & 6 deletions example/.buckconfig

This file was deleted.

2 changes: 2 additions & 0 deletions example/.bundle/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BUNDLE_PATH: "vendor/bundle"
BUNDLE_FORCE_RUBY_PLATFORM: 1
3 changes: 0 additions & 3 deletions example/.editorconfig

This file was deleted.

Loading

0 comments on commit 14fa21e

Please sign in to comment.