Compiling C library for android

As a proof of concept, I’m trying to compile and utilize the power of this library for an android app. I’m using the android studio. What I’ve done so far was:

  1. Installed NDK and SDK (I would like to target android version 4.0.3 and above if possible)
  2. Created an android project in Android Studio
  3. Created the JNI folder in
  4. Stored all the contents of the library in my JNI folder
  5. Linked my build.gradle to the Android.MK file that was present in the sources

  6. Google Speech Recognition timeout
  7. Testing android app using Android Studio
  8. DialogFragment.dismiss crashing with NullPointerException
  9. Escape “ ” in Text in TextView
  10. Which animation/view is used for Recent apps/History in android 5.0(Lollipop)?
  11. Android - Get time of chronometer widget
  12. externalNativeBuild {
         ndkBuild {
             path 'src/main/jni/'
  13. Set ProductFlavors in order to avoid trying to make a build for architectures that are not supported by the library (i might be wrong here already)

    productFlavors {
        dev { ndk.abiFilters 'x86', 'armeabi', 'armeabi-v7a' }
        develx86 { ndk.abiFilters 'x86', 'armeabi', 'armeabi-v7a' }
        production { ndk.abiFilters 'x86', 'armeabi', 'armeabi-v7a' }
        productionx86 { ndk.abiFilters 'x86', 'armeabi', 'armeabi-v7a' }
  14. At this point, I’ve noticed that some files like cpu-miner.c are missing some includes highlighting them as missing, specifically

    #include <curl/curl.h>
    #include <openssl/sha.h>`

So I went to the official websites of these two libraries and downloaded the sources, created two corresponding folders and places all the files there. In OpenSSL there was an file – a config as I understand. I was able to get a configured one from here (might also be a place where I made a mistake)

  1. I’ve added to JNI folder root with the following content APP_ABI:= armeabi armeabi-v7a x86 to exclude some of the architectures I don’t support.

At this point, I have some problems – one of them is that I can’t actually build my JNI stuff with NDK-build – sched.h doesn’t get resolved (even if I increase API version to 20). This results in errors like:

JNI/cpu-miner.c:473:2: error: use of undeclared identifier 'cpu_set_t'
    cpu_set_t set;
jni/cpu-miner.c:474:12: error: use of undeclared identifier 'set'

So the questions are:

  1. How can I force the build to resolve sched.h?
  2. Was my approach correct with downloading and adding the sources of missing libraries or there was some other option – I assume that there is one indeed because they were not included in the library source code? If so – could somebody please provide instructions how to do this with NDK (or it doesn’t matter what I use for that?)
  3. As soon as I fix the library build will it be enough to just generate java interface to my c files and start using them or there are additional steps to consider?

    Please feel free to ask for any additional details if you require any, thank you in advance.
    Disclaimer: I’m the a.NET dev so please be more specific about android/C things where applicable.
    Disclaimer 2: This program is being made for educational purposes as a proof of concept.

Related posts:

Corona SDK free alternatives
Distributing Android apps outside of the Android Market
How to implement Image Gallery in Gridview in android?
How to remove listview all items
Read x y z coordinates of android phone using accelerometer
GIF Image display using WebView
  • Android: How to find the position clicked from the context menu
  • Hot swap debugging with Android
  • Android - If WebView visible an extra, buggus, View is also is created
  • Tools.jar is not in Android Studio classpath
  • WIN DEATH error in Android?
  • Enumerate/Iterate all Views in Activity?
  • 2 Solutions collect form web for “Compiling C library for android”

    It looks like cpu_set_t didn’t make it into the NDK headers until android-21.

    NDK r14 (unreleased, but should be out next week) has completely redone the headers so that you’ll have access to the struct definitions and constants (whether or not the APIs you’ll need will be available before android-21 is a different question). To use them, grab the r14 beta and follow the instructions for unified headers.

    Try add this before your include <sched.h>

    #define _GNU_SOURCE

    In case if it doesn’t work, just add below structure in you cpu-miner.c file:

    #define CPU_SETSIZE 1024
    #define __NCPUBITS  (8 * sizeof (unsigned long))
    typedef struct
       unsigned long __bits[CPU_SETSIZE / __NCPUBITS];
    } cpu_set_t;
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.