1#!/bin/bash
2
3set -exu -o pipefail
4cat /VERSION
5
6BASE_DIR="$(pwd)"
7
8# Install gRPC and codegen for the Android examples
9# (a composite gradle build can't find protoc-gen-grpc-java)
10
11cd "$BASE_DIR/github/grpc-java"
12
13export GRADLE_OPTS=-Xmx512m
14export LDFLAGS=-L/tmp/protobuf/lib
15export CXXFLAGS=-I/tmp/protobuf/include
16export LD_LIBRARY_PATH=/tmp/protobuf/lib
17export OS_NAME=$(uname)
18
19# Proto deps
20buildscripts/make_dependencies.sh
21
22./gradlew install
23
24# Build grpc-cronet
25
26pushd cronet
27../gradlew build
28popd
29
30# Build grpc-android
31
32pushd android
33../gradlew build
34popd
35
36# Build examples
37
38cd ./examples/android/clientcache
39./gradlew build
40cd ../routeguide
41./gradlew build
42cd ../helloworld
43./gradlew build
44
45cd "$BASE_DIR/github/grpc-java/examples/example-kotlin/android/helloworld/"
46./gradlew build
47
48# Skip APK size and dex count comparisons for non-PR builds
49
50if [[ -z "${KOKORO_GITHUB_PULL_REQUEST_COMMIT:-}" ]]; then
51    echo "Skipping APK size and dex count"
52    exit 0
53fi
54
55# Save a copy of set_github_status.py (it may differ from the base commit)
56
57SET_GITHUB_STATUS="$TMPDIR/set_github_status.py"
58cp "$BASE_DIR/github/grpc-java/buildscripts/set_github_status.py" "$SET_GITHUB_STATUS"
59
60
61# Collect APK size and dex count stats for the helloworld example
62
63HELLO_WORLD_OUTPUT_DIR="$BASE_DIR/github/grpc-java/examples/android/helloworld/app/build/outputs"
64
65read -r ignored new_dex_count < \
66  <("${ANDROID_HOME}/tools/bin/apkanalyzer" dex references \
67  "$HELLO_WORLD_OUTPUT_DIR/apk/release/app-release-unsigned.apk")
68
69set +x
70all_new_methods=`"${ANDROID_HOME}/tools/bin/apkanalyzer" dex packages \
71  --proguard-mapping "$HELLO_WORLD_OUTPUT_DIR/mapping/release/mapping.txt" \
72  "$HELLO_WORLD_OUTPUT_DIR/apk/release/app-release-unsigned.apk" | grep ^M | cut -f4 | sort`
73set -x
74
75new_apk_size="$(stat --printf=%s $HELLO_WORLD_OUTPUT_DIR/apk/release/app-release-unsigned.apk)"
76
77
78# Get the APK size and dex count stats using the pull request base commit
79
80cd $BASE_DIR/github/grpc-java
81git checkout HEAD^
82./gradlew install
83cd examples/android/helloworld/
84./gradlew build
85
86read -r ignored old_dex_count < \
87  <("${ANDROID_HOME}/tools/bin/apkanalyzer" dex references app/build/outputs/apk/release/app-release-unsigned.apk)
88
89set +x
90all_old_methods=`"${ANDROID_HOME}/tools/bin/apkanalyzer" dex packages --proguard-mapping app/build/outputs/mapping/release/mapping.txt app/build/outputs/apk/release/app-release-unsigned.apk | grep ^M | cut -f4 | sort`
91set -x
92
93old_apk_size="$(stat --printf=%s app/build/outputs/apk/release/app-release-unsigned.apk)"
94
95dex_count_delta="$((new_dex_count-old_dex_count))"
96
97apk_size_delta="$((new_apk_size-old_apk_size))"
98
99set +x
100dex_method_diff=`diff -u <(echo "$all_old_methods") <(echo "$all_new_methods") || true`
101set -x
102
103if [[ -n "$dex_method_diff" ]]
104then
105  echo "Method diff: ${dex_method_diff}"
106fi
107
108# Update the statuses with the deltas
109
110gsutil cp gs://grpc-testing-secrets/github_credentials/oauth_token.txt ~/
111
112"$SET_GITHUB_STATUS" \
113  --sha1 "$KOKORO_GITHUB_PULL_REQUEST_COMMIT" \
114  --state success \
115  --description "New DEX reference count: $(printf "%'d" "$new_dex_count") (delta: $(printf "%'d" "$dex_count_delta"))" \
116  --context android/dex_diff --oauth_file ~/oauth_token.txt
117
118"$SET_GITHUB_STATUS" \
119  --sha1 "$KOKORO_GITHUB_PULL_REQUEST_COMMIT" \
120  --state success \
121  --description "New APK size in bytes: $(printf "%'d" "$new_apk_size") (delta: $(printf "%'d" "$apk_size_delta"))" \
122  --context android/apk_diff --oauth_file ~/oauth_token.txt
123