这是我第一次做JNI,对Android Studio也不太了解,而且我使用的是Windows。我只是遵循helloworld jni教程,但我遇到了问题。也许这与NDK有关。此外,我在MainActivity.java文件中的本机函数hello()是红色的,所以我认为这是错误的。
MainActivity.java:
package com.example.user.ndkdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
textView.setText(hello());
setContentView(textView);
}
static{
System.loadLibrary("hello");
}
public native String hello();
}
com_example_user_ndkdemo_MainActivity.h:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#ifndef _Included_com_example_user_ndkdemo_MainActivity
#define _Included_com_example_user_ndkdemo_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#undef com_example_user_ndkdemo_MainActivity_BIND_ABOVE_CLIENT
#define com_example_user_ndkdemo_MainActivity_BIND_ABOVE_CLIENT 8L
#undef com_example_user_ndkdemo_MainActivity_BIND_ADJUST_WITH_ACTIVITY
#define com_example_user_ndkdemo_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L
#undef com_example_user_ndkdemo_MainActivity_BIND_ALLOW_OOM_MANAGEMENT
#define com_example_user_ndkdemo_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
#undef com_example_user_ndkdemo_MainActivity_BIND_AUTO_CREATE
#define com_example_user_ndkdemo_MainActivity_BIND_AUTO_CREATE 1L
#undef com_example_user_ndkdemo_MainActivity_BIND_DEBUG_UNBIND
#define com_example_user_ndkdemo_MainActivity_BIND_DEBUG_UNBIND 2L
#undef com_example_user_ndkdemo_MainActivity_BIND_IMPORTANT
#define com_example_user_ndkdemo_MainActivity_BIND_IMPORTANT 64L
#undef com_example_user_ndkdemo_MainActivity_BIND_NOT_FOREGROUND
#define com_example_user_ndkdemo_MainActivity_BIND_NOT_FOREGROUND 4L
#undef com_example_user_ndkdemo_MainActivity_BIND_WAIVE_PRIORITY
#define com_example_user_ndkdemo_MainActivity_BIND_WAIVE_PRIORITY 32L
#undef com_example_user_ndkdemo_MainActivity_CONTEXT_IGNORE_SECURITY
#define com_example_user_ndkdemo_MainActivity_CONTEXT_IGNORE_SECURITY 2L
#undef com_example_user_ndkdemo_MainActivity_CONTEXT_INCLUDE_CODE
#define com_example_user_ndkdemo_MainActivity_CONTEXT_INCLUDE_CODE 1L
#undef com_example_user_ndkdemo_MainActivity_CONTEXT_RESTRICTED
#define com_example_user_ndkdemo_MainActivity_CONTEXT_RESTRICTED 4L
#undef com_example_user_ndkdemo_MainActivity_MODE_APPEND
#define com_example_user_ndkdemo_MainActivity_MODE_APPEND 32768L
#undef com_example_user_ndkdemo_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING
#define com_example_user_ndkdemo_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L
#undef com_example_user_ndkdemo_MainActivity_MODE_MULTI_PROCESS
#define com_example_user_ndkdemo_MainActivity_MODE_MULTI_PROCESS 4L
#undef com_example_user_ndkdemo_MainActivity_MODE_PRIVATE
#define com_example_user_ndkdemo_MainActivity_MODE_PRIVATE 0L
#undef com_example_user_ndkdemo_MainActivity_MODE_WORLD_READABLE
#define com_example_user_ndkdemo_MainActivity_MODE_WORLD_READABLE 1L
#undef com_example_user_ndkdemo_MainActivity_MODE_WORLD_WRITEABLE
#define com_example_user_ndkdemo_MainActivity_MODE_WORLD_WRITEABLE 2L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_DIALER
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_DIALER 1L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_DISABLE
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_DISABLE 0L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SHORTCUT
#define com_example_user_ndkdemo_MainActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef com_example_user_ndkdemo_MainActivity_RESULT_CANCELED
#define com_example_user_ndkdemo_MainActivity_RESULT_CANCELED 0L
#undef com_example_user_ndkdemo_MainActivity_RESULT_FIRST_USER
#define com_example_user_ndkdemo_MainActivity_RESULT_FIRST_USER 1L
#undef com_example_user_ndkdemo_MainActivity_RESULT_OK
#define com_example_user_ndkdemo_MainActivity_RESULT_OK -1L
#undef com_example_user_ndkdemo_MainActivity_HONEYCOMB
#define com_example_user_ndkdemo_MainActivity_HONEYCOMB 11L
#undef com_example_user_ndkdemo_MainActivity_MSG_REALLY_STOPPED
#define com_example_user_ndkdemo_MainActivity_MSG_REALLY_STOPPED 1L
#undef com_example_user_ndkdemo_MainActivity_MSG_RESUME_PENDING
#define com_example_user_ndkdemo_MainActivity_MSG_RESUME_PENDING 2L
/*
* Class: com_example_user_ndkdemo_MainActivity
* Method: hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_user_ndkdemo_MainActivity_hello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
main.cpp:
#include <com_example_user_ndkdemo_MainActivity.h>
/*
* Class: com_example_user_ndkdemo_MainActivity
* Method: hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_user_ndkdemo_MainActivity_hello
(JNIEnv *env, jobject obj){
return (*env)->NewStringUTF(env, "hello from JNI");
}
Android.mk:
LOCAL_PATH := $(call my-dir)
include #(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_LDLIBS += -llog
LOCAL_SRC_FILES := main.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk:
APP_ABI:=all
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.example.user.ndkdemo"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
ndk{
moduleName ="hello"
}
}
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
jniLibs.srcDir 'src/main/libs'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
}
local.properties:
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Thu Nov 19 12:40:02 PST 2015
sdk.dir=C:\Users\user\AppData\Local\Android\sdk
ndk.dir=C:\Users\user\AppData\Local\Android\android-ndk-r10e
grandle.properties:
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.useDeprecatedNdk=true
因此,当我通过键入:"C:\Users\user\AppData\Local\Android\Android-ndk-r10e\ndk-build.cmd"运行ndk构建时,我在Android Studio终端窗口消息中看不到任何文本。所以我认为一切都正常,但当我在android手机上运行该项目时,手机上弹出消息,说"不幸的是,NDKDemo已经停止。"
请告诉我我做错了什么。非常感谢。
我可以提到的三件事是,如果对你有用的话,你可以用它们来检查。
1。以下是我在JNI.cpp文件中使用的语法,它对我有效
env-> NewStringUTF ( "Hellofrom JNI!");
2。我不认为
ndk{
moduleName ="hello"
}
在build.gradle文件中需要,正如您在Android.mk 中已经声明的那样
- 您的模块名称和JNI方法名称是相同的"你好",您应该避免这种差异