AdiAtAnd
AdiAtAnd

Reputation: 119

Android Studio JNI with C++ UnsatisfiedLinkError

I am using Android Studio 2.1.2 I am not using Experimental Plugin

Please Check the following files and Check the error I am getting.

I solved the issue. Edited file is as follows. They way I fixed it may not be the correct way as I am setting property to use deprecated way, but it works. Experimental plugin can still be unstable. I will try with experimental plugin soon.

build.gradle from Module

sourceSets.main {
        jniLibs.srcDir 'src/main/libs'
        /*jni.srcDirs = [] not using this, I commented this. Please check SO links which explained when to use this and when not to use this*/
    }

following 4 files are in jni folder of main

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := mylib
LOCAL_SRC_FILES := HelloJni.cpp

include $(BUILD_SHARED_LIBRARY)

Application.mk

APP_ABI := all

HelloJni.cpp

#include <jni.h>
#include <Header.h>

 JNIEXPORT jstring JNICALL Java_com_example_m1035325_ndksampleapp_MainActivity_getStringFromJni(JNIEnv *env,jobject thiz)
 {
 env-> NewStringUTF ( "Hellofrom JNI!");
 }

Header.h

#include <jni.h>;
using namespace std;

#ifndef HEADER
#define HEADER

extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_m1035325_ndksampleapp_MainActivity_getStringFromJni(JNIEnv *env, jobject obj);
}

#endif //NDKSAMPLEAPP_HEADER_H

MainActicity.java

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tvHello=(TextView)findViewById(R.id.tvHello);
        tvHello.setText(getStringFromJni());
    }

    public native String getStringFromJni();

Here when I hover on method getStringFromJni it shows Can't resolve corresponding JNI function

I have set NDK path in Project Structure and in Path environment variable too.

I am getting following error

Process: com.example.m1035325.ndksampleapp, PID: 12831
                                                                                   java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String com.example.m1035325.ndksampleapp.MainActivity.getStringFromJni() (tried Java_com_example_m1035325_ndksampleapp_MainActivity_getStringFromJni and Java_com_example_m1035325_ndksampleapp_MainActivity_getStringFromJni__)
                                                                                       at com.example.m1035325.ndksampleapp.MainActivity.getStringFromJni(Native Method)

I searched a lot on SO also but I am not getting what I missed?

No error now , above error is fixed. Please check my answer to this question.

Upvotes: 0

Views: 720

Answers (2)

AdiAtAnd
AdiAtAnd

Reputation: 119

One important change I made is in file gradle.properties, is as follows

android.useDeprecatedNdk=true

so its related to Android Studio version 2.1.2 , for this version experimental plugin is the default option to use and the approach I am using is deprecated. I will be trying with experimental plugin soon.

Upvotes: 0

michalsrb
michalsrb

Reputation: 4891

I think the problem is in your Android.mk file:

LOCAL_SOURCE_FILE := HelloJni.cpp

AFAIK the Android build system doesn't use variable of that name. It should be:

LOCAL_SRC_FILES := HelloJni.cpp

Upvotes: 1

Related Questions