简体   繁体   English

带有C ++的Android Studio JNI UnsatisfiedLinkError

[英]Android Studio JNI with C++ UnsatisfiedLinkError

I am using Android Studio 2.1.2 I am not using Experimental Plugin 我正在使用Android Studio 2.1.2,而不是在使用实验性插件

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 来自Module的build.gradle

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 以下4个文件位于main的jni文件夹中

Android.mk Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := mylib
LOCAL_SRC_FILES := HelloJni.cpp

include $(BUILD_SHARED_LIBRARY)

Application.mk Application.mk

APP_ABI := all

HelloJni.cpp 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 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 在这里,当我将鼠标悬停在方法getStringFromJni上时,它显示无法解析相应的JNI函数

I have set NDK path in Project Structure and in Path environment variable too. 我也在项目结构和Path环境变量中设置了NDK路径。

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? 我也在SO上搜索了很多,但是我没有得到我错过的东西?

No error now , above error is fixed. 现在没有错误,以上错误已修复。 Please check my answer to this question. 请检查我对这个问题的回答。

I think the problem is in your Android.mk file: 我认为问题出在您的Android.mk文件中:

LOCAL_SOURCE_FILE := HelloJni.cpp

AFAIK the Android build system doesn't use variable of that name. AFAIK Android构建系统未使用该名称的变量。 It should be: 它应该是:

LOCAL_SRC_FILES := HelloJni.cpp

One important change I made is in file gradle.properties, is as follows 我所做的一项重要更改是在文件gradle.properties中,如下所示

android.useDeprecatedNdk=true 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. 因此它与Android Studio版本2.1.2有关,对于该版本,实验性插件是默认使用的选项,并且不赞成使用我使用的方法。 I will be trying with experimental plugin soon. 我将很快尝试使用实验性插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM