Search code examples
androidshared-librariescocos2d-xstatic-librariescrypto++

Adding Cryptopp to Android.mk


I have tried building cryptopp library for android.I have used this part of the tutorial. https://www.cryptopp.com/wiki/Android_(Command_Line)

$ cat build-all-android.sh 
#!/bin/bash

for arch in armeabi armeabi-v7a armeabi-v7a-hard arm64-v8a mips mips64 x86 x86_64
do
    . ./setenv-android.sh $arch stlport
    if [ "$?" -eq "0" ]; then
        make -f GNUmakefile-cross distclean
        make -f GNUmakefile-cross static dynamic
        sudo make -f GNUmakefile-cross install PREFIX=/usr/local/cryptopp/android-$arch
    fi
done

I was able to make the libraries. My problem is, I can't add it to my project. It seems like I need to edit my Android.mk file but I can't seem to do it properly. I added this block to my Anroid.mk

#########################################################
# STLport library
include $(CLEAR_VARS)

STLPORT_INCL     := /Applications/Cocos/Android/android-ndk-r10e/sources/cxx-stl/stlport
STLPORT_LIB      := /Applications/Cocos/Android/android-ndk-r10e/sources/cxx-stl/stlport/libs/$(TARGET_ARCH_ABI)

LOCAL_MODULE := stlport_shared
LOCAL_SRC_FILES := $(STLPORT_LIB)/libstlport_shared.so
LOCAL_CPP_FEATURES += rtti exceptions

LOCAL_EXPORT_CPPFLAGS :=
LOCAL_EXPORT_C_INCLUDES := $(STLPORT_INCL)

include $(PREBUILT_SHARED_LIBRARY)

LOCAL_SHARED_LIBRARIES  := stlport_shared

#########################################################
# Crypto++ library
include $(CLEAR_VARS)

CRYPTOPP_INCL   := /usr/local/cryptopp/android-$(TARGET_ARCH_ABI)/include
CRYPTOPP_LIB    := /usr/local/cryptopp/android-$(TARGET_ARCH_ABI)/lib

LOCAL_MODULE       := cryptopp
LOCAL_SRC_FILES    := $(CRYPTOPP_LIB)/libcryptopp.so
LOCAL_CPP_FEATURES := rtti exceptions

LOCAL_EXPORT_C_INCLUDES := $(CRYPTOPP_INCL) $(CRYPTOPP_INCL)/cryptopp

include $(PREBUILT_SHARED_LIBRARY)

LOCAL_SHARED_LIBRARIES  := cryptopp

#########################################################

I based this on the Crypto++ wiki page Android Activity which referenced AndroidPRNG, which demonstrates using Crypto++ as a shared object on Android.

I'm stuck because I can't make it work. I'm very new to android so I don't exactly know what to do about it. I was able to make the ios version of it run. I followed the XCode tutorial. It's just this that's making me crazy. I would appreciate all the help I can get. Is there something wrong with how I build? After building, do I copy it to my project or just linking the installed verion is fine? How do I properly add/link it to my project? Sorry for asking so many questions.

PS: I'm using cocos2dx for my project. Maybe it's worth mentioning.


Solution

  • After reading and trying all sorts of combination for my Android.mk, I was able to compile cryptopp with cocos2dx.

    Anyway, here's how i fixed the compiler error:

    FIRST - Build the libraries

    cat build-all-android.sh 
    #!/bin/bash
    
    for arch in armeabi armeabi-v7a armeabi-v7a-hard arm64-v8a mips mips64 x86 x86_64
    do
        AOSP_PI="android-16" . ./setenv-android.sh $arch gnu-static
        if [ "$?" -eq "0" ]; then
            make -f GNUmakefile-cross distclean
            make -f GNUmakefile-cross static dynamic
            sudo make -f GNUmakefile-cross install PREFIX=/usr/local/cryptopp/android-$arch
        fi
    done
    

    Notice that I used gnu-static -> this is because cocos2dx is using gnu-static.


    SECOND - Update Android.mk

    # Crypto++ library
    include $(CLEAR_VARS)
    
    CRYPTOPP_INCL   := /usr/local/cryptopp/android-$(TARGET_ARCH_ABI)/include
    CRYPTOPP_LIB    := /usr/local/cryptopp/android-$(TARGET_ARCH_ABI)/lib
    LOCAL_MODULE       := cryptopp
    LOCAL_SRC_FILES    := $(CRYPTOPP_LIB)/libcryptopp.a
    LOCAL_CPP_FEATURES := rtti exceptions
    LOCAL_EXPORT_C_INCLUDES := $(CRYPTOPP_INCL) $(CRYPTOPP_INCL)/cryptopp
    include $(PREBUILT_STATIC_LIBRARY)
    LOCAL_SHARED_LIBRARIES  := cryptopp
    



    ---------------------------------------- FOR WINDOWS ----------------------------------------

    The answer above only works for mac. In Windows, I tried installing the cryptopp libraries but I wasn't successful. AOSP_TOOLCHAIN_PATH was invalid. I tried fixing it by updating the setenv-android.sh to include "windows-x86_64". I was able to fix that part but sadly I had a "make:not a command" error. I was told that I had to download it for my cygwin.

    I'm currently downloading it. I will udpate this answer when I'm able to install the make and sudo commands to my Windows.

    In the mean time, here's what I did to make my project using cryptopp compile in Windows assuming that you have successfully compiled it in mac:
    1.) Copy the cryptopp android specific libraries and header files from /usr/local/cryptopp to your jni folder. It should look something like this: folder hierarchy for cryptopp

    Note: In /usr/local/cryptopp the include files are located in the specific android folder. In the example above, I placed the include file outside since they are all using the same header files. This way, we will avoid duplicated header files.

    2.) After copying, update your Android.mk. Make it point to your new cryptopp directory instead. If you followed the hierarchy in step1, your Android.mk code for cryptopp will look something like this:

    # Crypto++ library
    include $(CLEAR_VARS)
    
    CRYPTOPP_INCL   := $(LOCAL_PATH)/Cryptopp/include
    CRYPTOPP_LIB    := Cryptopp/android-$(TARGET_ARCH_ABI)/lib
    LOCAL_MODULE       := cryptopp
    LOCAL_SRC_FILES    := $(CRYPTOPP_LIB)/libcryptopp.a
    LOCAL_CPP_FEATURES := rtti exceptions
    LOCAL_EXPORT_C_INCLUDES := $(CRYPTOPP_INCL) $(CRYPTOPP_INCL)/cryptopp
    include $(PREBUILT_STATIC_LIBRARY)
    LOCAL_SHARED_LIBRARIES  := cryptopp
    

    Note: Notice that we didn't use $(LOCAL_PATH) for the CRYPTOPP_LIB which was used by LOCAL_SRC_FILES. This is to avoid having a missing directory: jni/jni/Cryptopp/android-$(TARGET_ARCH_ABI)/lib/libcryptopp.a. Remember that LOCAL_SRC_FILES start searching in jni/.