1#!/bin/bash
2
3# Copyright 2019 The Amber Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     https://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -x
18
19if [[ $1 == "" ]]; then
20  echo "Usage: $0 [build directory]"
21  exit 1
22fi
23
24BUILD_DIR=$(readlink -f $1)
25if [[ $(ls $BUILD_DIR 2> /dev/null) == "" ]]; then
26  mkdir -p $BUILD_DIR
27fi
28
29if [[ $ANDROID_SDK_HOME == "" ]]; then
30  echo "Error: ANDROID_SDK_HOME missing, please set env variable e.g.,"
31  echo "       $ export ANDROID_SDK_HOME=path/to/Android/SDK"
32  exit 1
33fi
34
35if [[ $ANDROID_NDK_HOME == "" ]]; then
36  echo "Error: ANDROID_NDK_HOME missing, please set env variable e.g.,"
37  echo "       $ export ANDROID_NDK_HOME=path/to/Android/NDK"
38  exit 1
39fi
40
41if [[ $(command -v javac) == "" ]]; then
42  echo "Error: Install Java. Recommended version is Java 8."
43  exit 1
44fi
45
46if [[ $KEY_STORE_PATH == "" ]]; then
47  echo "Error: KEY_STORE_PATH missing, please set env variable."
48  exit 1
49fi
50
51ANDROID_SOURCE_DIR=$(dirname $(readlink -f $0))/../android_sample
52
53APK_NAME=AmberSample.apk
54ANDROID_PLATFORM=android-28
55ANDROID_BUILD_TOOL_VERSION=28.0.0
56ABI=arm64-v8a
57BUILD_TYPE=Release
58
59AAPT=$ANDROID_SDK_HOME/build-tools/$ANDROID_BUILD_TOOL_VERSION/aapt
60AAPT_ADD="$AAPT add"
61AAPT_PACK="$AAPT package -f -I
62           $ANDROID_SDK_HOME/platforms/$ANDROID_PLATFORM/android.jar"
63
64DX="$ANDROID_SDK_HOME/build-tools/$ANDROID_BUILD_TOOL_VERSION/dx --dex"
65
66JAVAC="javac -classpath
67       $ANDROID_SDK_HOME/platforms/$ANDROID_PLATFORM/android.jar
68       -sourcepath $BUILD_DIR/gen -d $BUILD_DIR"
69
70mkdir -p $BUILD_DIR/gen $BUILD_DIR/output/lib/$ABI $BUILD_DIR/$BUILD_TYPE
71
72pushd $BUILD_DIR/$BUILD_TYPE
73cmake \
74  -DANDROID_ABI=$ABI \
75  -DANDROID_PLATFORM=$ANDROID_PLATFORM \
76  -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$BUILD_DIR/output/lib/$ABI \
77  -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
78  -DANDROID_NDK=$ANDROID_NDK_HOME \
79  -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake \
80  -DCMAKE_MAKE_PROGRAM=$(which ninja) \
81  -GNinja \
82  -DANDROID_TOOLCHAIN=clang \
83  -DANDROID_STL=c++_static \
84  $ANDROID_SOURCE_DIR
85ninja
86popd
87
88ANDROID_VULKAN=$ANDROID_NDK_HOME/sources/third_party/vulkan
89for f in $(find $ANDROID_VULKAN/src/build-android/jniLibs/$ABI/ -name '*.so')
90do
91  LINK=$BUILD_DIR/output/lib/$ABI/$(basename $f)
92  if [[ $(ls $LINK 2> /dev/null) == "" ]]; then
93    ln -s $f $LINK
94  fi
95done
96
97$AAPT_PACK --non-constant-id -m \
98  -M $ANDROID_SOURCE_DIR/AndroidManifest.xml \
99  -S $ANDROID_SOURCE_DIR/res \
100  -J $BUILD_DIR/gen/ \
101  --generate-dependencies
102
103$AAPT_PACK -m \
104  -M $ANDROID_SOURCE_DIR/AndroidManifest.xml \
105  -A $ANDROID_SOURCE_DIR/assets \
106  -S $ANDROID_SOURCE_DIR/res \
107  -J "$BUILD_DIR/gen" \
108  -F "$BUILD_DIR/$APK_NAME" \
109  --shared-lib $BUILD_DIR/output
110
111$JAVAC $BUILD_DIR/gen/com/google/amber/*.java
112$DX --output="$BUILD_DIR/classes.dex" $BUILD_DIR
113
114cd $BUILD_DIR
115$AAPT_ADD $APK_NAME classes.dex
116
117$ANDROID_SDK_HOME/build-tools/$ANDROID_BUILD_TOOL_VERSION/apksigner sign \
118--min-sdk-version 28 --ks $KEY_STORE_PATH $APK_NAME
119
120echo "Successfully built $BUILD_DIR/$APK_NAME"
121