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

Remove the restriction on supported types for Godot Android plugins #97500

Conversation

m4gr3d
Copy link
Contributor

@m4gr3d m4gr3d commented Sep 26, 2024

The first commit updates the Android plugin implementation to use JavaClassWrapper which was fixed in #96182, thus removing the limitation on supported types.

The second commit builds on that change to expose the Android runtime to developers by introducing a bundled AndroidRuntime plugin.
For example, the following logic can be used to check whether the device supports vibration:

var android_runtime = Engine.get_singleton("AndroidRuntime")
if android_runtime:
       print("Checking if the device supports vibration")
       var vibrator_service = android_runtime.getApplicationContext().getSystemService("vibrator")
       if vibrator_service:
               if vibrator_service.hasVibrator():
                       print("Vibration is supported on device!")
               else:
                       printerr("Vibration is not supported on device")
       else:
               printerr("Unable to retrieve the vibrator service")
else:
       printerr("Couldn't find AndroidRuntime singleton")

Those changes put together have significant impact for Godot developers on Android platforms as it removes the need for complex plugins in order to access Android libraries and SDKs.
So long as the library or SDK is written in Java or Kotlin, it only needs to be included in the build process via EditorExportPlugin#_get_android_libraries (...) or EditorExportPlugin#_get_android_dependencies(...) to be directly accessible to the script logic!

@m4gr3d m4gr3d added this to the 4.4 milestone Sep 26, 2024
@m4gr3d m4gr3d requested a review from dsnopek September 26, 2024 15:45
@m4gr3d m4gr3d force-pushed the update_godot_plugin_to_use_javaclasswrapper branch 2 times, most recently from 2e1dc18 to 2e46cf9 Compare September 26, 2024 18:00
@m4gr3d m4gr3d force-pushed the update_godot_plugin_to_use_javaclasswrapper branch 3 times, most recently from c4376dd to f1f7e73 Compare September 26, 2024 18:44
@m4gr3d m4gr3d requested review from akien-mga and reduz September 26, 2024 19:51
@m4gr3d m4gr3d marked this pull request as ready for review September 26, 2024 19:51
@m4gr3d m4gr3d requested a review from a team as a code owner September 26, 2024 19:51
@m4gr3d
Copy link
Contributor Author

m4gr3d commented Sep 26, 2024

cc @reduz @akien-mga This PR may drastically simplify access to Android SDKs for Godot developers!

Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

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

Looks good to me!

The Android plugin implementation is updated to use `JavaClassWrapper` which was fixed in godotengine#96182, thus removing the limitation on supported types.

Note that `JavaClassWrapper` has also been updated in order to only provide access to public methods and constructor to GDScript.
Thanks for the fix of `JavaClassWrapper` in godotengine#96182 and the changes in the previous commit, this introduces an `AndroidRuntime` plugin which provides GDScript access to the Android runtime capabilities.

This allows developers to get access to various Android capabilities without the need of a plugin.
For example, the following logic can be used to check whether the device supports vibration:

```
var android_runtime = Engine.get_singleton("AndroidRuntime")
 if android_runtime:
 	print("Checking if the device supports vibration")
 	var vibrator_service = android_runtime.getApplicationContext().getSystemService("vibrator")
 	if vibrator_service:
 		if vibrator_service.hasVibrator():
 			print("Vibration is supported on device!")
 		else:
 			printerr("Vibration is not supported on device")
 	else:
 		printerr("Unable to retrieve the vibrator service")
 else:
 	printerr("Couldn't find AndroidRuntime singleton")
```
@m4gr3d m4gr3d force-pushed the update_godot_plugin_to_use_javaclasswrapper branch from 739ed91 to 4587d14 Compare September 29, 2024 21:34
@akien-mga akien-mga merged commit 06a7cf7 into godotengine:master Oct 1, 2024
19 checks passed
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants