Search code examples
androidc++reactivexrxcpp

Compile rxcpp for Android


I download RxCpp from github, modify the CMakeLists.txt as follow (I use the CMake modified by Microsoft in order to build cross platform project with VS2015):

cmake_minimum_required (VERSION 3.4)
project(RxCppAndroid VERSION 2.2.4 LANGUAGES C CXX)
file(GLOB_RECURSE SOURCES "rxcpp/*.*")

# Grouping all the source files puts them into a virtual folder in Visual Studio
source_group("src" FILES ${SOURCES})

add_library(RxCppAndroid SHARED ${SOURCES})
SET_TARGET_PROPERTIES(RxCppAndroid PROPERTIES LINKER_LANGUAGE CXX)

# optional, demo of the VC_MDD_ANDROID* properties
set_property(TARGET RxCppAndroid PROPERTY VC_MDD_ANDROID_USE_OF_STL "c++_static")
set_property(TARGET RxCppAndroid PROPERTY VC_MDD_ANDROID_API_LEVEL "android-21")

Created the Project.sln solution with the following command:

cmake -G “Visual Studio 14 ARM” –DCMAKE_SYSTEM_NAME=VCMDDAndroid

I tweaked a little bit the project build by CMake in order to:

  1. enable support for C++11
  2. enable exceptions
  3. Use of STL : GNU STL static library (gnustl_static)

I added a simple cpp file with the following code:

#include "rx.hpp"
int test()
{
    auto keys = rxcpp::observable<>::create<int>(
        [](rx::subscriber<int> dest) {
        for (;;) {
            int key = std::cin.get();
            dest.on_next(key);
        }
    }).
        publish();
}

Project does not compile and gives hundreds of errors. If I just try to compile with just this line:

#include "rx.hpp"

everything works fine. If I do

rxcpp::....

intellisense seems just recognizing a few methods (not much). Its not clear to me what's going on. I'm stuck. Does somebody have some suggestion on what to try? Does somebody have some success compiling RxCpp for Android?

//UPDATE - 03-01-2016

After some analysis turn out that most of the error messages were not compilation errors but related to intellisense errors. So right now the library compile successfully. I set up a simple Xamarin application that is using a simple c++ function as follow:

//C++
#include "rx.hpp"

    int test()
    {
        auto keys = rxcpp::observable<>::create<int>([](rxcpp::subscriber<int> dest)
        {
            dest.on_next(5);
        }).
            publish();

        //keys.subscribe([](int key)
        //{
        //   int a = key;
        //});

        return 0;
    }

extern "C"
{
    int test2()
    {
        return 5;
    }
}

//C#
        [DllImport("libTest.so")]
        public extern static int test2();
        var res=test2();

If I compiled and deploy on the phone everything work just fine (even thought really the "reactive" code is not used...). The problem is when I uncomment the following rows:

    keys.subscribe([](int key)
    {
       int a = key;
    });

Then the program compile fine (no error), the libTest.so is generated (the size is almost double the version with the commented line) but as soon as the Test2() function is called I get:

System.DllNotFOundException: libTest.so

I checked with adb.exe and really the libTest.so is present under: /data/data/"package_name"/lib/

What's going on?

//UPDATE - 06-01-2016

Finally I found some time to prepare a repo which reproduce the problem:

https://github.com/easysoft2k15/AndroidRxCppTest.git

My suspicion is that for some reasons when I include the line that crash the application, the size of the .so lib increase over a certain limit and thus Android does not load it anymore. Your thoughts would be highly appreciated.

Thank You

Alessandro


Solution

  • thanks to the repro and the issue Alessandro reported I was able to find a workaround!

    Apparently, the std lib support for tls is excluded on android and iOS. Previously someone contributed pthread support for rxcpp on iOS. The workaround is to add:

    #define RXCPP_ON_IOS

    before including rx.hpp or to set -DRXCPP_ON_IOS flag on the compiler.

    The fix will be a pull request that changes the pthread support to work for android as well as iOS.