1# Building for Android NDK 2 3If you have the ndk and prebuilt toolchains with that, you can simply build 4lws library for your android app from one cmake and one make command. 5 6However if you want a tls lib, you have to take care of building and pointing 7to that first. But if it's a cmake project like mbedtls, that also is just a 8matter of one cmake and one make. 9 10## Installing NDK pieces 11 12There's probably a more direct way but the official way is install the whole 13Android Studio and then run `sdkmanager` to install a recent NDK. 14 15I installed the sdk and ndk pieces into /opt/android/ and that's how the 16`./contrib/cross-aarch64-android.cmake` toolchain file is shipped. You can 17adapt some settings at the top of that file including the path if needed. 18 19## Fetching lws (needed first for cross toolchain file) 20 21It doesn't care where you put these projects, but for simplicity they should 22be in the same parent dir, like 23 24``` 25 - /home/someone 26 - /home/someone/libwebsockets 27 - /home/someone/mbedtls 28``` 29 30The reason is that building mbedtls need the cross toolchain file from 31libwebsockets, that's also why we have to get libwebsockets first now but 32build it later. 33 34``` 35$ git clone https://libwebsockets.org/repo/libwebsockets 36``` 37 38## Building mbedtls 39 40``` 41$ git clone https://github.com/ARMmbed/mbedtls.git 42$ cd mbedtls 43$ mkdir build 44$ cd build 45$ rm -f CMakeCache.txt && \ 46 cmake .. -DCMAKE_TOOLCHAIN_FILE=../libwebsockets/contrib/cross-aarch64-android.cmake \ 47 -DUSE_SHARED_MBEDTLS_LIBRARY=1 \ 48 -DENABLE_PROGRAMS=0 \ 49 -Wno-dev && \ 50 make -j && \ 51 cmake --install . 52``` 53 54The lws toolchain file sets the path to install into as the cross root path, so 55despite it looks like the destination dir is missing for the install, it will 56go into, eg `/opt/android/ndk/21.1.6352462/platforms/android-24/arch-arm64/lib/libmbedcrypto.a` 57where lws will look for it 58 59## Building lws 60 61You don't need to explain where mbedtls can be found... lws will build with the 62same toolchain file that sets the cross root to the same place as mbedtls, it 63will easily find them there without any further hints. 64 65``` 66$ mkdir build 67$ cd build 68$ rm -f CMakeCache.txt && \ 69 cmake .. -DCMAKE_TOOLCHAIN_FILE=../libwebsockets/contrib/cross-aarch64-android.cmake \ 70 -DLWS_WITH_MBEDTLS=1 \ 71 -DLWS_WITHOUT_TESTAPPS=1 && \ 72 make && \ 73 cmake --install . 74``` 75 76That's it, both mbedtls and lws library and header files are installed into the 77ndk cross root. 78