2014-11-23 118 views
0

我正嘗試構建本地應用程序以僅從命令行(adb shell)使用它。我試圖使用ndk-build(不創建項目)來構建它。這裏是我的代碼Application.mk在不創建項目的情況下構建NDK源碼

APP_ABI := all 

Application.mk

LOCAL_PATH := $(call my-dir) 

include $(CLEAR_VARS) 

LOCAL_MODULE := test.and 
LOCAL_SRC_FILES := main.cpp 
LOCAL_CPPFLAGS := -std=gnu++0x -Wall   # whatever g++ flags you like 
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog # whatever ld flags you like 

include $(BUILD_EXECUTABLE) # <-- Use this to build an executable. 

的main.cpp

#include <stdio.h>//for printf 
#include <stdlib.h>//for exit 

int main(int argc, char **argv) 
{ 
     int i = 1; 
     i+=2; 

     printf("Hello, world (i=%d)!\n", i); 

     return 0; 
     exit(0); 
} 

而且我得到下一個錯誤

Android NDK: Could not find application project directory !  
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.  
/home/crosp/.AndroidStudioBeta/android-ndk-cr/build/core/build-local.mk:130: *** Android NDK: Aborting . Stop. 

有什麼無需創建項目即可編譯此方法,我根本不需要項目,我只想編寫沒有GUI的本機應用程序,並在Java應用程序中使用本機代碼。 感謝您的幫助。

回答

1

以下是使我完成所需任務的最低步驟。
(我假設你已經下載並安裝了Android SDK和NDK,並使用ndk-build命令從外殼正在建設。)

1)瀏覽到您的首選位置,並創建所需的文件夾:

$ mkdir -p ndk-test/jni 

2)設置NDK_PROJECT_PATH環境變量:

$ export NDK_PROJECT_PATH=./ndk-test 

3)創建Android.mk and的main.cpp files under NDK測試/ JNI /`:

$ cd ndk-test/jni/ 
$ vi Android.mk 
LOCAL_PATH:= $(call my-dir) 
include $(CLEAR_VARS) 

# see note at ** for following flags 
LOCAL_CFLAGS += -fPIE 
LOCAL_LDFLAGS += -fPIE -pie 

LOCAL_MODULE := test 
LOCAL_SRC_FILES := main.cpp 

include $(BUILD_EXECUTABLE) 

:wq 


$ vi main.cpp 
#include <stdio.h>//for printf 
#include <stdlib.h>//for exit 

int main(int argc, char **argv) 
{ 
    int i = 1; 
    i += 2; 

    printf("Hello, world (i=%d)!\n", i); 

    return 0; 
    exit(0); 
} 

:wq 

4)導航回原來的文件夾,並生成項目:

$ cd - 

$ ndk-build 
[armeabi] Compile++ thumb: test <= main.cpp 
[armeabi] StaticLibrary : libstdc++.a 
[armeabi] Executable  : test 
[armeabi] Install  : test => libs/armeabi/test 

現在你應該有ndk-test/libs/armeabi/文件夾下的文件test

5)測試:

$ adb push ndk-test/libs/armeabi/test /data/local/tmp/ 
$ adb shell 
[email protected]:/ $ cd /data/local/tmp 
[email protected]:/data/local/tmp $ ./test 
Hello, world (i=3)! 

瞧!


** 我在Nexus 5上用Android 5.0進行測試,因此需要這些標誌,您可能不需要它們。詳情請參閱Running a native library on Android L. error: only position independent executables (PIE) are supported

+2

如果您在jni/Application.mk中設置APP_PLATFORM:= android-16(或更高版本)或APP_PIE:= true,則不需要手動設置PIE選項(儘管對於此示例,一個額外的文件)。 – mstorsjo 2014-11-24 07:43:25

+0

@mstorsjo:是的,的確,這可以做到這一點。感謝您的意見。 – ozbek 2014-11-24 08:05:04

+0

Thx !!作品!我不得不添加變量! – braohaufngec 2014-11-24 22:40:44

相關問題