android-aar.sh (5802B)
1 #! /bin/sh 2 3 # Create an AAR with libsodium in all combinations of static | shared | minimal | full. 4 # 5 # To simplify linking, library variants have distinct names: sodium, sodium-static, sodium-minimal and sodium-minimal-static. 6 7 SODIUM_VERSION="1.0.20.0" 8 9 if [ -z "$ANDROID_NDK_HOME" ]; then 10 echo "ANDROID_NDK_HOME must be set to the directory containing the Android NDK." 11 exit 1 12 fi 13 14 if [ ! -f "${ANDROID_NDK_HOME}/ndk-build" ]; then 15 echo "The ANDROID_NDK_HOME directory does not contain an 'ndk-build' file." 16 exit 1 17 fi 18 19 if [ ! -d "${ANDROID_NDK_HOME}/toolchains" ]; then 20 echo "The ANDROID_NDK_HOME directory does not contain a 'toolchains' directory." 21 exit 1 22 fi 23 24 if [ ! -f "${ANDROID_NDK_HOME}/source.properties" ]; then 25 echo "The ANDROID_NDK_HOME directory does not contain a 'source.properties' file." 26 exit 1 27 fi 28 29 NDK_VERSION=$(grep "Pkg.Revision = " <"${ANDROID_NDK_HOME}/source.properties" | cut -f 2 -d '=' | cut -f 2 -d' ' | cut -f 1 -d'.') 30 DEST_PATH=$(mktemp -d) 31 32 if [ -z "$NDK_PLATFORM" ]; then 33 export NDK_PLATFORM="android-21" 34 echo "Compiling for default platform: [${NDK_PLATFORM}]" 35 echo "That can be changed by setting an NDK_PLATFORM environment variable." 36 fi 37 38 SDK_VERSION=$(echo "$NDK_PLATFORM" | cut -f2 -d"-") 39 if [ -z "$NDK_VERSION" ]; then 40 echo "Failed to determine the NDK version." 41 exit 1 42 fi 43 if [ -z "$SDK_VERSION" ]; then 44 echo "Failed to determine the SDK version." 45 exit 1 46 fi 47 echo "NDK version: [$NDK_VERSION]" 48 echo "SDK version: [$SDK_VERSION]" 49 50 echo 51 52 if which zip >/dev/null; then 53 echo "The 'zip' command is installed." 54 else 55 echo "The 'zip' command is not installed. Please install it before running this script." 56 exit 1 57 fi 58 59 cd "$(dirname "$0")/../" || exit 60 61 trap 'kill -TERM -$$' INT 62 63 make_abi_json() { 64 echo "{\"abi\":\"${NDK_ARCH}\",\"api\":${SDK_VERSION},\"ndk\":${NDK_VERSION},\"stl\":\"none\"}" >"$1/abi.json" 65 } 66 67 make_prefab_json() { 68 echo "{\"name\":\"sodium\",\"schema_version\":1,\"dependencies\":[],\"version\":\"$SODIUM_VERSION\"}" >"$1/prefab.json" 69 } 70 71 make_manifest() { 72 echo "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" package=\"com.android.ndk.thirdparty.sodium\" android:versionCode=\"1\" android:versionName=\"1.0\"> 73 <uses-sdk android:minSdkVersion=\"$SDK_VERSION\" android:targetSdkVersion=\"$SDK_VERSION\"/> 74 </manifest>" >"${1}/AndroidManifest.xml" 75 } 76 77 make_prefab_structure() { 78 for variant_dirs in "prefab" "prefab/modules" "META-INF"; do 79 mkdir "${DEST_PATH}/${variant_dirs}" 80 done 81 82 make_prefab_json "${DEST_PATH}/prefab" 83 make_manifest "${DEST_PATH}" 84 cp "LICENSE" "${DEST_PATH}/META-INF" 85 86 for variant in \ 87 "prefab/modules/sodium" "prefab/modules/sodium-static" \ 88 "prefab/modules/sodium-minimal" "prefab/modules/sodium-minimal-static"; do 89 mkdir "${DEST_PATH}/${variant}" 90 91 if [ "$variant" = "prefab/modules/sodium-minimal" ]; then 92 echo "{\"library_name\":\"libsodium\"}" >"${DEST_PATH}/${variant}/module.json" 93 else 94 echo "{}" >"${DEST_PATH}/${variant}/module.json" 95 fi 96 97 mkdir "${DEST_PATH}/${variant}/libs" 98 99 for arch in "arm64-v8a" "armeabi-v7a" "x86" "x86_64"; do 100 mkdir "$DEST_PATH/${variant}/libs/android.${arch}" 101 mkdir "$DEST_PATH/${variant}/libs/android.${arch}/include" 102 NDK_ARCH="$arch" 103 104 make_abi_json "$DEST_PATH/${variant}/libs/android.${arch}" 105 done 106 done 107 } 108 109 copy_libs() { 110 SRC_DIR="libsodium-android-${1}" 111 112 SHARED_DEST_DIR="${DEST_PATH}/prefab/modules/sodium${3}/libs/android.${2}" 113 STATIC_DEST_DIR="${DEST_PATH}/prefab/modules/sodium${3}-static/libs/android.${2}" 114 115 cp -r "${SRC_DIR}/include" "$SHARED_DEST_DIR" 116 cp -r "${SRC_DIR}/include" "$STATIC_DEST_DIR" 117 cp "${SRC_DIR}/lib/libsodium.so" "${SHARED_DEST_DIR}/libsodium.so" 118 cp "${SRC_DIR}/lib/libsodium.a" "${STATIC_DEST_DIR}/libsodium${3}-static.a" 119 120 rm -r "$SRC_DIR" 121 } 122 123 build_all() { 124 dist-build/android-armv7-a.sh 125 dist-build/android-armv8-a.sh 126 dist-build/android-x86_64.sh 127 dist-build/android-x86.sh 128 } 129 130 make_prefab_structure 131 132 build_all 133 134 copy_libs "armv7-a" "armeabi-v7a" "-minimal" 135 copy_libs "armv8-a+crypto" "arm64-v8a" "-minimal" 136 copy_libs "i686" "x86" "-minimal" 137 copy_libs "westmere" "x86_64" "-minimal" 138 139 LIBSODIUM_FULL_BUILD="Y" 140 export LIBSODIUM_FULL_BUILD 141 142 build_all 143 144 copy_libs "armv7-a" "armeabi-v7a" 145 copy_libs "armv8-a+crypto" "arm64-v8a" 146 copy_libs "i686" "x86" 147 copy_libs "westmere" "x86_64" 148 149 AAR_PATH="$(pwd)/libsodium-${SODIUM_VERSION}.aar" 150 cd "$DEST_PATH" || exit 151 rm "$AAR_PATH" 152 zip -9 -r "$AAR_PATH" META-INF prefab AndroidManifest.xml 153 cd .. || exit 154 rm -r "$DEST_PATH" 155 156 echo " 157 158 Congrats you have built an AAR containing libsodium! 159 The build used a min Android SDK of version $SDK_VERSION 160 You can build for a different SDK version by specifying NDK_PLATFORM=\"android-{SDK_VERSION}\" 161 as an environment variable before running this script but the defaults should be fine. 162 163 To use the aar with 164 gradle or cmake (as set by default for Android Studio projects): 165 166 - Edit the app/build.gradle file to add: 167 168 android { 169 buildFeatures { 170 prefab true 171 } 172 } 173 174 and 175 176 dependencies { 177 implementation fileTree(dir:'path/to/aar/',include:['libsodium-$SODIUM_VERSION.aar']) 178 } 179 180 Optionally, store multiple AAR files in the same folder and include '*.aar' 181 182 - Edit your module's CMakeLists.txt file to add: 183 184 find_package(sodium REQUIRED CONFIG) 185 186 - Then, specify 'sodium::x' as an item in the relevant 'target_link_libraries' statement. 187 The first part is the AAR name and should be 'sodium'. 188 The second part ('x', to be replaced) should be set to: 189 - 'sodium' for the full shared library, 190 - 'sodium-static' for the full static library 191 - 'sodium-minimal' for the minimal shared library, or 192 - 'sodium-minimal-static' for the minimal static library. 193 194 "