【发布时间】:2017-11-18 15:34:57
【问题描述】:
我已经有一段时间向 StackOverFlow 发布了一个问题。 我试图描述我有什么问题以及我尝试了什么……尽可能详细,因为我上次发布问题时因为没有提供详细信息而投了减号。如果有任何我没有提供的信息或您需要解决问题的任何信息,请随时在下面发表评论,以便我提供必要的信息来解决此问题。
“我的问题”
native方法用红色高亮显示“无法解析对应的JNI函数Java_com_example_~”
[图片附在下面]
当我运行该应用程序时,它运行良好。
警告红色标志仅在 Windows 操作系统中显示,在 Mac 操作系统中不显示。
我使用的是最新的稳定版 Android Studio 2.3。
“我尝试了什么”
一些 cmets 建议将 externalNativeBuild { ...} 放在 gradle 中,因为 IDE 无法正常使用。
externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
我在 Mac OS 中进行了测试,警告标志消失了,但它不在 Windows 操作系统中,这是我必须在我的公司中使用的操作系统。 我确保我有相同的源代码,并且我还导入了我在 Mac OS 中测试过的项目。仍然显示警告标志。
我知道有些人建议只使用Simply Ignore JNI Function 但是,我不想简单地忽略警告标志,因为以后我需要移植已经移植库并包含许多我需要的本机方法的第 3 方项目看看他们每个人是否正确链接。
有没有人遇到过和我一样的问题并解决了这个问题?
[源代码]
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'
}
【问题讨论】:
-
仍然存在问题...我真的不知道为什么会发生这种情况。
标签: android android-ndk java-native-interface native