Home » Android » Compiling C library for android

Compiling C library for android

Posted by: admin May 14, 2020 Leave a comment


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

    externalNativeBuild {
         ndkBuild {
             path 'src/main/jni/Android.mk'
  6. 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' }
  7. 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 h.in 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 Application.mk 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.

How to&Answers:

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;