Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    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'
    CPU_ZERO(&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.

like image 477
HardLuck Avatar asked Feb 24 '17 00:02

HardLuck


People also ask

Can you compile C for Android?

Android is based on Linux Kernel so it's definitely possible to compile & run C/C++ programs on Android. C is quite cross-platform , so a C Program written in Windows can Run on Linux ( and android ) and vice versa. Special Note : You might be wondering why i included C++ where we should be only focusing on C.

What C library does Android use?

libc++ LLVM's libc++ is the C++ standard library that has been used by the Android OS since Lollipop, and as of NDK r18 is the only STL available in the NDK. Note: For full details of the expected level of C++ library support for any given version, see the C++14 Status, C++17 Status, and C++20 Status pages.

Can I make Android app using C++?

C++ can be used for Android App Development using the Android Native Development Kit(NDK). However, an app cannot be created totally using C++ and the NDK is used to implement parts of the app in C++ native code. This helps in using C++ code libraries for the app as required.


1 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.

like image 121
Dan Albert Avatar answered Sep 30 '22 05:09

Dan Albert