Some questions about native extensions

Is it possible to build a native extension that fetch local songs and a mediaplayer in android?

Yes, I don’t see why that shouldn’t be possible. If you can do it in a normal Java based Android app, then you should also be able to do it using a native extension.

Thanks britzl.

Also, I want to ask whether there is tutorial for me to follow.
It would be great if it is steps by steps.
I want to know more about the flow of making native extensions for android.

Have you read the native extension manual? https://www.defold.com/manuals/extensions/

I have read the manual but it is not enough for me :sob: .

So, in my case, what I need is coding my own java first, and then write a extension.cpp to link them together?

Yes.
Here is an example to get you going: extension-android

Thanks Mathias.

I have written a small one to test, but it comes out with “attempt to index global ‘mediaplayer’ (a nil value)” when I build and run.

Does I miss something important?

You mean when you build and run on desktop? Or on device? It’s likely that there’s an ifdef making the extension only available on Android. Like this: extension-android/androidnative/src/extension.cpp at master · defold/extension-android · GitHub

I use dmengine to test.

What does your code look like?
It’s difficult to help unless we get some more info.

It is almost the same with the example, only few editing.

When you say dmengine, do you mean you selected “Build and Run” from the menu?
Or do you mean the dmengine.apk on your Android?

If you chose “Build And Run”, you are running on either OSX/Windows/Linux, and none of those have DM_PLATFORM_ANDROID defined (Just as Björn mentioned above). See the code he linked

dmengine.apk on my android.

And, did you bundle it yourself?
Or, did you download the vanilla engine from dmengine.apk?

If you downloaded the .apk file from the download page, beware it’s the vanilla engine. It is uploaded every two weeks, with each release. It will not contain your extension.

You have to bundle your app for Android, and use that .apk instead.

3 Likes