有段时间我向stackoverflow发了一个问题。
我试图描述我有什么问题,我尝试了什么…尽可能详细,因为我投了反对票,不提供详细信息,我上次张贴了一个问题。如果有任何信息,我缺乏或您需要为解决问题,请随时评论它下面,以便我可以提供必要的信息,以解决这个问题。
“我的问题”
本机方法以红色突出显示,并表示“无法解析相应的JNI函数java_com_example”
[图片附在下面]
当我运行这个应用程序时,它工作得很好。
红色警告标志只在Windows操作系统中显示,而在Mac操作系统中不显示。
我正在使用最新的稳定版本的android studio 2.3。
“我试过什么”
一些评论建议将externalnativebuild{…}放在gradle中,因为ide没有正确地选择。
externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
我在mac操作系统中进行了测试,警告标志消失了,但在windows操作系统中没有,而windows操作系统是我公司必须使用的操作系统。
我确保我有相同的源代码,我还导入了我在mac os中测试的项目。仍然显示警告标志。
我知道有些人建议直接Simply Ignore JNI Function但是,我不想简单地忽略警告标志,因为稍后我需要移植已经移植了库的第三方项目,并且包含许多本地方法,我需要查看它们中的每一个是否正确链接。
有没有人遇到过和我一样的问题并解决了这个问题?
[源代码]
主要活动
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());
}
});
}
}
海伦德
package com.example.sonic.jniexample;
public class HelloNDK {
static {
System.loadLibrary("hello-jni");
}
public native String stringFromJNI();
}
安卓
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
例如Sonic
/* 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
海伦
#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;
}
平地
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'
}
最佳答案
问题实际上是HellondK的包,它与.so文件中定义的不一样。将hellondk移动到正确的包,它将解决此问题。
有关详细信息,请参阅下面的链接:
How to use the generated .so library in another Android Project?
关于android - 无法解析仅在Windows OS中显示的相应JNI函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44579390/