xref: /aosp_15_r20/external/grpc-grpc-java/buildscripts/kokoro/android.sh (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1*e07d83d3SAndroid Build Coastguard Worker#!/bin/bash
2*e07d83d3SAndroid Build Coastguard Worker
3*e07d83d3SAndroid Build Coastguard Workerset -exu -o pipefail
4*e07d83d3SAndroid Build Coastguard Workercat /VERSION
5*e07d83d3SAndroid Build Coastguard Worker
6*e07d83d3SAndroid Build Coastguard WorkerBASE_DIR="$(pwd)"
7*e07d83d3SAndroid Build Coastguard Worker
8*e07d83d3SAndroid Build Coastguard Worker# Install gRPC and codegen for the Android examples
9*e07d83d3SAndroid Build Coastguard Worker# (a composite gradle build can't find protoc-gen-grpc-java)
10*e07d83d3SAndroid Build Coastguard Worker
11*e07d83d3SAndroid Build Coastguard Workercd "$BASE_DIR/github/grpc-java"
12*e07d83d3SAndroid Build Coastguard Worker
13*e07d83d3SAndroid Build Coastguard Workerexport LDFLAGS=-L/tmp/protobuf/lib
14*e07d83d3SAndroid Build Coastguard Workerexport CXXFLAGS=-I/tmp/protobuf/include
15*e07d83d3SAndroid Build Coastguard Workerexport LD_LIBRARY_PATH=/tmp/protobuf/lib
16*e07d83d3SAndroid Build Coastguard Workerexport OS_NAME=$(uname)
17*e07d83d3SAndroid Build Coastguard Worker
18*e07d83d3SAndroid Build Coastguard Workercat <<EOF >> gradle.properties
19*e07d83d3SAndroid Build Coastguard Worker# defaults to -Xmx512m -XX:MaxMetaspaceSize=256m
20*e07d83d3SAndroid Build Coastguard Worker# https://docs.gradle.org/current/userguide/build_environment.html#sec:configuring_jvm_memory
21*e07d83d3SAndroid Build Coastguard Worker# Increased due to java.lang.OutOfMemoryError: Metaspace failures, "JVM heap
22*e07d83d3SAndroid Build Coastguard Worker# space is exhausted", and to increase build speed
23*e07d83d3SAndroid Build Coastguard Workerorg.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=1024m
24*e07d83d3SAndroid Build Coastguard WorkerEOF
25*e07d83d3SAndroid Build Coastguard Worker
26*e07d83d3SAndroid Build Coastguard Workerecho y | ${ANDROID_HOME}/tools/bin/sdkmanager "build-tools;28.0.3"
27*e07d83d3SAndroid Build Coastguard Worker
28*e07d83d3SAndroid Build Coastguard Worker# Proto deps
29*e07d83d3SAndroid Build Coastguard Workerbuildscripts/make_dependencies.sh
30*e07d83d3SAndroid Build Coastguard Worker
31*e07d83d3SAndroid Build Coastguard Worker# Build Android with Java 11, this adds it to the PATH
32*e07d83d3SAndroid Build Coastguard Workersudo update-java-alternatives --set java-1.11.0-openjdk-amd64
33*e07d83d3SAndroid Build Coastguard Worker# Unset any existing JAVA_HOME env var to stop Gradle from using it
34*e07d83d3SAndroid Build Coastguard Workerunset JAVA_HOME
35*e07d83d3SAndroid Build Coastguard Worker
36*e07d83d3SAndroid Build Coastguard WorkerGRADLE_FLAGS="-Pandroid.useAndroidX=true"
37*e07d83d3SAndroid Build Coastguard Worker
38*e07d83d3SAndroid Build Coastguard Worker./gradlew \
39*e07d83d3SAndroid Build Coastguard Worker    :grpc-android-interop-testing:build \
40*e07d83d3SAndroid Build Coastguard Worker    :grpc-android:build \
41*e07d83d3SAndroid Build Coastguard Worker    :grpc-cronet:build \
42*e07d83d3SAndroid Build Coastguard Worker    :grpc-binder:build \
43*e07d83d3SAndroid Build Coastguard Worker    assembleAndroidTest \
44*e07d83d3SAndroid Build Coastguard Worker    publishToMavenLocal \
45*e07d83d3SAndroid Build Coastguard Worker    $GRADLE_FLAGS
46*e07d83d3SAndroid Build Coastguard Worker
47*e07d83d3SAndroid Build Coastguard Workerif [[ ! -z $(git status --porcelain) ]]; then
48*e07d83d3SAndroid Build Coastguard Worker  git status
49*e07d83d3SAndroid Build Coastguard Worker  echo "Error Working directory is not clean. Forget to commit generated files?"
50*e07d83d3SAndroid Build Coastguard Worker  exit 1
51*e07d83d3SAndroid Build Coastguard Workerfi
52*e07d83d3SAndroid Build Coastguard Worker
53*e07d83d3SAndroid Build Coastguard Worker# Build examples
54*e07d83d3SAndroid Build Coastguard Worker
55*e07d83d3SAndroid Build Coastguard Workercd ./examples/android/clientcache
56*e07d83d3SAndroid Build Coastguard Worker../../gradlew build
57*e07d83d3SAndroid Build Coastguard Workercd ../routeguide
58*e07d83d3SAndroid Build Coastguard Worker../../gradlew build
59*e07d83d3SAndroid Build Coastguard Workercd ../helloworld
60*e07d83d3SAndroid Build Coastguard Worker../../gradlew build
61*e07d83d3SAndroid Build Coastguard Workercd ../strictmode
62*e07d83d3SAndroid Build Coastguard Worker../../gradlew build
63*e07d83d3SAndroid Build Coastguard Worker
64*e07d83d3SAndroid Build Coastguard Worker# Skip APK size and dex count comparisons for non-PR builds
65*e07d83d3SAndroid Build Coastguard Worker
66*e07d83d3SAndroid Build Coastguard Workerif [[ -z "${KOKORO_GITHUB_PULL_REQUEST_COMMIT:-}" ]]; then
67*e07d83d3SAndroid Build Coastguard Worker    echo "Skipping APK size and dex count"
68*e07d83d3SAndroid Build Coastguard Worker    exit 0
69*e07d83d3SAndroid Build Coastguard Workerfi
70*e07d83d3SAndroid Build Coastguard Worker
71*e07d83d3SAndroid Build Coastguard Worker# Save a copy of set_github_status.py (it may differ from the base commit)
72*e07d83d3SAndroid Build Coastguard Worker
73*e07d83d3SAndroid Build Coastguard WorkerSET_GITHUB_STATUS="$TMPDIR/set_github_status.py"
74*e07d83d3SAndroid Build Coastguard Workercp "$BASE_DIR/github/grpc-java/buildscripts/set_github_status.py" "$SET_GITHUB_STATUS"
75*e07d83d3SAndroid Build Coastguard Worker
76*e07d83d3SAndroid Build Coastguard Worker
77*e07d83d3SAndroid Build Coastguard Worker# Collect APK size and dex count stats for the helloworld example
78*e07d83d3SAndroid Build Coastguard Workersudo update-java-alternatives --set java-1.8.0-openjdk-amd64
79*e07d83d3SAndroid Build Coastguard Worker
80*e07d83d3SAndroid Build Coastguard WorkerHELLO_WORLD_OUTPUT_DIR="$BASE_DIR/github/grpc-java/examples/android/helloworld/app/build/outputs"
81*e07d83d3SAndroid Build Coastguard Worker
82*e07d83d3SAndroid Build Coastguard Workerread -r ignored new_dex_count < \
83*e07d83d3SAndroid Build Coastguard Worker  <("${ANDROID_HOME}/tools/bin/apkanalyzer" dex references \
84*e07d83d3SAndroid Build Coastguard Worker  "$HELLO_WORLD_OUTPUT_DIR/apk/release/app-release-unsigned.apk")
85*e07d83d3SAndroid Build Coastguard Worker
86*e07d83d3SAndroid Build Coastguard Workerset +x
87*e07d83d3SAndroid Build Coastguard Workerall_new_methods=`"${ANDROID_HOME}/tools/bin/apkanalyzer" dex packages \
88*e07d83d3SAndroid Build Coastguard Worker  --proguard-mapping "$HELLO_WORLD_OUTPUT_DIR/mapping/release/mapping.txt" \
89*e07d83d3SAndroid Build Coastguard Worker  "$HELLO_WORLD_OUTPUT_DIR/apk/release/app-release-unsigned.apk" | grep ^M | cut -f4 | sort`
90*e07d83d3SAndroid Build Coastguard Workerset -x
91*e07d83d3SAndroid Build Coastguard Worker
92*e07d83d3SAndroid Build Coastguard Workernew_apk_size="$(stat --printf=%s $HELLO_WORLD_OUTPUT_DIR/apk/release/app-release-unsigned.apk)"
93*e07d83d3SAndroid Build Coastguard Worker
94*e07d83d3SAndroid Build Coastguard Worker
95*e07d83d3SAndroid Build Coastguard Worker# Get the APK size and dex count stats using the pull request base commit
96*e07d83d3SAndroid Build Coastguard Workersudo update-java-alternatives --set java-1.11.0-openjdk-amd64
97*e07d83d3SAndroid Build Coastguard Worker
98*e07d83d3SAndroid Build Coastguard Workercd $BASE_DIR/github/grpc-java
99*e07d83d3SAndroid Build Coastguard Worker./gradlew clean
100*e07d83d3SAndroid Build Coastguard Workergit checkout HEAD^
101*e07d83d3SAndroid Build Coastguard Worker./gradlew --stop  # use a new daemon to build the previous commit
102*e07d83d3SAndroid Build Coastguard Worker./gradlew publishToMavenLocal $GRADLE_FLAGS
103*e07d83d3SAndroid Build Coastguard Workercd examples/android/helloworld/
104*e07d83d3SAndroid Build Coastguard Worker../../gradlew build
105*e07d83d3SAndroid Build Coastguard Worker
106*e07d83d3SAndroid Build Coastguard Workersudo update-java-alternatives --set java-1.8.0-openjdk-amd64
107*e07d83d3SAndroid Build Coastguard Workerread -r ignored old_dex_count < \
108*e07d83d3SAndroid Build Coastguard Worker  <("${ANDROID_HOME}/tools/bin/apkanalyzer" dex references app/build/outputs/apk/release/app-release-unsigned.apk)
109*e07d83d3SAndroid Build Coastguard Worker
110*e07d83d3SAndroid Build Coastguard Workerset +x
111*e07d83d3SAndroid Build Coastguard Workerall_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`
112*e07d83d3SAndroid Build Coastguard Workerset -x
113*e07d83d3SAndroid Build Coastguard Worker
114*e07d83d3SAndroid Build Coastguard Workerold_apk_size="$(stat --printf=%s app/build/outputs/apk/release/app-release-unsigned.apk)"
115*e07d83d3SAndroid Build Coastguard Worker
116*e07d83d3SAndroid Build Coastguard Workerdex_count_delta="$((new_dex_count-old_dex_count))"
117*e07d83d3SAndroid Build Coastguard Worker
118*e07d83d3SAndroid Build Coastguard Workerapk_size_delta="$((new_apk_size-old_apk_size))"
119*e07d83d3SAndroid Build Coastguard Worker
120*e07d83d3SAndroid Build Coastguard Workerset +x
121*e07d83d3SAndroid Build Coastguard Workerdex_method_diff=`diff -u <(echo "$all_old_methods") <(echo "$all_new_methods") || true`
122*e07d83d3SAndroid Build Coastguard Workerset -x
123*e07d83d3SAndroid Build Coastguard Worker
124*e07d83d3SAndroid Build Coastguard Workerif [[ -n "$dex_method_diff" ]]
125*e07d83d3SAndroid Build Coastguard Workerthen
126*e07d83d3SAndroid Build Coastguard Worker  echo "Method diff: ${dex_method_diff}"
127*e07d83d3SAndroid Build Coastguard Workerfi
128*e07d83d3SAndroid Build Coastguard Worker
129*e07d83d3SAndroid Build Coastguard Worker# Update the statuses with the deltas
130*e07d83d3SAndroid Build Coastguard Worker
131*e07d83d3SAndroid Build Coastguard Workergsutil cp gs://grpc-testing-secrets/github_credentials/oauth_token.txt ~/
132*e07d83d3SAndroid Build Coastguard Worker
133*e07d83d3SAndroid Build Coastguard Worker"$SET_GITHUB_STATUS" \
134*e07d83d3SAndroid Build Coastguard Worker  --sha1 "$KOKORO_GITHUB_PULL_REQUEST_COMMIT" \
135*e07d83d3SAndroid Build Coastguard Worker  --state success \
136*e07d83d3SAndroid Build Coastguard Worker  --description "New DEX reference count: $(printf "%'d" "$new_dex_count") (delta: $(printf "%'d" "$dex_count_delta"))" \
137*e07d83d3SAndroid Build Coastguard Worker  --context android/dex_diff --oauth_file ~/oauth_token.txt
138*e07d83d3SAndroid Build Coastguard Worker
139*e07d83d3SAndroid Build Coastguard Worker"$SET_GITHUB_STATUS" \
140*e07d83d3SAndroid Build Coastguard Worker  --sha1 "$KOKORO_GITHUB_PULL_REQUEST_COMMIT" \
141*e07d83d3SAndroid Build Coastguard Worker  --state success \
142*e07d83d3SAndroid Build Coastguard Worker  --description "New APK size in bytes: $(printf "%'d" "$new_apk_size") (delta: $(printf "%'d" "$apk_size_delta"))" \
143*e07d83d3SAndroid Build Coastguard Worker  --context android/apk_diff --oauth_file ~/oauth_token.txt
144