Access C ++ code in Apache Cordova for Android

I am developing an application for installation on Windows 10 and Android using Apache Cordova in Visual Studio 2015. The backend logic will be contained in a C ++ project, which we hope will be compiled into .so or .dll depending on the platform.

I did some research on Cordoba plugins for Android, but there were no good lessons. What exactly do I need to do (both on C ++ sites and on javascript) to connect the two base codes?

+6
source share
2 answers

After further research and many dead ends, I was able to get my C ++ code to work on Cordova. I am answering here to document some of my problems and provide some good resources for others trying to do the same.

Cordova plugin development

A good tutorial that explains each part of the plugin better than the official documentation: https://blogs.oracle.com/mobile/entry/introduction_to_custom_cordova_plugin

My biggest problem with this part of the development was that removing the Cordova plugin in VS 2015 does crappy work and sometimes searches for files in the wrong places. Be careful with this if you need to modify the XML file of your plugin.

Communication with C ++

VS, .so ( → android). , ARM, x86 . .so /libs/armeabi, .java loadLibrary().

.java :

package com.example.plugin.cpp;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;

import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.PluginResult;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

public class Cpp extends CordovaPlugin {

    public static final String ACTION_SEVEN = "seven"; 

    public Cpp() { }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {

        if (ACTION_SEVEN.equals(action)) {
            alert("Message from Java", "Grabbed the number " + get7() + " from C++", "dismiss", callbackContext);
            callbackContext.success();
            return true;
        }
        return false;
    }

    private synchronized void alert(final String title, 
                              final String message, 
                              final String buttonLabel, 
                              final CallbackContext callbackContext) {
        new AlertDialog.Builder(cordova.getActivity())
            .setTitle(title)
            .setMessage(message)
            .setCancelable(false)
            .setNeutralButton(buttonLabel, new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int which) {
                    dialogInterface.dismiss();
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
                }
            })
            .create()
            .show();
    }

    public native int get7();

    static {
        System.loadLibrary("CppLibrary");
    }
}

C++:

CppLibrary.cpp:

#include "CppLibrary.h"

extern "C" {

    JNIEXPORT int JNICALL Java_org_example_plugin_cpp_Cpp_get7(JNIEnv* env, jobject thiz)
    {
        return 7;
    }

}

Cpp.h:

#pragma once

extern "C" {
    JNIEXPORT int JNICALL Java_org_example_plugin_cpp_Cpp_get7(JNIEnv* env, jobject thiz);
}

C-:

http://developer.android.com/ndk/samples/sample_hellojni.html#ci

http://developer.android.com/training/articles/perf-jni.html#faq_ULE

+7

++ API- RESTful? Cordova , REST.

, ++ , Cordova, ++ . , Cordova: http://cordova.apache.org/docs/en/latest/guide/hybrid/plugins/index.html

0

Source: https://habr.com/ru/post/1629048/


All Articles