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