2017-06-16 27 views
2

已經有一段時間我向StackOverFlow發佈了一個問題。 我想描述我有什麼問題,我試過什麼...儘可能詳細,因爲我投票減去上次沒有提供詳細信息我發佈了一個問題。如果有任何我缺乏提供的信息或您需要解決問題的信息,請隨時在下面發表評論,以便我可以提供必要的信息來解決此問題。無法解析僅在Windows操作系統中顯示的相應JNI函數

本地方法被高亮顯示的紅色和說 「我有問題」, 「無法解析對應JNI功能Java_com_example_〜」

[圖像如下附]

當我運行的應用程序,它完美的作品。

警告紅色標誌僅在Windows操作系統中顯示,不在Mac OS中顯示。

我正在使用Android Studio 2.3的最新穩定版本。

「我曾嘗試」

一些評論建議把externalNativeBuild {...}在gradle這個,因爲IDE不拿起正確的。

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

我在Mac OS中進行測試,並警告標誌消失了,但它能在Windows OS,這是我在我的公司使用的OS不。 我確定我擁有相同的源代碼,並且我還導入了在Mac OS中測試的項目。仍然顯示警告標誌。

我知道一些人建議只是Simply Ignore JNI Function但是,我不想簡單地忽略警告標誌,因爲後來我需要移植已經移植了庫的第三方項目,幷包含很多我需要查看的本機方法如果他們每個人都正確鏈接。

有沒有人曾經面對同樣的問題,我已經解決了這個問題?

Cannot resolve corresponding JNI function

[源代碼]

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) 

應用。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' 
} 
+1

仍在進行中的問題...我真的不知道爲什麼會這樣。 –

回答

相關問題