1

I'm trying to get my Android.mk file to optimize the binaries:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := fred
LOCAL_SRC_FILES := fred.c
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS = -O3
NDKDEBUG = 0

include $(BUILD_SHARED_LIBRARY)

I then save this, and run

$NDK/ndk-build -B V=1

I then run md5sum on the resulting library, and there is no difference between it and one built with

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := fred
LOCAL_SRC_FILES := fred.c
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

So, what am I doing wrong?

Output of the compiler seems to be:

/home/AStupidNoob/Documents/Android/android-ndk-r7b/toolchains/arm-linux-androideabi-
4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -MMD -MP -MF /home/AStupidNoob
/workspace/Fred/obj/local/armeabi/objs/fred/fred.o.d -fpic -ffunction-sections -funwind-
tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ 
-D__ARM_ARCH_5TE__  -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os 
-fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -I/home/AStupidNoob
/workspace/Fred/jni -DANDROID -O3 -Wa,--noexecstack -O2 -DNDEBUG -g -I/home/AStupidNoob
/Documents/Android/android-ndk-r7b/platforms/android-4/arch-arm/usr/include -c  
/home/AStupidNoob/workspace/Fred/jni/fred.c -o /home/AStupidNoob/workspace/Fred/obj
/local/armeabi/objs/fred/fred.o

I'm not sure if this is OK, but it seems that the problem is perhaps the 3 optimization flags, -Os -O2 -O3, but how do I fix this?

Thanks!

AStupidNoob
  • 1,980
  • 3
  • 23
  • 35

1 Answers1

2

Look into the APP_OPTIM variable. I set mine to release in my Application.mk file for optimized code. (I'm not sure if you can get the granularity you are looking for with various -O# options this way, but at least you get something.)

Turix
  • 4,470
  • 2
  • 19
  • 27
  • Thanks for the response, but it still doesnt work. `APP_ABI := all APP_OPTIM = release NDKDEBUG = 0` is my Application.mk file, the md5 does not change... – AStupidNoob Jul 03 '12 at 05:08
  • 1
    Weird. Notice that immediately after the -O3 in your compiler command there is "-O2 -DNDEBUG -g", which almost certainly overrides what you are attempting. – Turix Jul 03 '12 at 05:25
  • 1
    Just looked into the ndk-build script and what it calls. I suspect the problem in your case is that the two binaries you are comparing are *both* release builds (instead of both being debug builds). Look at `add-application.mk` in particular (in the build/core directory). The `-O2` bit that I pointed out above is added there. These are added to the command line after your LOCAL_C_FLAGS, so they will take precedence over any `-O#` you specify there. – Turix Jul 03 '12 at 05:31
  • Thanks alot! It seems strange that the build scripts would override any thing I specify, when it seems others do not have trouble with this. Another thing: The symbols are NOT being stripped (Checked with a hex editor). Hence these are debug builds still. Any ideas? Thanks though. – AStupidNoob Jul 03 '12 at 06:20
  • 1
    Yeah, that's what the comment and code indicated in `add-application.mk`. In both cases, `debug` and `release`, it leaves the `-g` flag in because, to quote the comment, "we generate symbol versions of the binaries that are later stripped when they are copied to the final project's libs/ directory." I wonder if that final stripping is not happening for you. – Turix Jul 03 '12 at 15:05
  • Hehe I guess I wasn't concentrating when I said that. JNI cannot find the functions for java if they don't have names. Also, looking at `add-application.mk`, it seems I can add flags with the `$(APP_CFLAGS)` variable in `Application.mk`, which considering I only have one module, thats more than enough. Thanks. – AStupidNoob Jul 04 '12 at 03:08