1. 程式人生 > >android NDK如何解決Please define the NDK_PROJECT_PATH variable to point to it

android NDK如何解決Please define the NDK_PROJECT_PATH variable to point to it

從Google android官網下載最新的android ndk安裝包android-ndk-r15b,自己配置環境使用ndk-build編譯出現如下錯誤

$ ndk-build 
Android NDK: Could not find application project directory !    
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.    
/home/使用者路徑/android-ndk-r15b/build/core/build-local.mk:151: *** Android NDK: Aborting    .  Stop.

原來最新的NDK使用命令列編譯需要制定專案路徑,Android.mk,Application.mk檔案路徑

ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk

注意:NDK_PROJECT_PATH=.  後面是一個英文句號“.”,表示當前路徑

重新編譯就可以了

$ ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk


Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-14.    
[armeabi] Compile++ thumb: HelloJni <= com_demo_Hello.cpp
[armeabi] StaticLibrary  : libstdc++.a
[armeabi] SharedLibrary  : libHelloJni.so
[armeabi] Install        : libHelloJni.so => libs/armeabi/libHelloJni.so
[armeabi-v7a] Compile++ thumb: HelloJni <= com_demo_Hello.cpp
[armeabi-v7a] StaticLibrary  : libstdc++.a
[armeabi-v7a] SharedLibrary  : libHelloJni.so
[armeabi-v7a] Install        : libHelloJni.so => libs/armeabi-v7a/libHelloJni.so
[x86] Compile++      : HelloJni <= com_demo_Hello.cpp
[x86] StaticLibrary  : libstdc++.a
[x86] SharedLibrary  : libHelloJni.so
[x86] Install        : libHelloJni.so => libs/x86/libHelloJni.so

究其原因,谷歌的親兒子android studio現在高度集成了NDK,所以在編譯策略上有所改變,所幸改變不大。

Hello.java

package com.demo;

public class Hello {
    public native static void native_HelloWorld();
}

com_demo_Hello.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_demo_Hello */

#ifndef _Included_com_demo_Hello
#define _Included_com_demo_Hello
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_demo_Hello
 * Method:    native_HelloWorld
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_demo_Hello_native_1HelloWorld
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif
com_demo_Hello.cpp

#include "com_demo_Hello.h"

#include <android/log.h>

#define TAG "HelloJni"

/*
 * Class:     com_demo_Hello
 * Method:    native_HelloWorld
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_demo_Hello_native_1HelloWorld
  (JNIEnv * env, jclass clazz) {
    __android_log_print(ANDROID_LOG_VERBOSE, TAG, "Hello, World.");
}

Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := libHelloJni
LOCAL_C_INCLUDES := com_demo_Hello.h
LOCAL_SRC_FILES := com_demo_Hello.cpp

LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

Application.mk

 APP_ABI := armeabi armeabi-v7a x86

NDK谷歌編譯官網

https://developer.android.com/studio/projects/add-native-code.html#new-project