【问题标题】:Build NDK sources without creating project在不创建项目的情况下构建 NDK 源
【发布时间】:2015-01-21 10:51:39
【问题描述】:

我正在尝试构建本机应用程序以仅从命令行(adb shell)使用它。我尝试使用 ndk-build 构建它(不创建项目)。这是我的代码 Application.mk

APP_ABI := all

应用程序.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 应用程序中使用本机代码。 提前感谢您的帮助。

【问题讨论】:

    标签: android c android-ndk


    【解决方案1】:

    以下是我完成您想要的最少步骤。
    (我假设您已经下载并设置了 Android SDK 和 NDK,并且正在使用 shell 中的ndk-build 命令进行构建。)

    1) 导航到您的首选位置并创建所需的文件夹:

    $ mkdir -p ndk-test/jni
    

    2)设置NDK_PROJECT_PATH环境变量:

    $ export NDK_PROJECT_PATH=./ndk-test
    

    3) 创建Android.mk andmain.cppfiles underndk-test/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
    shell@hammerhead:/ $ cd /data/local/tmp
    shell@hammerhead:/data/local/tmp $ ./test
    Hello, world (i=3)!
    

    瞧!


    ** 我在带有 Android 5.0 的 Nexus 5 上进行了测试,因此需要这些标志,您可能不需要它们。请参阅Running a native library on Android L. error: only position independent executables (PIE) are supported 了解更多详情。

    【讨论】:

    • 如果您在 jni/Application.mk 中设置 APP_PLATFORM := android-16(或更高版本)或 APP_PIE := true (尽管对于此示例,它需要创建一个额外的文件)。
    • @mstorsjo:是的,确实可以。感谢您的意见。
    • 谢谢!!工作!我必须添加变量!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2010-10-04
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多