Skip to content

Commit

Permalink
fix(android): upgrade build.gradle to fix an issue when importing the…
Browse files Browse the repository at this point in the history
… library

See #67 (comment)
  • Loading branch information
ThibaultBee committed Feb 27, 2024
1 parent 872a665 commit eb17ff9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 106 deletions.
140 changes: 47 additions & 93 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
buildscript {
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
def kotlinVersion = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["ApiVideoLivestream_kotlinVersion"]
import com.android.Version

import java.nio.file.Paths

buildscript {
ext.safeExtGet = {prop ->
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : project.properties['ApiVideoLiveStream_' + prop]
}
repositories {
google()
mavenCentral()
Expand All @@ -10,54 +14,38 @@ buildscript {
dependencies {
classpath "com.android.tools.build:gradle:8.1.1"
// noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${safeExtGet('kotlinVersion')}"
}
}

def isNewArchitectureEnabled() {
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["ApiVideoLivestream_" + name]
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"

import groovy.json.JsonSlurper

// https://github.com/callstack/react-native-builder-bob/discussions/359
def registrationCompat = {
def reactNativeManifest = file("$projectDir/../node_modules/react-native/package.json").exists()
? file("$projectDir/../node_modules/react-native/package.json") // developer mode, to run example app
: file("$projectDir/../../react-native/package.json")
def reactNativeVersion = new JsonSlurper().parseText(reactNativeManifest.text).version as String
// Fabric was introduced at [email protected], full CMake support were introduced at [email protected]
// Use Android.mk for compatibility with [email protected]/0.69
reactNativeVersion.matches('(0.68.*|0.69.*)')
}()

def codegenViewLibraryName = "ApiVideoLiveStreamView"
def codegenViewModuleName = {
// Autolink for Fabric uses codegenConfig.name in package.json since [email protected]
// Use codegenViewLibraryName for compatibility with [email protected]/0.69
def libraryManifestJson = new JsonSlurper().parseText(file("$projectDir/../package.json").text)
registrationCompat ? codegenViewLibraryName : libraryManifestJson.codegenConfig.name
}()

def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }

if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"
def getExtOrIntegerDefault(prop) {
return getExtOrDefault(prop).toInteger()
}

def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["ApiVideoLivestream_" + name]
static def findNodeModulePath(baseDir, packageName) {
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 candidatePath = Paths.get(basePath.toString(), "node_modules", packageName)
if (candidatePath.toFile().exists()) {
return candidatePath.toString()
}
basePath = basePath.getParent()
}
return null
}

def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ApiVideoLivestream_" + name]).toInteger()
def isNewArchitectureEnabled() {
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

def supportsNamespace() {
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
static def supportsNamespace() {
def parsed = Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
def major = parsed[0].toInteger()
def minor = parsed[1].toInteger()

Expand All @@ -69,6 +57,12 @@ def supportsNamespace() {
return major >= 8
}

apply plugin: 'com.android.library'
if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}
apply plugin: 'kotlin-android'

android {
if (supportsNamespace()) {
namespace "video.api.reactnative.livestream"
Expand All @@ -87,42 +81,6 @@ android {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
buildConfigField "String", "CODEGEN_MODULE_REGISTRATION", (isNewArchitectureEnabled() && registrationCompat ? "\"${codegenViewModuleName}_registration\"" : "null")
if (isNewArchitectureEnabled() && registrationCompat) {
def reactAndroidProject = project(':ReactAndroid')
externalNativeBuild {
ndkBuild {
arguments "APP_PLATFORM=android-21",
"APP_STL=c++_shared",
"NDK_TOOLCHAIN_VERSION=clang",
"GENERATED_SRC_DIR=$buildDir/generated/source", // for react_codegen_* in this library's codegen/jni
"PROJECT_BUILD_DIR=${appProject.buildDir}", // for REACT_NDK_EXPORT_DIR in ReactAndroid's Android-prebuilt.mk
"REACT_ANDROID_DIR=${reactAndroidProject.projectDir}",
"REACT_ANDROID_BUILD_DIR=${reactAndroidProject.buildDir}",
"CODEGEN_MODULE_NAME=$codegenViewModuleName"
cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
cppFlags "-std=c++17"
targets "${codegenViewModuleName}_registration"
}
}
}
}
if (isNewArchitectureEnabled() && registrationCompat) {
// We configure the NDK build only if you decide to opt-in for the New Architecture.
externalNativeBuild {
ndkBuild {
path "Android.mk"
}
}
}
buildTypes {
release {
minifyEnabled false
}
}

lintOptions {
disable "GradleCompatible"
}

compileOptions {
Expand All @@ -133,24 +91,28 @@ android {
sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += [
"src/newarch",
// This is needed to build Kotlin project with NewArch enabled
"${project.buildDir}/generated/source/codegen/java"
]
java.srcDirs += ["src/newarch"]
} else {
java.srcDirs += ["src/oldarch"]
}
}
}
}

repositories {
mavenCentral()
google()
def reactNativePath = findNodeModulePath(projectDir, "react-native")
def codegenPath = findNodeModulePath(projectDir, "@react-native/codegen")
if (codegenPath == null) {
// Compat for 0.71 and lower (to be removed)
codegenPath = findNodeModulePath(projectDir, "react-native-codegen")
}

def kotlinVersion = getExtOrDefault("kotlinVersion")
repositories {
maven {
url "${reactNativePath}/android"
}
mavenCentral()
google()
}

dependencies {
// For < 0.71, this will be from the local maven repo
Expand All @@ -166,11 +128,3 @@ dependencies {

implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
}

if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = codegenViewLibraryName
codegenJavaPackageName = "video.api.reactnative.livestream"
}
}
10 changes: 5 additions & 5 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ApiVideoLivestream_kotlinVersion=1.8.0
ApiVideoLivestream_minSdkVersion=21
ApiVideoLivestream_targetSdkVersion=34
ApiVideoLivestream_compileSdkVersion=34
ApiVideoLivestream_ndkVersion=25.1.8937393
ApiVideoLiveStream_kotlinVersion=1.8.0
ApiVideoLiveStream_minSdkVersion=21
ApiVideoLiveStream_targetSdkVersion=34
ApiVideoLiveStream_compileSdkVersion=34
ApiVideoLiveStream_ndkVersion=25.1.8937393
8 changes: 0 additions & 8 deletions android/src/newarch/LiveStreamViewManagerSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,4 @@ abstract class LiveStreamViewManagerSpec<T : View> : SimpleViewManager<T>(),
override fun getDelegate(): ViewManagerDelegate<T>? {
return mDelegate
}

companion object {
init {
if (BuildConfig.CODEGEN_MODULE_REGISTRATION != null) {
SoLoader.loadLibrary(BuildConfig.CODEGEN_MODULE_REGISTRATION)
}
}
}
}

0 comments on commit eb17ff9

Please sign in to comment.