Android Gradle NDK: Linking prebuilt static library

5,198 views
Skip to first unread message

Nimrod Dayan

unread,
Jul 13, 2015, 9:53:29 PM7/13/15
to adt...@googlegroups.com

In traditional NDK make file, I can link a prebuilt static library and specify its headers directory:
Android.mk

include $(CLEAR_VARS)
LOCAL_MODULE            := somelibrary-prebuilt-static
LOCAL_SRC_FILES         := $(TARGET_ARCH_ABI)/libsomelibrary.a
LOCAL_EXPORT_C_INCLUDES := include 
include $(PREBUILT_STATIC_LIBRARY)

How do I do that in build.gradle using the new plugin's DSL?

Streets Of Boston

unread,
Jul 14, 2015, 6:24:15 PM7/14/15
to adt...@googlegroups.com
From here: http://tools.android.com/tech-docs/new-build-system/gradle-experimental
"No support for creating and depending on static libraries"

Emanuele Zattin

unread,
Aug 7, 2015, 12:00:31 PM8/7/15
to adt-dev
Is this going to be supported eventually?

Iliya

unread,
Aug 25, 2015, 4:40:10 PM8/25/15
to adt-dev
I second that. This is the last thing that is keeping us from using Android Studio and Eclipse is starting to get on my nerves more and more with all the new features available in AS :)

Johan Euphrosine

unread,
Aug 28, 2015, 2:35:53 PM8/28/15
to adt-dev
You can link to a prebuild static library with the `ldFlags` option introduced in gradle-experimental:0.2.0.

I just published a sample to show how it works (with the google play services library: that ship as a set of .a for each platform):

Let me know if that helps.

--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Iliya

unread,
Sep 1, 2015, 6:57:06 PM9/1/15
to adt-dev
Still seem to be getting the same undefined reference errors as before even with the flags set:

def libPath = "src/main/jni/src/cocos2d/cocos2dx/platform/third_party/android/prebuilt/"
create("arm7") {
    ndk.abiFilters += "armeabi-v7a"
    ndk.ldFlags += "-L${file(libPath+"libcurl/libs/armeabi-v7a")}".toString()
    ndk.ldFlags += "-L${file(libPath+"libjpeg/libs/armeabi-v7a")}".toString()
    ndk.ldFlags += "-L${file(libPath+"libpng/libs/armeabi-v7a")}".toString()
    ndk.ldFlags += "-L${file(libPath+"libtiff/libs/armeabi-v7a")}".toString()
    ndk.ldFlags += "-L${file(libPath+"libwebp/libs/armeabi-v7a")}".toString()
}

Error:(66) undefined reference to 'png_get_io_ptr'
Error:(75) undefined reference to 'png_error'



also tried to specify the full path to the files directly with the same error:
create("arm7") {
    ndk.abiFilters += "armeabi-v7a"
    ndk.ldFlags += "-L${file(libPath+"libcurl/libs/armeabi-v7a/libcurl.a")}".toString()
    ndk.ldFlags += "-L${file(libPath+"libjpeg/libs/armeabi-v7a/libjpeg.a")}".toString()
    ndk.ldFlags += "-L${file(libPath+"libpng/libs/armeabi-v7a/libpng.a")}".toString()
    ndk.ldFlags += "-L${file(libPath+"libtiff/libs/armeabi-v7a/libtiff.a")}".toString()
    ndk.ldFlags += "-L${file(libPath+"libwebp/libs/armeabi-v7a/libwebp.a")}".toString()

Johan Euphrosine

unread,
Sep 1, 2015, 6:59:53 PM9/1/15
to adt-dev

Iliya

unread,
Sep 2, 2015, 7:57:59 PM9/2/15
to adt-dev
tried this using the same lib names that are used in the makefiles for jenkins and getting a different error now:
Error:error: cannot find -llibpng_static
...

Will try some more playing around with the configs some more

Johan Euphrosine

unread,
Sep 15, 2015, 5:51:23 PM9/15/15
to adt-dev
Hi Iliya, did you end up solving your issue?

As an alternative we could move the question to http://stackoverflow.com/questions/tagged/android-ndk that way it's easier for other to find the answer.

Iliya

unread,
Sep 18, 2015, 2:32:52 PM9/18/15
to adt-dev
Yeah thanks! After some tinkering got it all set up and working. compiling, running, debugging all seem good :)

tresalmasenmimente1

unread,
Sep 27, 2015, 1:46:05 AM9/27/15
to adt-dev
Hey hows it going Iliya?  I am having the same issues linking the static libraries with this new plugins.  I am using the ldFlags and ldLibs but I cannot figure it out.  If you could show me how you did it would be much appreciated, I really need to figure it out.

Thank you from NMSU,
Slowhand

Iliya

unread,
Sep 28, 2015, 11:39:56 AM9/28/15
to adt-dev
Sure here is the snippet from the gradle.build file that I am using:
    android.ndk {
        moduleName = "TestProject"

        // Generic stuff
        cppFlags += "-O2"
        cppFlags += "-std=gnu++11"
        cppFlags += "-frtti"
        cppFlags += "-fexceptions"
        cppFlags += "-fpermissive"
        cppFlags += "-DANDROID"
        
        def jniPath = "src/main/jni"
        cppFlags += "-I${file(jniPath)}".toString()
        file(jniPath).eachDirRecurse { dir ->
            cppFlags += "-I${file(dir)}".toString()
        }

        CFlags += cppFlags

        ldLibs += ["android", "EGL","GLESv2", "dl", "log", "jpeg", "png", "tiff", "z"]
        stl = "c++_static"
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles += file('proguard-rules.txt')
        }
    }
    android.productFlavors {
        def libPath = "src/main/jni/prebuilt/"
        create("arm7") {
            ndk.abiFilters += "armeabi-v7a"
            ndk.ldFlags += "-L${file(libPath+"libjpeg/libs/armeabi-v7a/")}".toString()
            ndk.ldFlags += "-L${file(libPath+"libpng/libs/armeabi-v7a/")}".toString()
            ndk.ldFlags += "-L${file(libPath+"libtiff/libs/armeabi-v7a/")}".toString()
        }
        create("arm") {
            ndk.abiFilters += "armeabi"
            ndk.ldFlags += "-L${file(libPath+"libjpeg/libs/armeabi/")}".toString()
            ndk.ldFlags += "-L${file(libPath+"libpng/libs/armeabi/")}".toString()
            ndk.ldFlags += "-L${file(libPath+"libtiff/libs/armeabi/")}".toString()
        }
        create("x86-32") {
            ndk.abiFilters += "x86"
            ndk.ldFlags += "-L${file(libPath+"libjpeg/libs/x86")}".toString()
            ndk.ldFlags += "-L${file(libPath+"libpng/libs/x86")}".toString()
            ndk.ldFlags += "-L${file(libPath+"libtiff/libs/x86")}".toString()
        }
        create("fat")
    }

Main issue I was running into is that in our old makefiles we actually had to add in the libraries by specifying "libtiff", "libpng", etc. here after linking the actual lib paths it just had to be "tiff", "png", etc. without "lib" prepended.

Alex Cohn

unread,
Oct 6, 2015, 4:43:48 PM10/6/15
to adt-dev
Please check the hybrid Android.mk + grade plugin approach as demonstrated for define LOCAL_SRC_FILES in ndk{} DSL.

Cheers,
Alex

Igor Ganapolsky

unread,
Jul 18, 2016, 9:32:49 AM7/18/16
to adt-dev, pro...@google.com
That repo doesn't even exist anymore.  Are you sure it is current?

Igor Ganapolsky

unread,
Jul 20, 2016, 1:07:16 PM7/20/16
to adt-dev
If you are using gradle-experimental:0.7.0, you would do this in the model.repositories.libs block.  Like this:
model {
    repositories {
        libs(PrebuiltLibraries) {
            // These are akin to LOCAL_SRC_FILES in the old Android.mk
           
           somelibrary_prebuilt {
               binaries.withType(StaticLibraryBinary) {
                   staticLibraryFile = file("${TARGET_ARCH_ABI}/libsomelibrary.a")
        }
      }
    }
  }
}   

On Monday, July 13, 2015 at 9:53:29 PM UTC-4, Nimrod Dayan wrote:

Igor Ganapolsky

unread,
Aug 8, 2016, 2:56:03 PM8/8/16
to adt-dev
What about in gradle-experimental:0.8.0-alpha7?
Reply all
Reply to author
Forward
0 new messages