3

It has been a while I posted a question to StackOverFlow. I am trying to describe what problem I have and what have I tried...as detail as possible since I voted minus for not providing detail information last time I posted a question. If there is any information that I lack of providing or you need in order to resolve the problem, please feel free to comment it below so that I can provide necessary information to solve this issue.

"The Problem I Have"

The native method is highlighted red and saying "Cannot resolve corresponding JNI function Java_com_example_~"

[Image is attached below]

When I run the app, it works perfectly.

The warning red sign is only showing in Windows OS, not in Mac OS.

I am using the latest stable version of Android Studio 2.3.

"What Have I Tried"

Some of the comments suggest to put externalNativeBuild { ...} in gradle since IDE is not picking up right.

externalNativeBuild {
    ndkBuild {
        path "src/main/jni/Android.mk"
    }
}

I tested in Mac OS, and warning sign disappeared, but it DOES NOT in Windows OS, which is the OS that I have to use in my company. I made sure I have the same source code, and I also imported project that I tested in Mac OS. STILL showing the warning sign.

I know some people suggested to just Simply Ignore JNI Function However, I do not want to simply ignore the warning sign because later I need to port the 3rd party project that already ported library and contain a lot of native methods that I need to see if each one of them links correctly.

HAS anyone ever FACED the same issue as I have and solved the issue?

Cannot resolve corresponding JNI function

[Source Code]

MainActivity

package com.example.sonic.jniexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    Button button;

    HelloNDK helloNDK = new HelloNDK();

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

        textView = (TextView)findViewById(R.id.textview);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(helloNDK.stringFromJNI());
            }
        });


    }
}

HelloNDK

package com.example.sonic.jniexample;

public class HelloNDK {

    static {
        System.loadLibrary("hello-jni");
    }

    public native String stringFromJNI();

}

Android.mk

LOCAL_PATH := $(call my-dir)                    

include $(CLEAR_VARS)                           

LOCAL_MODULE := hello-jni                           
LOCAL_CFLAGS += -std=c++14                      
LOCAL_SRC_FILES := hello-jni.cpp                   

include $(BUILD_SHARED_LIBRARY)   

Application.mk

APP_MODULES :=  hello-jni
APP_ABI := all

com_example_sonic_jniexample_HelloNDK.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_sonic_jniexample_HelloNDK */

#ifndef _Included_com_example_sonic_jniexample_HelloNDK
#define _Included_com_example_sonic_jniexample_HelloNDK
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_sonic_jniexample_HelloNDK
 * Method:    stringFromJNI
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_sonic_jniexample_HelloNDK_stringFromJNI
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

HelloNDK.cpp

#include <com_example_sonic_jniexample_HelloNDK.h>

JNIEXPORT jstring JNICALL
Java_com_example_sonic_jniexample_HelloNDK_stringFromJNI(JNIEnv *env,jobject obj) {
    jstring str = (*env).NewStringUTF("From JNI");
    return str;
}

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.0'
    defaultConfig {
        applicationId "com.example.sonic.jniexample"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        ndk {
            moduleName "hello-jni"
        }

        sourceSets.main {
            jni.srcDirs = [] // This prevents the auto generation of Android.mk
            jniLibs.srcDir 'src/main/libs'
        }

    }

    externalNativeBuild {
        ndkBuild {
            path "src/main/jni/Android.mk"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}
JayB Kim
  • 327
  • 5
  • 16

1 Answers1

1

The issue actually is the package of your HelloNDk, it is not same as defined inside .so files. Move the HelloNDk to correct package and it will resolve this.

For more details, refer to the link below: How to use the generated .so library in another Android Project?

Harmin
  • 11
  • 2