2015年2月9日 星期一

Android Bash Command

bash command for Android automation.

$SDK_PATH: full path of Android SDK/build-tools/[version]
$apk: full path of apk file
  • Get app's label name from apk
    app_name=$($SDK_PATH/aapt d --values badging $apk | sed -n "/^application/s/.*label='\([^']*\).*/\1/p")
  • Get app's version name from apk
    app_version=$($SDK_PATH/aapt d --values badging $apk | sed -n "/^package:/s/.*versionName='\([^']*\).*/\1/p")
  • Get app's package name from apk
    pkg_name=$($SDK_PATH/aapt d --values badging $apk | sed -n "/^package:/s/.*name='\([^']*\).*/\1/p")
  • Get app's lanchable activity name from apk
    act_name=$($SDK_PATH/aapt d --values badging $apk | sed -n "/^launchable-activity:/s/.*name='\([^']*\).*/\1/p")
  • Get app's icon from apk
    unzip -p $apk $($SDK_PATH/aapt d --values badging $apk | sed -n "/^application/s/.*240:'\([^']*\).*/\1/p") > "$output_dir/icon-hdpi.png"
    unzip -p $apk $($SDK_PATH/aapt d --values badging $apk | sed -n "/^application/s/.*320:'\([^']*\).*/\1/p") > "$output_dir/icon-xhdpi.png"
    unzip -p $apk $($SDK_PATH/aapt d --values badging $apk | sed -n "/^application/s/.*480:'\([^']*\).*/\1/p") > "$output_dir/icon-xxhdpi.png"

  • Install app
    output=`adb install -r $apk`
    echo "$output"
  • Unstall app
    adb uninstall $pkg_name
  • Launch app
    adb shell am start -n $pkg_name/$act_name
  • Check app launched  from logcat
    ./logcat_monitor.py "Displayed $pkg_name"

  • Run monkey with random seed
    seed=`date +%s%N`
    adb shell monkey -s $seed -p $pkg_name -v $MONKEY_EVENT_COUNT > "$output_dir/monkey.log"
  • Get screencap
    adb shell screencap -p | sed 's/\r$//' > "$abs_filename"

  • Get file name (without extention/ path)
    apk_fullname_no_ext=${apk%.*}
    app_name=${apk_fullname_no_ext##*/}
  • for loop
    regex_app_dir=$APP_DIR/*.apk
    for file in $regex_app_dir
    do
     APPS_COUNT=$((APPS_COUNT+1))
     echo "# $APPS_COUNT: $file"
    done
  • Array usage
    # declare
    Install_Failed_List=()
    
    # push data into array and write error message to file
    if [[ $output == *"Failure"* ]] 
     then
      info="${apk} ($app_name)"
      Install_Failed_List[${#Install_Failed_List[*]}]=$info
      echo "$output" > "$output_dir/install_failed.txt"
      return 1
     fi
    
    # print data in array
    index=0
    for name in "${Install_Failed_List[@]}"
    do
     index=$((index+1))
     printf "  %10d   %s\n" $index "$name"
    done
    

沒有留言:

張貼留言