Question

I'm trying to make a NDK application, but I get this error java.lang.UnsatisfiedLinkError: Native method not found: com.examplejni.MainActivity.sum:(II)I

I want to call a sum function written in c from my android application

I missing a step to do?

This is the c file (sum.c)

int sum(int n1,int n2)
{
    return n1+n2;
}

This is the Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := sum
LOCAL_SRC_FILES := sum.c
include $(BUILD_SHARED_LIBRARY)

This is my activity

package com.examplejni;

import com.examplejni.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity
{

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

    public static native int sum(int n1, int n2);

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int value1 = 20;
        int value2 = 30;

        int nativeOut;

        nativeOut = sum(value1, value2);

        TextView tv = (TextView) findViewById(R.id.text);
        tv.setText(nativeOut);
    }
}
Was it helpful?

Solution

You cannot access a native function directly. You must create a JNI function to call first.

See this tutorial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top