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