Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .changeset/sharp-berries-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rnef/plugin-brownfield-android': minor
'rnef-docs': patch
---

correctly load and present RNViews for the respective RN arch
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@ buildscript {
}
repositories {
google()
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/callstack/big-fat-aar")
credentials {
username = System.getenv("GITHUB_USERNAME")
password = System.getenv("GITHUB_TOKEN")
}
}
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle")
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
classpath("com.callstack.react:brownfield-gradle-plugin:0.2.0")
classpath("com.callstack.react:brownfield-gradle-plugin:0.3.0")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import groovy.json.JsonOutput
import groovy.json.JsonSlurper

plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("com.callstack.react.brownfield")
`maven-publish`
id("com.facebook.react")
}

react {
autolinkLibrariesWithApp()
}

repositories {
mavenCentral()
}

val appProject = project(":app")
val appBuildDir: Directory = appProject.layout.buildDirectory.get()
val moduleBuildDir: Directory = layout.buildDirectory.get()
val autolinkingJavaSources = "generated/autolinking/src/main/java"

android {
namespace = "com.helloworldreact"
compileSdk = 34

defaultConfig {
minSdk = 24

buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", properties["newArchEnabled"].toString())
buildConfigField("boolean", "IS_HERMES_ENABLED", properties["hermesEnabled"].toString())
}

compileOptions {
Expand All @@ -31,18 +37,9 @@ android {
jvmTarget = "17"
}

buildTypes {
release {
buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", properties["newArchEnabled"].toString())
buildConfigField("boolean", "IS_HERMES_ENABLED", properties["hermesEnabled"].toString())
}
}

sourceSets {
getByName("main") {
assets.srcDirs("$appBuildDir/generated/assets/createBundleReleaseJsAndAssets")
res.srcDirs("$appBuildDir/generated/res/createBundleReleaseJsAndAssets")
java.srcDirs("$moduleBuildDir/$autolinkingJavaSources")
publishing {
multipleVariants {
allVariants()
}
}
}
Expand All @@ -53,20 +50,22 @@ publishing {
groupId = "com.helloworldreact"
artifactId = "helloworld"
version = "0.0.1-local"
artifact("$moduleBuildDir/outputs/aar/helloworldreact-release.aar")
afterEvaluate {
from(components.getByName("default"))
}

pom {
withXml {
asNode().appendNode("dependencies").apply {
configurations.getByName("api").allDependencies.forEach { dependency ->
appendNode("dependency").apply {
appendNode("groupId", dependency.group)
appendNode("artifactId", dependency.name)
appendNode("version", dependency.version)
appendNode("scope", "compile")
}
}
}
/**
* As a result of `from(components.getByName("default")` all of the project
* dependencies are added to `pom.xml` file. We do not need the react-native
* third party dependencies to be a part of it as we embed those dependencies.
*/
val dependenciesNode = (asNode().get("dependencies") as groovy.util.NodeList).first() as groovy.util.Node
dependenciesNode.children()
.filterIsInstance<groovy.util.Node>()
.filter { (it.get("groupId") as groovy.util.NodeList).text() == rootProject.name }
.forEach { dependenciesNode.remove(it) }
}
}
}
Expand All @@ -78,23 +77,29 @@ publishing {
}

dependencies {
api("com.facebook.react:react-android:0.77.0-rc.2")
api("com.facebook.react:hermes-android:0.77.0-rc.2")
api("com.facebook.react:react-android:0.78.0")
api("com.facebook.react:hermes-android:0.78.0")
}

tasks.register<Copy>("copyAutolinkingSources") {
dependsOn(":app:generateAutolinkingPackageList")
from("$appBuildDir/$autolinkingJavaSources")
into("$moduleBuildDir/$autolinkingJavaSources")
}
val moduleBuildDir: Directory = layout.buildDirectory.get()

tasks.named("preBuild").configure{
dependsOn("copyAutolinkingSources")
val buildType = when {
gradle.startParameter.taskNames.any { it.contains("Release", ignoreCase = true) } -> "Release"
else -> "Debug"
}
if (buildType == "Release") {
dependsOn(":app:createBundleReleaseJsAndAssets")
/**
* As a result of `from(components.getByName("default")` all of the project
* dependencies are added to `module.json` file. We do not need the react-native
* third party dependencies to be a part of it as we embed those dependencies.
*/
tasks.register("removeDependenciesFromModuleFile") {
doLast {
file("$moduleBuildDir/publications/mavenAar/module.json").run {
val json = inputStream().use { JsonSlurper().parse(it) as Map<String, Any> }
(json["variants"] as? List<MutableMap<String, Any>>)?.forEach { variant ->
(variant["dependencies"] as? MutableList<Map<String, Any>>)?.removeAll { it["group"] == rootProject.name }
}
writer().use { it.write(JsonOutput.prettyPrint(JsonOutput.toJson(json))) }
}
}
}

tasks.named("generateMetadataFileForMavenAarPublication") {
finalizedBy("removeDependenciesFromModuleFile")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,55 @@ package com.helloworldreact
import android.content.Context
import android.os.Bundle
import android.widget.FrameLayout
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.facebook.react.ReactDelegate
import com.facebook.react.ReactInstanceManager
import com.facebook.react.ReactRootView

object RNViewFactory {
fun createFrameLayout(
context: Context,
params: Bundle? = null,
activity: FragmentActivity,
initialParams: Bundle? = null,
): FrameLayout {
val componentName = "HelloWorld"
val reactHost = ReactNativeHostManager.shared.getReactHost()

if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
val reactDelegate = ReactDelegate(activity, reactHost!!, componentName, initialParams)
val lifecycleObserver = getLifeCycleObserver(reactDelegate)

activity.lifecycle.addObserver(lifecycleObserver)
reactDelegate.loadApp()
return reactDelegate.reactRootView as FrameLayout
}

val instanceManager: ReactInstanceManager? = ReactNativeHostManager.shared.getReactNativeHost()?.reactInstanceManager
val reactView = ReactRootView(context)
val reactNativeHost = ReactNativeHostManager.shared.getReactNativeHost()
val instanceManager: ReactInstanceManager? = reactNativeHost?.reactInstanceManager
reactView.startReactApplication(
instanceManager,
"HelloWorld",
params,
componentName,
initialParams,
)
return reactView
}

private fun getLifeCycleObserver(reactDelegate: ReactDelegate): DefaultLifecycleObserver {
return object : DefaultLifecycleObserver {
override fun onResume(owner: LifecycleOwner) {
reactDelegate.onHostResume()
}

override fun onPause(owner: LifecycleOwner) {
reactDelegate.onHostPause()
}

override fun onDestroy(owner: LifecycleOwner) {
reactDelegate.onHostDestroy()
owner.lifecycle.removeObserver(this)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import android.app.Application
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.react.soloader.OpenSourceMergedSoMapping
import com.facebook.soloader.SoLoader
Expand All @@ -12,39 +16,49 @@ class ReactNativeHostManager {
companion object {
val shared: ReactNativeHostManager by lazy { ReactNativeHostManager() }
private var reactNativeHost: ReactNativeHost? = null
private var reactHost: ReactHost? = null
}

fun getReactNativeHost(): ReactNativeHost? {
return reactNativeHost
}

fun initialize(
application: Application,
) {
if (reactNativeHost == null) {
SoLoader.init(application, OpenSourceMergedSoMapping)
reactNativeHost =
object : DefaultReactNativeHost(application) {
override fun getUseDeveloperSupport(): Boolean {
return BuildConfig.DEBUG
}
fun getReactHost(): ReactHost? {
return reactHost
}

fun initialize(application: Application) {
if (reactNativeHost != null && reactHost != null) {
return
}

SoLoader.init(application, OpenSourceMergedSoMapping)
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// If you opted-in for the New Architecture, we load the native entry point for this app.
load()
}

val reactApp = object : ReactApplication {
override val reactNativeHost: ReactNativeHost =
object : DefaultReactNativeHost(application) {
override fun getPackages(): MutableList<ReactPackage> {
val packages: MutableList<ReactPackage> = PackageList(application).packages
return packages
return PackageList(application).packages
}

override fun getJSMainModuleName(): String {
return "index"
}
override fun getJSMainModuleName(): String = "index"
override fun getBundleAssetName(): String = "index.android.bundle"

override fun getBundleAssetName(): String {
return "index.android.bundle"
}
override fun getUseDeveloperSupport() = BuildConfig.DEBUG

override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
}

override val reactHost: ReactHost
get() = getDefaultReactHost(application, reactNativeHost)
}

reactNativeHost = reactApp.reactNativeHost
reactHost = reactApp.reactHost
}
}
Loading