Files

193 lines
6.2 KiB
Groovy

import java.nio.file.Paths
buildscript {
// The Android Gradle plugin is only required when opening the android folder stand-alone.
// This avoids unnecessary downloads and potential conflicts when the library is included as a
// module dependency in an application project.
if (project == rootProject) {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:4.2.2")
}
}
}
task downloadDependencies() {
description 'Download all dependencies to the Gradle cache'
doLast {
configurations.findAll().each { config ->
if (config.name.contains("minReactNative") && config.canBeResolved) {
print config.name
print '\n'
config.files
}
}
}
}
def getExtOrInitialValue(name, initialValue) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : initialValue
}
def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['ReactNativeCameraRoll_' + name]
}
def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['ReactNativeCameraRoll_' + name]).toInteger()
}
apply plugin: 'com.android.library'
/**
* Original code: https://github.com/facebook/react-native/blob/main/packages/react-native/ReactAndroid/build.gradle#L382
*
* Finds the path of the installed npm package with the given name using Node's
* module resolution algorithm, which searches "node_modules" directories up to
* the file system root. This handles various cases, including:
*
* - Working in the open-source RN repo:
* Gradle: /path/to/react-native/ReactAndroid
* Node module: /path/to/react-native/node_modules/[package]
*
* - Installing RN as a dependency of an app and searching for hoisted
* dependencies:
* Gradle: /path/to/app/node_modules/react-native/ReactAndroid
* Node module: /path/to/app/node_modules/[package]
*
* - Working in a larger repo (e.g., Facebook) that contains RN:
* Gradle: /path/to/repo/path/to/react-native/ReactAndroid
* Node module: /path/to/repo/node_modules/[package]
*
* The search begins at the given base directory (a File object). The returned
* path is a string.
*/
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 resolveReactNativeDirectory() {
def reactNative = file("${findNodeModulePath(rootProject.projectDir, "react-native")}")
if (reactNative.exists()) {
return reactNative
}
throw new Exception(
"[react-native-cameraroll] Unable to resolve react-native location in " +
"node_modules. You should add project extension property (in app/build.gradle) " +
"`REACT_NATIVE_NODE_MODULES_DIR` with path to react-native."
)
}
def REACT_NATIVE_DIR = resolveReactNativeDirectory()
def reactProperties = new Properties()
file("$REACT_NATIVE_DIR/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }
def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME")
def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.startsWith("0.0.0-") ? 1000 : REACT_NATIVE_VERSION.split("\\.")[1].toInteger()
def isNewArchitectureEnabled() {
// To opt-in for the New Architecture, you can either:
// - Set `newArchEnabled` to true inside the `gradle.properties` file
// - Invoke gradle with `-newArchEnabled=true`
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}
if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"
}
def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
def shouldUseNameSpace = agpVersion >= 7
def PACKAGE_PROP = "package=\"com.reactnativecommunity.cameraroll\""
def manifestOutFile = file("${projectDir}/src/main/AndroidManifest.xml")
def manifestContent = manifestOutFile.getText()
if(shouldUseNameSpace){
manifestContent = manifestContent.replaceAll(
PACKAGE_PROP,
''
)
} else {
if(!manifestContent.contains("$PACKAGE_PROP")){
manifestContent = manifestContent.replace(
'<manifest',
"<manifest $PACKAGE_PROP "
)
}
}
manifestContent.replaceAll(" ", " ")
manifestOutFile.write(manifestContent)
android {
if(shouldUseNameSpace){
namespace = "com.reactnativecommunity.cameraroll"
}
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
// 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
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
}
sourceSets.main {
java {
if (!isNewArchitectureEnabled()) {
srcDirs += 'src/paper/java'
}
}
}
lintOptions{
abortOnError false
}
}
repositories {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
google()
mavenLocal()
mavenCentral()
}
dependencies {
if (isNewArchitectureEnabled() && REACT_NATIVE_MINOR_VERSION < 71) {
implementation project(":ReactAndroid")
} else {
//noinspection GradleDynamicVersion
implementation 'com.facebook.react:react-native:+'
}
}