vinivendra / Gryphon

The Swift to Kotlin translator.
https://vinivendra.github.io/Gryphon/
Other
609 stars 46 forks source link

Kotlin target tries to build Android app with system's JDK #106

Open batkov opened 3 years ago

batkov commented 3 years ago

Basically I'm getting error:

Showing All Messages
> Unable to make field private final java.lang.String java.io.File.path accessible: module java.base does not "opens java.io" to unnamed module @165ff712
Screen Shot 2021-06-01 at 8 15 51 PM

But from Android Studio everything looks good. It looks like when we are building from the Xcode with Kotlin target, jdk installed in the macOS system. I've tried to change STUDIO_JDK and JAVA_HOME but it did not work.

I believe there is some simple solution, like set env variable, pass script parameter or update Android project settings.

vinivendra commented 3 years ago

Thanks for reporting this bug. I don't have a lot of experience with Android compilation but let's try to figure out what's happening to see how we can fix it with Gryphon.

Basically what Gryphon does in the Kotlin target is switch to the Android app's folder and call Gradle to compile the app:

cd "$ANDROID_ROOT"
./gradlew compileDebugSources

This is the main part of a larger script that you can probably see in your iOS project's folder under .gryphon/scripts/compileKotlin.sh if you want to.

Could you try to run ./gradlew compileDebugSources in your Android app's folder and see what you get?

batkov commented 3 years ago

Ok, trying to reproduce. First, I forgot to set $ANDROID_ROOT again and got an error .gryphon/scripts/compileKotlin.sh: line 21: cd: ../Android: No such file or directory

I believe it would be beneficial to expand an error with message like Please make sure you've set $ANDROID_ROOT build variable properly

batkov commented 3 years ago

So, on fresh macOS i'm getting

./gradlew compileDebugSource
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

Which means that build tries to build app with system's JDK. Bunch of fixes is here. But basically I think it is not right way to fix the issue, because the tools to build app via Xcode and Android Studio will differ. I'll try to find solution and come back to you.

batkov commented 3 years ago

Here is my findings.

I've resolved problem locally by changing the script: from

bash .gryphon/scripts/compileKotlin.sh

to

env JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home bash .gryphon/scripts/compileKotlin.sh

This is far from elegant solution, but it is much better than using duplicate JDK in one system. I would like to see the good solution as following. root folder of project would be having:

.gryphon-android-root // '../my_android_app_folder'
.gryphon-jdk-path // empty/nonexistent means using of system's JAVA_HOME, existent passed though 'env JAVA_HOME='
.gryphon-android-src // root path to all files

I know I mentioning another issue here, but having .gryphon-android-root + .gryphon-android-src would mean that android app could change location/package/module without having iOS app changing its sources, only few files that could be in gitignore.

vinivendra commented 3 years ago

Hey @batkov, I implemented the config file. It doesn't yet support the Java home setting you asked for, but it's a first step. Let me know if it works for you for now, ok? I'll try to add the Java home support later using this system, that sounds like a good idea.