build_test_apk.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/bin/bash -e
  2. # Copyright 2012, The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Creates and builds projects from a RenderScript testcase and a set of Java templates
  16. HELP=no
  17. VERBOSE=no
  18. MINSDK=1
  19. TARGET=1
  20. NAME=""
  21. OUT_DIR=
  22. ACTIVITY=""
  23. PACKAGE=""
  24. SDK=""
  25. TESTCASE_PATH=""
  26. DRIVER=""
  27. check_param ()
  28. {
  29. if [ -z "$2" ]; then
  30. echo "ERROR: Missing parameter after option '$1'"
  31. exit 1
  32. fi
  33. }
  34. check_required_param()
  35. {
  36. if [ -z "$1" ]; then
  37. echo "ERROR: Missing required parameter $2"
  38. exit 1
  39. fi
  40. }
  41. run ()
  42. {
  43. if [ "$VERBOSE" = "yes" ] ; then
  44. echo "## COMMAND: $@"
  45. fi
  46. $@ 2>&1
  47. }
  48. process_template()
  49. {
  50. src=$1
  51. dest=$2
  52. sed -e "s/%ACTIVITY%/$3/g" -e "s/%PACKAGE%/$4/g" -e "s/%TESTCASE%/$5/g" -e "s/%MINSDK%/$6/g" < $src > $dest;
  53. echo "processed $src ==> $dest"
  54. }
  55. while [ -n "$1" ]; do
  56. opt="$1"
  57. case "$opt" in
  58. --help|-h|-\?)
  59. HELP=yes
  60. ;;
  61. --verbose|-v)
  62. VERBOSE=yes
  63. ;;
  64. --sdk)
  65. check_param $1 $2
  66. SDK="$2"
  67. ;;
  68. --name)
  69. check_param $1 $2
  70. NAME="$2"
  71. ;;
  72. --out)
  73. check_param $1 $2
  74. OUT_DIR="$2"
  75. ;;
  76. --activity)
  77. check_param $1 $2
  78. ACTIVITY="$2"
  79. ;;
  80. --package)
  81. check_param $1 $2
  82. PACKAGE="$2"
  83. ;;
  84. --minsdk)
  85. check_param $1 $2
  86. MINSDK="$2"
  87. ;;
  88. --target)
  89. check_param $1 $2
  90. TARGET="$2"
  91. ;;
  92. --testcase)
  93. check_param $1 $2
  94. TESTCASE_PATH="$2"
  95. ;;
  96. --driver)
  97. check_param $1 $2
  98. DRIVER="${2%/}"
  99. ;;
  100. -*) # unknown options
  101. echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
  102. exit 1
  103. ;;
  104. *) # Simply record parameter
  105. if [ -z "$PARAMETERS" ] ; then
  106. PARAMETERS="$opt"
  107. else
  108. PARAMETERS="$PARAMETERS $opt"
  109. fi
  110. ;;
  111. esac
  112. shift
  113. done
  114. if [ "$HELP" = "yes" ] ; then
  115. echo "Usage: $PROGNAME [options]"
  116. echo ""
  117. echo "Build a test project from a RS testcase and a java driver template."
  118. echo ""
  119. echo "Required Parameters:"
  120. echo " --sdk Location of Android SDK installation"
  121. echo " --out <path> Location of your project directory"
  122. echo " --testcase <name> The .rs testcase file with which to build the project"
  123. echo " --driver <name> The java template directory with which to build the project"
  124. echo ""
  125. echo "Optional Parameters (reasonable defaults are used if not specified)"
  126. echo " --activity <name> Name for your default Activity class"
  127. echo " --package <name> Package namespace for your project"
  128. echo " --target <name> Android build target. Execute 'android list targets' to list available targets and their ID's."
  129. echo " --minsdk <name> minSdkVersion attribute to embed in AndroidManifest.xml of test project."
  130. echo " --help|-h|-? Print this help"
  131. echo " --verbose|-v Enable verbose mode"
  132. echo ""
  133. exit 0
  134. fi
  135. # Verify required parameters are non-empty
  136. check_required_param "$SDK" "--sdk"
  137. check_required_param "$OUT_DIR" "--out"
  138. check_required_param "$TESTCASE_PATH" "--testcase"
  139. check_required_param "$DRIVER" "--driver"
  140. # Compute name of testcase
  141. TESTCASE=`basename $TESTCASE_PATH .rs`
  142. # Compute activity, appname, and java package, if not specified via parameters
  143. if [ -z "$ACTIVITY" ]; then
  144. ACTIVITY="$TESTCASE";
  145. fi
  146. if [ -z "$NAME" ]; then
  147. NAME="$ACTIVITY"
  148. fi
  149. if [ -z "$PACKAGE" ]; then
  150. PACKAGE=com.android.test.rsdebug.$TESTCASE
  151. fi
  152. # Create the project
  153. run $SDK/tools/android create project --target $TARGET --name $NAME --path $OUT_DIR --activity $ACTIVITY --package $PACKAGE
  154. if [ $? != 0 ] ; then
  155. echo "ERROR: Could not create Android project."
  156. echo " Check parameters and try again."
  157. exit 1
  158. fi
  159. # Compute name of destination source directory
  160. DEST_SRC_DIR=$OUT_DIR/src/`echo $PACKAGE | sed 's/\./\//g'`
  161. if [ ! -d "$DRIVER" ]; then
  162. # If driver directory does not exist, try to fix it up by searching the
  163. # testcase directory as well
  164. DRIVER=`dirname $TESTCASE_PATH`/"$DRIVER"
  165. if [ ! -d $DRIVER ]; then
  166. echo "unable to find driver in $DRIVER, please check --driver"
  167. exit 1;
  168. fi
  169. fi
  170. echo "Copying driver template from $DRIVER -> $DEST_SRC_DIR"
  171. if [ ! -d "$DEST_SRC_DIR" ]; then
  172. echo "Error, destination directory does not exist: $DEST_SRC_DIR";
  173. exit 1;
  174. fi
  175. echo "Performing template substitutions:"
  176. echo " %ACTIVITY% ==> $ACTIVITY"
  177. echo " %PACKAGE% ==> $PACKAGE"
  178. echo " %TESTCASE% ==> $TESTCASE"
  179. echo " %MINSDK% ==> $MINSDK"
  180. SUBST_PARAMS="$ACTIVITY $PACKAGE $TESTCASE $MINSDK"
  181. # If it exists, use contents of driver-common directory to seed
  182. # the testcase project
  183. DRIVER_COMMON="`dirname $TESTCASE_PATH`/driver-common"
  184. if [ -d $DRIVER_COMMON ]; then
  185. echo "Found common driver directory: $DRIVER_COMMON"
  186. ls $DRIVER_COMMON/SRC/*.java.template | while read src; do
  187. SRC_BASENAME=`basename $src .java.template`;
  188. dest=$DEST_SRC_DIR/`echo $SRC_BASENAME | sed "s/ACTIVITY/$ACTIVITY/g"`.java
  189. process_template $src $dest $SUBST_PARAMS
  190. done;
  191. # Copy AndroidManifest.xml
  192. COMMON_MANIFEST="$DRIVER_COMMON/AndroidManifest.xml"
  193. if [ -e $COMMON_MANIFEST ]; then
  194. process_template $COMMON_MANIFEST $OUT_DIR/AndroidManifest.xml $SUBST_PARAMS
  195. fi
  196. fi
  197. # Copy Java source to project directory.
  198. ls $DRIVER/*.java.template | while read src; do
  199. SRC_BASENAME=`basename $src .java.template`
  200. dest=$DEST_SRC_DIR/`echo $SRC_BASENAME | sed "s/ACTIVITY/$ACTIVITY/g"`.java
  201. process_template $src $dest $SUBST_PARAMS
  202. done;
  203. # Copy AndroidManifest.xml override, if it exists
  204. OVERRIDE_MANIFEST="$DRIVER/AndroidManifest.xml"
  205. if [ -e $OVERRIDE_MANIFEST ]; then
  206. process_template $OVERRIDE_MANIFEST $OUT_DIR/AndroidManifest.xml $SUBST_PARAMS
  207. fi
  208. # Copy RS testcase to project directory.
  209. TESTCASE_DEST=$DEST_SRC_DIR/`basename $TESTCASE_PATH`
  210. process_template $TESTCASE_PATH $TESTCASE_DEST $SUBST_PARAMS
  211. # Buid signed and aligned apk
  212. cd $OUT_DIR
  213. run ant clean debug install
  214. if [ $? != 0 ] ; then
  215. echo "ERROR: Apk build and install failed"
  216. exit 1
  217. fi
  218. exit 0