Error compiling a static zlib library for Android

0

I have the Android.mk configuration file for the zlib for Android build , but it contains the build options, , and host , I just need to compile a static library, BUILD_STATIC_LIBRARY .

I do not know what to keep or remove from Android.mk, how could I compile properly removing the unnecessary options?

Android.mk file:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

# measurements show that the ARM version of ZLib is about x1.17 faster
# than the thumb one...
LOCAL_ARM_MODE := arm

zlib_files := \
    src/adler32.c \
    src/compress.c \
    src/crc32.c \
    src/deflate.c \
    src/gzclose.c \
    src/gzlib.c \
    src/gzread.c \
    src/gzwrite.c \
    src/infback.c \
    src/inflate.c \
    src/inftrees.c \
    src/inffast.c \
    src/trees.c \
    src/uncompr.c \
    src/zutil.c

LOCAL_MODULE := libz
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS += -O3 -DUSE_MMAP

# TODO: This is to work around b/24465209. Remove after root cause is fixed
LOCAL_LDFLAGS_arm := -Wl,--hash-style=both

LOCAL_SRC_FILES := $(zlib_files)
ifneq ($(TARGET_BUILD_APPS),)
  LOCAL_SDK_VERSION := 9
else
  LOCAL_CXX_STL := none
endif
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_ARM_MODE := arm
LOCAL_MODULE := libz
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS += -O3 -DUSE_MMAP
LOCAL_SRC_FILES := $(zlib_files)
ifneq ($(TARGET_BUILD_APPS),)
  LOCAL_SDK_VERSION := 9
else
  LOCAL_CXX_STL := none
endif
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := libz
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS += -O3 -DUSE_MMAP
LOCAL_SRC_FILES := $(zlib_files)
LOCAL_MULTILIB := both
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
LOCAL_MODULE_HOST_OS := darwin linux windows
LOCAL_CXX_STL := none
include $(BUILD_HOST_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := libz-host
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS += -O3 -DUSE_MMAP
LOCAL_SRC_FILES := $(zlib_files)
LOCAL_MULTILIB := both
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
LOCAL_CXX_STL := none
include $(BUILD_HOST_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:=        \
    src/test/minigzip.c

LOCAL_MODULE:= gzip

LOCAL_SHARED_LIBRARIES := libz

LOCAL_CXX_STL := none

include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:=        \
    src/test/minigzip.c

LOCAL_MODULE:= minigzip

LOCAL_STATIC_LIBRARIES := libz

LOCAL_CXX_STL := none

include $(BUILD_HOST_EXECUTABLE)

$(TARGET_OUT_COMMON_GEN)/zlib_fingerprint : $(wildcard $(LOCAL_PATH)/src/*.[ch])
    printf '%s\n' $^ | LC_ALL=C sort | xargs cat | shasum -a 256 | \
        awk '{printf $$1}' > $@

When trying to compile with the command ndk-build I get the following error:

  

[root @ localhost platform_external_zlib-master] #   / run / media / root / linux / workspace / android-ndk-r11c / ndk-build   TARGET_PLATFORM = android-14 TARGET_ARCH_ABI = x86 NDK_PROJECT_PATH =.   Android NDK: Trying to define local module 'z' in   /run/media/root/Volume/platform_external_zlib-master/Android.mk.
  Android NDK: But this module was already defined by   /run/media/root/Volume/platform_external_zlib-master/Android.mk.
  /run/media/root/Volume/android-ndk-r11c/build/core/build-module.mk:34:   *** Android NDK: Aborting. . Stop.
[root @ localhost platform_external_zlib-master] #

    
asked by anonymous 27.06.2016 / 23:41

1 answer

1

Basically what you needed to do was clean the code. The reason for the displayed error is still unknown, but with the code below you can compile zlib according to parameters passed in ndk-build .

zlib / Android.mk

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := libz
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS += -O3 -DUSE_MMAP
LOCAL_SRC_FILES := $(zlib_files)
ifneq ($(TARGET_BUILD_APPS),)
  LOCAL_SDK_VERSION := 9
else
  LOCAL_CXX_STL := none
endif
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)

In order not to give an error due to the lack of the jni directory, the folder and file are created below.

zlib / jni / Application.mk with the following content:

NDK_TOOLCHAIN_VERSION := 4.9
APP_ABI := all
APP_PROJECT_PATH := $(shell pwd)
APP_BUILD_SCRIPT := $(APP_PROJECT_PATH)/Android.mk

Note: Change the value of NDK_TOOLCHAIN_VERSION according to the version supported by your GCC compiler on Android NDK. Most recent aversion uses the 4.9 value, for more information see the Application.mk documentation .

Now the zlib library can be compiled for multiple architectures according to the value passed in TARGET_ARCH_ABI by opening the console in the zlib folder with the right mouse button , then selecting < in> Open in Console and typing the command:

/run/media/root/linux/workspace/android-ndk-r11c/ndk-build TARGET_PLATFORM=android-14 TARGET_ARCH_ABI=x86 NDK_PROJECT_PATH=.

Remembering that /run/media/root/linux/workspace/ refers to the location of your Android NDK and TARGET_ARCH_ABI its architecture, in the above example x86 was used, check available layouts .

Additional information about the build can be found on the Android NDK site.

    
28.06.2016 / 16:00