Domanda

Is it possible to use a native C/C++ library with dot42? If so are there examples of how to do this? If not is this a planned feature?

È stato utile?

Soluzione

Given the JNI implementation a native method such as the following:

#include <string.h>
#include <jni.h>

jstring Java_dllImportTest_MainActivity_Foo( JNIEnv* env, jobject thiz)
{
  return (*env)->NewStringUTF(env, "Hello from dot42 JNI !");
}

The JNI tooling will compile this to a native library and put this in e.g. libs\armeabi.

Copy the libs folder to the root of your dot42 project and include the .so in your project. Next set the build action to NativeCodeLibrary:

enter image description here

Next declare the native method in your C# code using the DllImport attribute and call it like this:

using System;
using System.Runtime.InteropServices;
using Android.App;
using Android.Os;
using Android.Widget;
using Dot42;
using Dot42.Manifest;

[assembly: Application("DllImportTest")]

namespace DllImportTest
{
    [Activity(Label ="DllImport MainActivity")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstance)
        {
            base.OnCreate(savedInstance);
            SetContentView(R.Layouts.MainActivityLayout);

            var textView = FindViewById<TextView>(R.Ids.label);
            textView.Text = Foo();
        }

        [DllImport("dllImportTest")]
        public static extern string Foo();
    }
}

The loadLibrary call is generated by the dot42 compiler.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top