From 6189080c9aac89aa3d2a4e60049999d8880bc971 Mon Sep 17 00:00:00 2001 From: Seyed Mostafa Hasani Date: Wed, 24 Jul 2024 11:37:19 +0330 Subject: [PATCH] feat(android): add error handling for Kotlin version (#4018) * feat(android): add error handling for Kotlin version mismatch * fix: lint error * refactor: use variables from gradle file * chore: downgrade required Kotlin version * refactor: check Kotlin version * refactor: kotlin variables in build.gradle * refactor: kotlin variables in build.gradle * chore(doc): update document * chore: add dependency to build.gradle for a specific version of react-native * fix: remove additional dependency --- android/build.gradle | 31 ++++++++++++++++--- android/gradle.properties | 2 +- .../exoplayer/FullScreenPlayerView.kt | 5 ++- docs/pages/installation.md | 6 ++-- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 7920bf65..5492c667 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -5,6 +5,21 @@ apply plugin: 'kotlin-android' buildscript { def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['RNVideo_kotlinVersion'] + def requiredKotlinVersion = project.properties['RNVideo_kotlinVersion'] + + def isVersionAtLeast = { version, requiredVersion -> + def (v1, v2) = [version, requiredVersion].collect { it.tokenize('.')*.toInteger() } + for (int i = 0; i < Math.max(v1.size(), v2.size()); i++) { + int val1 = i < v1.size() ? v1[i] : 0 + int val2 = i < v2.size() ? v2[i] : 0 + if (val1 < val2) { + return false + } else if (val1 > val2) { + return true + } + } + return true + } repositories { mavenCentral() @@ -13,9 +28,17 @@ buildscript { dependencies { classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") } + + ext { + if (!isVersionAtLeast(kotlin_version, requiredKotlinVersion)) { + throw new GradleException("Kotlin version mismatch: Project is using Kotlin version $kotlin_version, but it must be at least $requiredKotlinVersion. Please update the Kotlin version.") + } else { + println("Kotlin version is correct: $kotlin_version") + } + } } -// This looks funny but it's necessary to keep backwards compatibility (: +// This looks funny but it's necessary to keep backwards compatibility (: def safeExtGet(prop) { return rootProject.ext.has(prop) ? rootProject.ext.get(prop) : rootProject.ext.has("RNVideo_" + prop) ? @@ -184,7 +207,6 @@ repositories { } def media3_version = safeExtGet('media3Version') -def kotlin_version = safeExtGet('kotlinVersion') def androidxCore_version = safeExtGet('androidxCoreVersion') def androidxActivity_version = safeExtGet('androidxActivityVersion') @@ -239,7 +261,7 @@ dependencies { implementation "androidx.media3:media3-exoplayer-rtsp:$media3_version" } } - + // For ad insertion using the Interactive Media Ads SDK with ExoPlayer if (ExoplayerDependencies["useExoplayerIMA"]) { if (media3_buildFromSource) { @@ -280,5 +302,4 @@ dependencies { // Common functionality used across multiple media libraries implementation "androidx.media3:media3-common:$media3_version" } - implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" -} +} \ No newline at end of file diff --git a/android/gradle.properties b/android/gradle.properties index d4b23166..46daceb2 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,4 +1,4 @@ -RNVideo_kotlinVersion=1.7.0 +RNVideo_kotlinVersion=1.8.0 RNVideo_minSdkVersion=23 RNVideo_targetSdkVersion=34 RNVideo_compileSdkVersion=34 diff --git a/android/src/main/java/com/brentvatne/exoplayer/FullScreenPlayerView.kt b/android/src/main/java/com/brentvatne/exoplayer/FullScreenPlayerView.kt index b66c1e88..00f93975 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/FullScreenPlayerView.kt +++ b/android/src/main/java/com/brentvatne/exoplayer/FullScreenPlayerView.kt @@ -91,13 +91,12 @@ class FullScreenPlayerView( parent = null } - private fun getFullscreenIconResource(isFullscreen: Boolean): Int { - return if (isFullscreen) { + private fun getFullscreenIconResource(isFullscreen: Boolean): Int = + if (isFullscreen) { androidx.media3.ui.R.drawable.exo_icon_fullscreen_exit } else { androidx.media3.ui.R.drawable.exo_icon_fullscreen_enter } - } private fun updateFullscreenButton(playerControlView: LegacyPlayerControlView, isFullscreen: Boolean) { val imageButton = playerControlView.findViewById(com.brentvatne.react.R.id.exo_fullscreen) diff --git a/docs/pages/installation.md b/docs/pages/installation.md index e09aa972..26e1207c 100644 --- a/docs/pages/installation.md +++ b/docs/pages/installation.md @@ -57,12 +57,14 @@ $RNVideoUseGoogleIMA=true ## Android -From version >= 6.0.0, your application needs to have kotlin version >= 1.7.0 +From version >= 6.0.0, your application needs to have kotlin version >= 1.8.0 ```: buildscript { ... - ext.kotlinVersion = '1.7.0' + ext.kotlinVersion = '1.8.0', + ext.compileSdkVersion = 34 + ext.targetSdkVersion = 34 ... } ```