2

Android Studio, a project with an NDK library, using ndkBuild with Android.mk. My build uses a static library dependency, and the static library exists as a debug and as a release flavor, in separate directories. The makefile goes:

#Ref to libfoo
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := $(FOOPROJECT)\foo\build\intermediates\ndkBuild\debug\obj\local\$(TARGET_ARCH_ABI)\libfoo.a
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_SRC_FILES has the debug flavor hard-coded as a part of the path. Not good. I'd like to use either "debug" or "release" there, depending on the current build type.

Is the current build type available in the makefile as a variable? If not, is is possible to pass it to ndk-build via the gradle file?

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Just FYI, this is [how you can manage the different paths with CMake](https://stackoverflow.com/questions/53409479/android-ndk-path-variable-for-strip-command-in-cmake-build-tool-chain), but not ndk-build. – shizhen Feb 25 '19 at 01:24
  • 3
    Do you still want to build the rest of your native code in release mode? Otherwise there's the `APP_OPTIM` variable. – Michael Feb 25 '19 at 07:05
  • No, I want to keep the debug/release distinction both in this project and in the dependencies and make them match. Good find, let me play with that. – Seva Alekseyev Feb 25 '19 at 15:07
  • Bingo. Make an answer, I'll accept. For the record, `APP_OPTIM` is set to "debug" even if "JNI debuggable" is set to false on the debug type, I've checked. – Seva Alekseyev Feb 25 '19 at 17:02
  • "JNI debuggable" means your JNI code can be debugged, it does not mean anything to the "build type". – shizhen Feb 26 '19 at 03:06
  • My point was, one might reasonably expect `APP_OPTIM` to reflect the compiler flags being used as opposed to the specified build type. Meanwhile, if "JNI debuggable" is off, one might similarly expect the compiler flags to be identical to those of the release build. – Seva Alekseyev Feb 26 '19 at 15:16

2 Answers2

2

EDIT: Michael's APP_OPTIM is better. Once he writes it up, I'll accept. For now, I'll leave this here.


Couldn't find a built-in variable, did a Gradle trick:

buildTypes {
    release {
        externalNativeBuild {
            ndkBuild {
                arguments "BUILD_TYPE=release"
            }}
    }
    debug {
        externalNativeBuild {
            ndkBuild {
                arguments "BUILD_TYPE=debug"
            }}
    }
}

Then the line in Android.mk becomes:

LOCAL_SRC_FILES := $(FOOPROJECT)\foo\build\intermediates\ndkBuild\$(BUILD_TYPE)\obj\local\$(TARGET_ARCH_ABI)\libfoo.a
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
1

NDK_DEBUG is the var what you need, 1 is debug, 0 is release.

ifeq ($(NDK_DEBUG), 1)
    BUILD_TYPE := debug
else
    BUILD_TYPE := release
endif
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := $(FOOPROJECT)\foo\build\intermediates\ndkBuild\$(BUILD_TYPE)\obj\local\$(TARGET_ARCH_ABI)\libfoo.a
include $(PREBUILT_STATIC_LIBRARY)

Other useful vars:

  • APP_BUILD_SCRIPT: Android.mk path
  • NDK_APPLICATION_MK: Application.mk path
  • APP_ABI: Same like TARGET_ARCH_ABI
  • NDK_OUT: the build obj path
  • NDK_LIBS_OUT: build lib path
  • APP_PLATFORM: Target os and api version,like android-21

You can check this file : ProjectPath/ModuleName/build/.cxx/$(BUILD_TYPE)/xxxx/$(TARGET_ARCH_ABI)/android_gradle_build_command_ModuleName_$(TARGET_ARCH_ABI).txt too see more info.

And here is a Android NDK demo, you can check the lib_module_provider and lib_module_beneficiary to see how to use pre-built library by ndk-build or cmake

peerless2012
  • 179
  • 1
  • 7