Android studio ndk-build.cmd没有运行

时间:2015-11-19 22:34:42

标签: android android-ndk

这是我第一次做JNI而且对Android Studio知之甚少,我正在使用Windows。我只是按照helloworld jni教程,但我遇到了问题。也许它与NDK有关。另外我在MainActivity.java文件中调用hello()的本机函数是红色的,所以我认为这是错误的。

MainActivity.java:

VACUUM

com_example_user_ndkdemo_MainActivity.h:

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();
}

main.cpp中:

/* 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

Android.mk:

#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");
}

Application.mk:

LOCAL_PATH := $(call my-dir)

include #(CLEAR_VARS)

LOCAL_MODULE := hello
LOCAL_LDLIBS += -llog
LOCAL_SRC_FILES := main.cpp

include $(BUILD_SHARED_LIBRARY)

的build.gradle:

APP_ABI:=all

local.properties:

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'
}

grandle.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

所以当我通过键入以下命令运行ndk构建时:&#34; C:\ Users \ user \ AppData \ Local \ Android \ android-ndk-r10e \ ndk-build.cmd&#34;我在Android Studio终端窗口消息中看不到任何文本。所以我认为一切正常,但是当我在我的Android手机上运行该项目时,手机上会弹出消息说“#34;不幸的是,NDKDemo已经停止了。”#34;我检查Android Studio Android窗口消息:它说&#34;无法找到&#34; libshel​​lo.so&#34;但我从头开始创建这个项目时没有任何.so文件。

请让我知道我做错了什么。谢谢。

1 个答案:

答案 0 :(得分:0)

我可以提及的3件事情,如果它对你有用,请与他们核对。

1。 以下语法我在JNI .cpp文件中,并为我工作。

env-> NewStringUTF ( "Hellofrom JNI!");

2。 我不认为

ndk{
  moduleName ="hello"
        }
您已在Android.mk

中声明了build.gradle文件中的

  1. 您的模块名称和JNI方法名称相同&#34;您好&#34;你应该避免这种差异。