godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.17k stars 21.2k forks source link

4.2 version android plugin crash with java.lang.ClassNotFoundException #86152

Closed code-with-max closed 11 months ago

code-with-max commented 11 months ago

Tested versions

Reproduce in 4.2.1

System information

Ubuntu 23.10, Godot_v4.2.1-stable_linux.x86_64

Issue description

App died with java.lang.ClassNotFoundException when used this template - https://github.com/m4gr3d/Godot-Android-Plugin-Template It`s crashed with any imported classes, not only GoogleApiAvailability, also task.Task and others.

It seems that the classes from dependencies are not included in the apk's classes.dex and game always crash with java.lang.ClassNotFoundException.

Steps to reproduce

  1. Start new plug-in with - https://github.com/m4gr3d/Godot-Android-Plugin-Template
  2. Import any library, e.g.
    import com.google.android.gms.common.GoogleApiAvailability

    and call it:

    val googleApiAvailability = GoogleApiAvailability.getInstance()
    val status: Int = googleApiAvailability.isGooglePlayServicesAvailable(activity!!)
  3. Assemble and install plug-in
  4. Run game with it
  5. Watch logcat:
    2-13 11:28:55.970  5464  5464 F DEBUG   : Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.GoogleApiAvailability" on path: DexPathList[[zip file "/data/app/~~5i67fVLY5Ef9yC4AfWCKlw==/org.godotengine.classicsnake-ZMW6o20AcigpqLXz7bibYQ==/base.apk"],nativeLibraryDirectories=[/data/app/~~5i67fVLY5Ef9yC4AfWCKlw==/org.godotengine.classicsnake-ZMW6o20AcigpqLXz7bibYQ==/lib/x86_64, /data/app/~~5i67fVLY5Ef9yC4AfWCKlw==/org.godotengine.classicsnake-ZMW6o20AcigpqLXz7bibYQ==/base.apk!/lib/x86_64, /system/lib64, /system_ext/lib64]]

Minimal reproduction project (MRP)

Godot-Android-Plugin-Template.zip

Rrenovation commented 11 months ago

I addressed the issue in my project

I overridden the export_plugin.gd example:

func _get_android_dependencies (platform, debug):
    if debug:
        return PackedStringArray(["com.google.android.gms:play-services-base:18.2.0"])
    else:
        return PackedStringArray(["com.google.android.gms:play-services-base:18.2.0"])
code-with-max commented 11 months ago

@Rrenovation Thanks my friend!!! It works.

kyadalu1 commented 11 months ago

@Rrenovation Thanks your for your answer. I am little confused. Suppose my android library uses implementation("androidx.biometric:biometric:1.1.0") do i still need to com.google.android.gms:play-services-base:18.2.0 in PackedStringArray

Also similar issue occurs when i try to use Kotlin's coroutines, even for that do i still need to add com.google.android.gms:play-services-base:18.2.0 in PackedStringArray

Rrenovation commented 11 months ago

@kyadalu1 I have tried many things with the 4.2 version of the Android plugin, and the compileOnly and implementation parameters seem more like compileOnly. implementation appears not to take effect.

myselfuser1 commented 11 months ago

@Rrenovation Why did you use com.google.android.gms:play-services-base:18.2.0. I don't get it

code-with-max commented 11 months ago

@pritish-thub it's main class . Needed for my case: googleApiAvailability

kyadalu1 commented 11 months ago

@trash-max Doesn't seem to work for my use case. I am trying to import androidx.biometric:biometric:1.1.0 so i refactored the above code to

func _get_android_dependencies (platform, debug):
        if debug:
            return PackedStringArray(["androidx.biometric:biometric:1.1.0"])
        else:
            return PackedStringArray(["androidx.biometric:biometric:1.1.0"])
kyadalu1 commented 11 months ago

It works now, i am facing different issue. Thanks @trash-max @Rrenovation

kyadalu1 commented 11 months ago

@trash-max @Rrenovation One question which i would like to ask you guys is i face similar issue here. It is due to kotlin coroutines. Coroutines are part of kotlin lang and i am using version 1.9.10 and kotlin is not any third party dependency. If i use normal kotlin code my plugin works but as soon as i use coroutines it craashes. Any advice

myselfuser1 commented 11 months ago

@trash-max @Rrenovation One question which i would like to ask you guys is i face similar issue here. It is due to kotlin coroutines. Coroutines are part of kotlin lang and i am using version 1.9.10 and kotlin is not any third party dependency. If i use normal kotlin code my plugin works but as soon as i use coroutines it craashes. Any advice

Yup same here

myselfuser1 commented 11 months ago

@kyadalu1 Maybe the Godot android plugin might be using old kotlin version when coroutines where not part of kotlin and had to be added as a separate dependency

code-with-max commented 11 months ago

@kyadalu1 Godot 4.2.1 uses kotlin version 1.7.0 by default.

You can update it if need in /android/build/config.gradle

This part:

ext.versions = [
    androidGradlePlugin: '7.2.1',
    compileSdk         : 33,
    // Also update 'platform/android/export/export_plugin.cpp#OPENGL_MIN_SDK_VERSION'
    minSdk             : 21,
    // Also update 'platform/android/export/export_plugin.cpp#DEFAULT_TARGET_SDK_VERSION'
    targetSdk          : 33,
    buildTools         : '33.0.2',
    kotlinVersion      : '1.7.0',
    fragmentVersion    : '1.3.6',
    nexusPublishVersion: '1.1.0',
    javaVersion        : 17,
    // Also update 'platform/android/detect.py#get_ndk_version()' when this is updated.
    ndkVersion         : '23.2.8568313'
kyadalu1 commented 11 months ago

Thanks @trash-max for your reply, I just updated it to kotlinVersion : '1.9.20', but still same issue

m4gr3d commented 11 months ago

Hey all, this is behavior as intended. The Godot Android library only includes Godot related classes and logic, and so any additional dependencies need to be specified both within build.gradle.kts and within the _get_android_dependencies method as @Rrenovation mentioned.

@kyadalu1 Kotlin coroutines are a separate dependency to avoid inflicting the cost on Kotlin users that don't make use of coroutines capabilities. You can find information about how to include the coroutine dependency at https://developer.android.com/kotlin/coroutines#dependency

kyadalu1 commented 11 months ago

@m4gr3d Godot relies on kotlin v1.7.0 so i think coroutines needs to be added as separate dependency. In many of my native android projects which uses kotlin v1.9.20 and even for some which uses kotlin v1.8.20, i never had to add couroutines as separate dependency.

Will Godot 4.3 update kotlin version to 1.8 or 1.9?