• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Copyright (C) 2021 The Android Open Source Project
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#      http://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
17readme() {
18  echo '''
19Install apps in an app bundle release directory to the device via adb, e.g.
20./batch_install_app.sh /path/to/app_bundle /path/to/report
21
22Note: aapt is needed to get the metadata from APKs.
23'''
24}
25
26SECONDS=0
27MY_NAME=$0
28SCRIPT_NAME="${MY_NAME##*/}"
29SCRIPT_DIR="${MY_NAME%/$SCRIPT_NAME}"
30echo Running from $SCRIPT_DIR
31
32if [[ -z $OUT_DIR ]]; then
33  OUT_DIR="${HOME}/Downloads"
34fi
35
36INPUT_DIR=$1
37if [[ ! -d ${INPUT_DIR} ]]; then
38  echo "Error: ${INPUT_DIR} is not a directory."
39  readme
40  exit
41fi
42
43echo "LOG=${LOG}"
44log() {
45  if [[ -n ${LOG} ]]; then
46    echo $1
47  fi
48}
49
50# check an app/package version via adb, e.g.
51# checkAppVersion package_name
52checkAppVersion() {
53  pkg=$1
54  cmd="adb shell dumpsys package ${pkg}"
55  dump=$(${cmd})
56  log "$dump"
57  echo "${dump}" | grep versionName
58}
59
60echo "Process all APKs in ${INPUT_DIR}"
61# apkDic[apk_name]=apk_path
62declare -A apkDic="$(${SCRIPT_DIR}/get_file_dir.sh ${INPUT_DIR} apk)"
63echo "Found: ${#apkDic[@]} apks"
64
65screenshotDir="/data/local/tmp/screenshots"
66echo "Removig the following screenshots from the device"
67adb shell ls -l ${screenshotDir}
68adb shell rm -r ${screenshotDir}
69adb shell mkdir -p ${screenshotDir}
70
71# apkBadgingDic[apk_name]=aapt_badging_output_string
72declare -A apkBadgingDic
73# manifestDic[apk_name]=AndroidManifest_xml_content_string
74declare -A manifestDic
75i=1
76for apk in "${!apkDic[@]}"; do
77  path="${apkDic[${apk}]}"
78  badging=$(aapt dump badging ${path})
79  apkBadgingDic[${apk}]="\"${badging}\""
80  log "${apkBadgingDic[${apk}]}"
81
82  # Get package name from the aapt badging output string
83  # ... package: name='com.google.android.gsf' versionCode...
84  pkg0=${badging#package: name=\'}
85  pkg=${pkg0%\' versionCode*}
86
87  echo "$i,${pkg},${apk},${path}"
88  checkAppVersion ${pkg}
89  ${SCRIPT_DIR}/install_apk.sh ${path}
90  checkAppVersion ${pkg}
91  echo
92
93  # Get the 1st launchable activity
94  # ... launchable-activity: name='com.google.android.maps.MapsActivity'  label...
95  if [[ "$badging" == *"launchable-activity: name="* ]]; then
96    activity0=${badging#*launchable-activity: name=\'}
97    activity=${activity0%%\'  label=*}
98    echo "Launching an activity: ${activity}"
99    adb shell am start -n "${pkg}/${activity}"
100    sleep 5
101    adb shell screencap "${screenshotDir}/${pkg}.png"
102    echo "grep screen"
103  fi
104
105  i=$(($i + 1))
106done
107
108adb shell ls -l ${screenshotDir}
109adb pull ${screenshotDir} ${OUT_DIR}
110echo "Took ${SECONDS} seconds"
111