Search code examples
android-studioandroid-ndklibxml2static-linkinggradle-experimental

Building libxml2 into NDK with Android Studio and Gradle Experimental


My Android project has been going for a couple of years using Eclipse and ADT. The project makes use of 3 precompiled static libraries (curl, ssl, and crypto), and then compiles and statically links libxml2 in. The relevant lines from Android.mk are:

LOCAL_MODULE    := my_shim
LOCAL_SRC_FILES := $(LOCAL_FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_CFLAGS := -DCURL_DISABLE_TYPECHECK
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -lz
LOCAL_SHARED_LIBRARIES :=
LOCAL_STATIC_LIBRARIES += xml2 curl ssl crypto
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../c_module
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../libxml2/include

include $(BUILD_SHARED_LIBRARY)

include $(APP_LOCAL_PATH)/../../libxml2/Android.mk

I'm now switching to Android Studio 2 using Gradle Experimental, but I'm having problems getting the Gradle configuration right. I think I've managed to sort out the dependencies on log and z, and the statically compiled curl, ssl, and crypto libraries, but I can't work out how to tell it to build and then statically link the libxml2 module.

Any clues? This is what I have so far:

model {
    android {
        ...
    }

    android.ndk {
        moduleName "my_shim"
        platformVersion 19
        abiFilters.addAll(["armeabi", "x86"])
        CFlags.add("-DCURL_DISABLE_TYPECHECK")
        ldLibs.addAll(["log", "z"])
        stl "stlport_static"
    }

    android.sources {
        main {
            jni {
                dependencies {
                    library "crypto" linkage "static"
                    library "curl" linkage "static"
                    library "ssl" linkage "static"
                    library "xml2" linkage "static"
                }
            }
        }
    }

    repositories {
        libs(PrebuiltLibraries) {
            crypto {
                binaries.withType(StaticLibraryBinary) {
                    def cryptoLibPath = "src/main/jni/includes/${targetPlatform.getName()}/libcrypto.a"
                    staticLibraryFile = file("${cryptoLibPath}")
                }
            }
        }
        libs(PrebuiltLibraries) {
            curl {
                binaries.withType(StaticLibraryBinary) {
                    def curlLibPath = "src/main/jni/includes/${targetPlatform.getName()}/libcurl.a"
                    staticLibraryFile = file("${curlLibPath}")
                }
            }
        }
        libs(PrebuiltLibraries) {
            ssl {
                binaries.withType(StaticLibraryBinary) {
                    def sslLibPath = "src/main/jni/includes/${targetPlatform.getName()}/libssl.a"
                    staticLibraryFile = file("${sslLibPath}")
                }
            }
        }
    }
}

Solution

  • There're two ways, I think.

    1. Prebuild libxml2 manually and put it with other prebuilt libraries.

    2. Make a dependency (and separate project for xml2 library) It's described here