1

I am trying to build native application to use it just from command line (adb shell). I have tried to build it using ndk-build (without creating project). Here is my code Application.mk

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

And I am getting next errors

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.

Is there any way to compile this without creating project, I don't need project at all, I am just want to write native application without GUI and using native code in Java applications. Thanks for help in advance.

braohaufngec
  • 31
  • 1
  • 1
  • 8

1 Answers1

1

Following are the minimum steps that took me to to accomplish what you want.
(I assume you already have downloaded and setup the Android SDK and NDK, and are building using ndk-build command from shell.)

1) Navigate to your preferred location and create the required folders:

$ mkdir -p ndk-test/jni

2) Set NDK_PROJECT_PATH environment variable:

$ export NDK_PROJECT_PATH=./ndk-test

3) Create 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) Navigate back to the original folder and build the project:

$ cd -

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

Now you should have test file under ndk-test/libs/armeabi/ folder.

5) Test:

$ 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)!

Voilà!


** I tested on Nexus 5 with Android 5.0 and thus needed those flags, you may not need them. Please see Running a native library on Android L. error: only position independent executables (PIE) are supported for further details.
Community
  • 1
  • 1
ozbek
  • 20,955
  • 5
  • 61
  • 84
  • 2
    One doesn't have to set the PIE options manually if you set APP_PLATFORM := android-16 (or higher) or APP_PIE := true in jni/Application.mk (although for this example, it would have required creating an extra file). – mstorsjo Nov 24 '14 at 07:43
  • @mstorsjo: yes, indeed that would do the trick. Thanks for the input. – ozbek Nov 24 '14 at 08:05
  • Thx !! Works ! I had to add variable ! – braohaufngec Nov 24 '14 at 22:40