【发布时间】:2015-11-19 22:34:41
【问题描述】:
这是我第一次做 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
因此,当我通过键入以下内容运行 ndk 构建时:“C:\Users\user\AppData\Local\Android\android-ndk-r10e\ndk-build.cmd”之后,我在 Android Studio 终端中看不到任何文本窗口消息。所以我以为一切正常,但是当我在我的 android 手机上运行该项目时,我的手机上弹出消息说“不幸的是,NDKDemo 已停止。”我检查了 Android Studio Android windows 消息:它说“找不到”libshello.so”但我没有任何 .so 文件,因为我从头开始创建这个项目。
请让我知道我做错了什么。谢谢。
【问题讨论】:
标签: android android-ndk