Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): add error handling for Kotlin version mismatch #4018

Conversation

seyedmostafahasani
Copy link
Collaborator

@seyedmostafahasani seyedmostafahasani commented Jul 17, 2024

Summary

Create a custom task to check the Kotlin metadata version.

Motivation

Show clarify error message for Kotlin version mismatch.
And fix: #4009

Changes

Test plan

I changed the Kotlin version in the sample app to 1.7.0, then ran the app and encountered a new error: Kotlin version mismatch: Project is using Kotlin version '1.7.0' but it must be at least '1.8.0'. Please update the Kotlin version. After reverting the Kotlin version to 1.8.0 or higher, the app worked correctly.

Copy link
Member

@KrzysztofMoch KrzysztofMoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments - we also need remember to update documentation (also we should add info that we require compileSdkVersion & targetSdkVersion to be set to 34

android/build.gradle Outdated Show resolved Hide resolved
Comment on lines 46 to 48
tasks.matching { it.name.startsWith('pre') || it.name == 'compileDebugKotlin' }.all {
dependsOn checkKotlinVersion
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we need to do it that we - we can convert checkKotlinVersion task to function and call this function eg. in buildscript or somewhere else

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's refactored.

@freeboub
Copy link
Collaborator

Hello, I don't understand which version of kotlin is used in the sample. I tried to look for it and it seems to be 1.7.0, please correct me if I am wrong ?

@KrzysztofMoch
Copy link
Member

KrzysztofMoch commented Jul 18, 2024

@freeboub 1.7.0 is version "set" by react-native-video - sample is using 1.9.22 (as it's forced by react-native)
Thats why we did not have those issue with kotlin bcs our sample is using hight kotlin version

@freeboub
Copy link
Collaborator

Just for memory:
release react-native 0.73.0 : Kotlin to 1.8.0 and JDK Toolchain to 11
release react-native 0.72.0 : Kotlin to 1.7.22 for Gradle

@freeboub
Copy link
Collaborator

I had the issue while checking another bug. To workaround it I just remove :
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" from build.gradle.
Won't it be better to use the kotlin version used by react native and add the dependency only on old react native version which don't use it ?

@seyedmostafahasani
Copy link
Collaborator Author

@freeboub, if removing the kotlin-stdlib dependency fixes the issue, we can handle it conditionally based on the React Native version. For example, we can add the dependency only for React Native versions below a certain threshold:

if (reactNativeVersion < '0.65.0') { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" }

However, I don't think this dependency is the root cause of the problem. We should investigate further to identify the exact issue.

WDYT?

@KrzysztofMoch
Copy link
Member

I had the issue while checking another bug. To workaround it I just remove :

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" from build.gradle.

Won't it be better to use the kotlin version used by react native and add the dependency only on old react native version which don't use it ?

We should remove it I guess - I have checked template (react-native-create-library) and there is no line like this. You can also look at other libraries

@seyedmostafahasani
Copy link
Collaborator Author

@KrzysztofMoch you're right. I've checked other libraries like view-shot and vision-camera, and I didn't see it. So, if you agree, we can remove it.

@freeboub
Copy link
Collaborator

We all agree to remove it then :) I wonder if we should put a note in doc for old react native version (0.69 ?) to add it manually in app build.gradle ?

@KrzysztofMoch
Copy link
Member

I think this is good idea - we can put it even in installation section

@seyedmostafahasani
Copy link
Collaborator Author

seyedmostafahasani commented Jul 20, 2024

We can handle it in build.gradle. If the React Native version is 0.69.0 or below, it will be added. For higher versions, it doesn't apply.
@freeboub @KrzysztofMoch WDYT?

@freeboub
Copy link
Collaborator

Ok for me but please check in which react native version kotlin was introduced. If it is before 0.69, no need to add it as it is noticed that 0.69 is the lowest supported version

@seyedmostafahasani
Copy link
Collaborator Author

@freeboub I checked and noticed that starting from react native version 0.73.0, Kotlin was introduced. So, if the react native version of the app is between 0.69 and 0.72, we can add it.
https://reactnative.dev/blog/2023/12/06/0.73-debugging-improvements-stable-symlinks#kotlin-template-on-android

@freeboub
Copy link
Collaborator

@freeboub I checked and noticed that starting from react native version 0.73.0, Kotlin was introduced. So, if the react native version of the app is between 0.69 and 0.72, we can add it.
https://reactnative.dev/blog/2023/12/06/0.73-debugging-improvements-stable-symlinks#kotlin-template-on-android

Yes, let's go with that !

@seyedmostafahasani
Copy link
Collaborator Author

@freeboub @KrzysztofMoch @YangJonghun it has been updated according to the final suggestion.


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

def getReactNativeVersion() {
def packageJsonFile = file("$rootDir/../package.json")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Package.json is not necessarily in ../ is there any other way to retrieve the version ? I am also wondering if some frameworks can hide react-native in sub package 🤨

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

if (isReactNativeVersionBetween(reactNativeVersion, minReactNativeVersion, maxReactNativeVersionExclusive)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are blocking build for version < minVersion . I don't think this is really expected...

android/build.gradle Show resolved Hide resolved
def reactNativeVersion = getReactNativeVersion()

def isReactNativeVersionBetween = { version, minVersion, maxVersionExclusive ->
def versionParts = { v -> v.replaceAll(/[^0-9.]/, '').tokenize('.')*.toInteger() }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it work if user choose a RC version ? In a more general way, is there any gradle package to handle versioning check ?

@freeboub
Copy link
Collaborator

I just had a review , but in a more general way, I think we shouldn't integrate kotlin on the react native video side, but just let this dependency on the app side.
Your PR brings a lot of complexity which will be painful to maintain...
Don't worry the PR looks fine, thank you again for handling this issue!!! I just doubting of the best way to hanle it !

@seyedmostafahasani
Copy link
Collaborator Author

@freeboub I couldn't agree more with you. I was fixing this issue last night, and I thought about it. I think it's better to handle it on the app side. As you said earlier, maintaining these changes will be difficult and complex in the future. So, we can update the documentation installation.
Thanks for responding to my PR!

@seyedmostafahasani
Copy link
Collaborator Author

Let's see @KrzysztofMoch's opinion about it, then we can apply it to this PR.

android/build.gradle Outdated Show resolved Hide resolved
@KrzysztofMoch
Copy link
Member

@freeboub I would like to release 6.4.3 after this PR merge - can you look into it ?

@KrzysztofMoch KrzysztofMoch merged commit 6189080 into TheWidlarzGroup:master Jul 24, 2024
4 checks passed
@seyedmostafahasani seyedmostafahasani deleted the feat/error-handling-kotlin-version branch July 24, 2024 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: Android Kotlin version mismatch
4 participants