Skip to content

Commit

Permalink
fix: update gradle files for 0.73 support
Browse files Browse the repository at this point in the history
Current lib should just work with 0.73 but taking preliminary steps to keep up to date based on the work specified [here](react-native-community/discussions-and-proposals#671)
And in particular on the revised outputs of [create-react-native-library CLI](https://github.com/callstack/react-native-builder-bob/blob/fc8744092501ba00fd11bdb69e1ecb46fbb63c0f/packages/create-react-native-library/templates/native-common/android/build.gradle)

- Updated build.gradle and gradle.properties to match new CLI outputs
- Added new AndroidManifest.xml without namespace
- Added namespace in build.gradle only if Gradle 7.3+
  • Loading branch information
oscb committed Feb 9, 2024
1 parent 285a3bd commit 3862b4e
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 260 deletions.
133 changes: 48 additions & 85 deletions packages/core/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
buildscript {
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['AnalyticsReactNative_kotlinVersion']
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["AnalyticsReactNative_kotlinVersion"]

repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "com.android.tools.build:gradle:7.2.1"
// noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
def isNewArchitectureEnabled() {
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

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

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

def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['AnalyticsReactNative_' + name]
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["AnalyticsReactNative_" + name]
}

def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['AnalyticsReactNative_' + name]).toInteger()
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["AnalyticsReactNative_" + name]).toInteger()
}

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

// Namespace support was added in 7.3.0
return (major == 7 && minor >= 3) || major >= 8
}

android {
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
buildToolsVersion getExtOrDefault('buildToolsVersion')
if (supportsNamespace()) {
namespace "com.segmentanalyticsreactnative"

sourceSets {
main {
manifest.srcFile "src/main/AndroidManifestNew.xml"
}
}
}

compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

defaultConfig {
minSdkVersion 21
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
compileSdkVersion = getExtOrIntegerDefault('compileSdkVersion')
versionCode 1
versionName "1.0"
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")

}

Expand All @@ -43,9 +66,11 @@ android {
minifyEnabled false
}
}

lintOptions {
disable 'GradleCompatible'
disable "GradleCompatible"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
Expand All @@ -54,80 +79,18 @@ android {

repositories {
mavenCentral()
jcenter()
google()

def found = false
def defaultDir = null
def androidSourcesName = 'React Native sources'

if (rootProject.ext.has('reactNativeAndroidRoot')) {
defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
} else {
defaultDir = new File(
projectDir,
'/../../../node_modules/react-native/android'
)
}

if (defaultDir.exists()) {
maven {
url defaultDir.toString()
name androidSourcesName
}

logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
found = true
} else {
def parentDir = rootProject.projectDir

1.upto(5, {
if (found) return true
parentDir = parentDir.parentFile

def androidSourcesDir = new File(
parentDir,
'node_modules/react-native'
)

def androidPrebuiltBinaryDir = new File(
parentDir,
'node_modules/react-native/android'
)

if (androidPrebuiltBinaryDir.exists()) {
maven {
url androidPrebuiltBinaryDir.toString()
name androidSourcesName
}

logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
found = true
} else if (androidSourcesDir.exists()) {
maven {
url androidSourcesDir.toString()
name androidSourcesName
}

logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
found = true
}
})
}

if (!found) {
throw new GradleException(
"${project.name}: unable to locate React Native android sources. " +
"Ensure you have you installed React Native as a dependency in your project and try again."
)
}
}

def kotlin_version = getExtOrDefault('kotlinVersion')
def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
// noinspection GradleDynamicVersion
api 'com.facebook.react:react-native:+'
// For < 0.71, this will be from the local maven repo
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation project(path: ':segment_sovran-react-native')
// Requires sovran for communication
implementation project(path: ':segment_sovran-react-native')
}

7 changes: 4 additions & 3 deletions packages/core/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
AnalyticsReactNative_kotlinVersion=1.7.0
AnalyticsReactNative_compileSdkVersion=29
AnalyticsReactNative_buildToolsVersion=29.0.2
AnalyticsReactNative_targetSdkVersion=29
AnalyticsReactNative_minSdkVersion=21
AnalyticsReactNative_targetSdkVersion=31
AnalyticsReactNative_compileSdkVersion=31
AnalyticsReactNative_ndkversion=21.4.7075529
3 changes: 3 additions & 0 deletions packages/core/android/src/main/AndroidManifestNew.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
135 changes: 50 additions & 85 deletions packages/plugins/plugin-advertising-id/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,49 +1,76 @@
buildscript {
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['AnalyticsReactNativePluginAdvertisingId_kotlinVersion']
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["AnalyticsReactNative_kotlinVersion"]

repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "com.android.tools.build:gradle:7.2.1"
// noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
def isNewArchitectureEnabled() {
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

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

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

def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['AnalyticsReactNativePluginAdvertisingId_' + name]
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["AnalyticsReactNativePluginAdvertisingId_" + name]
}

def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['AnalyticsReactNativePluginAdvertisingId_' + name]).toInteger()
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["AnalyticsReactNativePluginAdvertisingId_" + name]).toInteger()
}

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

// Namespace support was added in 7.3.0
return (major == 7 && minor >= 3) || major >= 8
}

android {
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
if (supportsNamespace()) {
namespace "com.analyticsreactnativepluginadvertisingid"

sourceSets {
main {
manifest.srcFile "src/main/AndroidManifestNew.xml"
}
}
}

compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

defaultConfig {
minSdkVersion 19
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
versionCode 1
versionName "1.0"

minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")

}

buildTypes {
release {
minifyEnabled false
}
}

lintOptions {
disable 'GradleCompatible'
disable "GradleCompatible"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
Expand All @@ -52,80 +79,18 @@ android {

repositories {
mavenCentral()
jcenter()
google()

def found = false
def defaultDir = null
def androidSourcesName = 'React Native sources'

if (rootProject.ext.has('reactNativeAndroidRoot')) {
defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
} else {
defaultDir = new File(
projectDir,
'/../../../node_modules/react-native/android'
)
}

if (defaultDir.exists()) {
maven {
url defaultDir.toString()
name androidSourcesName
}

logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
found = true
} else {
def parentDir = rootProject.projectDir

1.upto(5, {
if (found) return true
parentDir = parentDir.parentFile

def androidSourcesDir = new File(
parentDir,
'node_modules/react-native'
)

def androidPrebuiltBinaryDir = new File(
parentDir,
'node_modules/react-native/android'
)

if (androidPrebuiltBinaryDir.exists()) {
maven {
url androidPrebuiltBinaryDir.toString()
name androidSourcesName
}

logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
found = true
} else if (androidSourcesDir.exists()) {
maven {
url androidSourcesDir.toString()
name androidSourcesName
}

logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
found = true
}
})
}

if (!found) {
throw new GradleException(
"${project.name}: unable to locate React Native android sources. " +
"Ensure you have you installed React Native as a dependency in your project and try again."
)
}
}

def kotlin_version = getExtOrDefault('kotlinVersion')
def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
// noinspection GradleDynamicVersion
api 'com.facebook.react:react-native:+'
// For < 0.71, this will be from the local maven repo
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
// Required for AdvertisingID
implementation "com.google.android.gms:play-services-ads-identifier:18.0.1"
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
AnalyticsReactNativePluginAdvertisingId_kotlinVersion=1.7.0
AnalyticsReactNativePluginAdvertisingId_compileSdkVersion=31
AnalyticsReactNativePluginAdvertisingId_minSdkVersion=21
AnalyticsReactNativePluginAdvertisingId_targetSdkVersion=31
AnalyticsReactNativePluginAdvertisingId_compileSdkVersion=31
AnalyticsReactNativePluginAdvertisingId_ndkversion=21.4.7075529
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Loading

0 comments on commit 3862b4e

Please sign in to comment.