# Mercury NDK examples

**URL:** https://talk.remobjects.com/t/mercury-ndk-examples/25614
**Category:** Mercury
**Created:** [October 29, 2021, 7:34pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614 "2021-10-29T19:34:54Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [October 29, 2021, 7:34pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/1 "2021-10-29T19:34:54Z")

</div>

Will the Android NDK article be updated for use with Mercury? I’m assuming the Declare statement will be used as normal for obtaining the function from the library.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 29, 2021, 7:48pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/2 "2021-10-29T19:48:08Z")

</div>

I’ll look at getting that updated asap, first thing next week. i assume you mean [Creating an Android NDK Extension](https://docs.elementscompiler.com/Tutorials/Platforms/AndroidNDKExtension/)?

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 29, 2021, 7:49pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/3 "2021-10-29T19:49:33Z")

</div>

If so, all the code shown there _is_ in the shipping Mercurty template:

```auto
Imports rtl

' ToDo: Update the ClassName and function name below, to match your Android app

<JNIExport(ClassName := "org.me.androidapp.MainActivity")>
Public Function HelloFromNDK(env as Ptr(Of JNIEnv), this as jobject) as jstring
  Return env.Dereference.Dereference.NewStringUTF(env, "Hello from NDK!")
End Function

```

but i’ll get the topic updated now.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 29, 2021, 7:57pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/4 "2021-10-29T19:57:19Z")

</div>

Note that’s for the NDK side. on the SDK side we have a problem with for now, as VB relied on DllImport to detect native functions, and doesn’t have a keyword like C# or Oxygene, but JNI doesnt use DllImport of course.

I’ll have to log an issue to get that fixed/supported;

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [October 29, 2021, 7:59pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/5 "2021-10-29T19:59:13Z")

</div>

Logged as [bugs://E25477](https://www.remobjects.com/portal/bugs/?id=E25477).

---

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [October 29, 2021, 8:05pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/6 "2021-10-29T20:05:52Z")

</div>

Yes, that was the example I was thinking of.

---

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [October 29, 2021, 10:40pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/7 "2021-10-29T22:40:21Z")

</div>

If I’m not mistaken, the JNI\* attributes handles the name mangling. Shouldn’t you be able to access it by using Declare Sub/Function Lib “libname.so” with the JNIImport attribute applied to the definition? I haven’t looked at the IL code in .NET to see exactly what this statement does so it’s just a guess.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 30, 2021, 2:15pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/8 "2021-10-30T14:15:02Z")

</div>

> [@sollapse](#):
>
> If I’m not mistaken, the JNI\* attributes handles the name mangling. Shouldn’t you be able to access it by using Declare Sub/Function Lib “libname.so” with the JNIImport attribute applied to the definition? I haven’t looked at the IL code in .NET to see exactly what this statement does so it’s just a guess.

Worth a try. I’m not sure, as I don’t do much Android myself, but IIRC there’s no attribute used on the managed (Java) side, for JNI, only the “extern(al)”/“native” keyword. Which VB/Mercury right now doesn’t have, so the compiler has no way to distinguish an empty function from an external one.

But i’ll have that looked at and (if necessary) fixed, Monday.

**Update** : there’s no JNIImport attribute AFAICT; we only have [JNIExport](https://docs.elementscompiler.com/API/Aspects/JNIExport/), on the unmanaged/native side.

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [November 1, 2021, 8:10pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/9 "2021-11-01T20:10:27Z")

</div>

[bugs://E25477](https://www.remobjects.com/portal/bugs/?id=E25477) was closed as fixed.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [November 1, 2021, 8:14pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/10 "2021-11-01T20:14:12Z")

</div>

Documentation also updated. vNext will support

```auto
<External>
Public Function HelloFromNDK() As String
End Function

```

---

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [November 1, 2021, 8:36pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/11 "2021-11-01T20:36:32Z")

</div>

Thanks for the addition. Now out of curiosity, will the Declare Lib statement be used normally for dynamically loading code from external libraries/object files at runtime?

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [November 1, 2021, 10:05pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/12 "2021-11-01T22:05:27Z")

</div>

> [@sollapse](#):
>
> Now out of curiosity, will the Declare Lib statement be used normally for dynamically loading code from external libraries/object files at runtime?

I’m not familiar with that, I’ll have to check with my colleague, tomorrow. But it looks like that don’t be needed here, just declare the function you want to import using `<External>`, leave the body empty, and the JNI runtime does the rest.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [November 2, 2021, 12:04pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/13 "2021-11-02T12:04:55Z")

</div>

Actually, id i use this on Java i get

```auto
  Declare Sub Foo Lib "Name" // E28 Unknown type "System.Runtime.InteropServices.DllImportAttribute"

```

so i assume this syntax is supported by Mercury, but not currently for Java (as the Attribute is doesn’t exist there). I suppose we could support a similar variant (optionally minus the “`Lib <name>`”) on Java.

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [November 2, 2021, 12:07pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/14 "2021-11-02T12:07:00Z")

</div>

Logged as [bugs://E25482](https://www.remobjects.com/portal/bugs/?id=E25482).

---

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [November 2, 2021, 12:20pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/15 "2021-11-02T12:20:34Z")

</div>

Very cool, the attribute option is good to have in general, yet the Declare statement has always historically been used for external declarations in VB. Lib also loads the object file in which the function resides, maybe this can be altered to support the Java/Island platforms?

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [November 2, 2021, 3:49pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/16 "2021-11-02T15:49:01Z")

</div>

> [@sollapse](#):
>
> Lib also loads the object file in which the function resides, maybe this can be altered to support the Java/Island platforms?

I’m not sure if JNI supports that. Since it doesn’t _need_ it, `Lib` will either be optional (if we _can_ provide a name), or not supported.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [November 4, 2021, 3:31pm UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/17 "2021-11-04T15:31:56Z")

</div>

`Declare` will be supported on Java in tomorrow’s build.

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [November 9, 2021, 9:56am UTC](https://talk.remobjects.com/t/mercury-ndk-examples/25614/18 "2021-11-09T09:56:36Z")

</div>

[bugs://E25482](https://www.remobjects.com/portal/bugs/?id=E25482) was closed as fixed.
