Merge "Accept both old (without path) and new (with) outputs in getActiveApexes"
diff --git a/Android.bp b/Android.bp
index 3e83e65..1d55ff1 100644
--- a/Android.bp
+++ b/Android.bp
@@ -74,7 +74,7 @@
 }
 
 // Main Target to build tradefed jar
-tradefed_java_library_host {
+java_library_host {
     name: "tradefed",
     defaults: ["tradefed_defaults"],
     java_resource_dirs: [
@@ -85,7 +85,6 @@
         "tradefed-test-framework",
     ],
     manifest: "MANIFEST.mf",
-    required: ["loganalysis"],
 }
 
 java_library_host {
diff --git a/Android.mk b/Android.mk
index 07f6fff..25647f5 100644
--- a/Android.mk
+++ b/Android.mk
@@ -16,6 +16,18 @@
 COMPATIBILITY.tradefed_tests_dir := \
   $(COMPATIBILITY.tradefed_tests_dir) $(LOCAL_PATH)/res/config $(LOCAL_PATH)/tests/res/config
 
+include $(CLEAR_VARS)
+
+# makefile rules to copy jars to HOST_OUT/tradefed
+# so tradefed.sh can automatically add to classpath
+deps := $(call copy-many-files,\
+  $(call intermediates-dir-for,JAVA_LIBRARIES,tradefed,HOST)/javalib.jar:$(HOST_OUT)/tradefed/tradefed.jar)
+
+# this dependency ensures the above rule will be executed if jar is installed
+$(HOST_OUT_JAVA_LIBRARIES)/tradefed.jar : $(deps)
+# The copy rule for loganalysis is in tools/loganalysis/Android.mk
+$(HOST_OUT_JAVA_LIBRARIES)/tradefed.jar : $(HOST_OUT)/tradefed/loganalysis.jar
+
 #######################################################
 include $(CLEAR_VARS)
 
diff --git a/atest/Android.bp b/atest/Android.bp
index 3904aeb..35f74cb 100644
--- a/atest/Android.bp
+++ b/atest/Android.bp
@@ -61,6 +61,7 @@
     ],
     data: [
         "tools/updatedb_darwin.sh",
+        ":asuite_version",
     ],
     // Make atest's built name to atest-dev
     stem: "atest-dev",
@@ -202,3 +203,16 @@
     ],
 }
 
+
+genrule {
+    name: "asuite_version",
+    cmd: "DATETIME=$$(TZ='America/Log_Angelos' date +'%F');" +
+         "if [[ -n $$BUILD_NUMBER ]]; then" +
+         "  echo $${DATETIME}_$${BUILD_NUMBER} > $(out);" +
+         "else" +
+         "  echo $$(date +'%F_%R') > $(out);" +
+         "fi",
+    out: [
+        "VERSION",
+    ],
+}
diff --git a/atest/atest.py b/atest/atest.py
index 874479d..27d6e5e 100755
--- a/atest/atest.py
+++ b/atest/atest.py
@@ -562,6 +562,11 @@
         test_references=args.tests,
         cwd=os.getcwd(),
         os=platform.platform())
+    if args.version:
+        if os.path.isfile(constants.VERSION_FILE):
+            with open(constants.VERSION_FILE) as version_file:
+                print(version_file.read())
+        return constants.EXIT_CODE_SUCCESS
     mod_info = module_info.ModuleInfo(force_build=args.rebuild_module_info)
     if args.rebuild_module_info:
         _run_extra_tasks(join=True)
diff --git a/atest/atest_arg_parser.py b/atest/atest_arg_parser.py
index bfa8cb6..575a344 100644
--- a/atest/atest_arg_parser.py
+++ b/atest/atest_arg_parser.py
@@ -66,6 +66,8 @@
                                'debugger prior to execution.')
         self.add_argument('-v', '--verbose', action='store_true',
                           help='Display DEBUG level logging.')
+        self.add_argument('-V', '--version', action='store_true',
+                          help='Display version string.')
         self.add_argument('-a', '--all-abi', action='store_true',
                           help='Set to run tests for all abi.')
         self.add_argument('--generate-baseline', nargs='?', type=int, const=5, default=0,
diff --git a/atest/constants_default.py b/atest/constants_default.py
index 918bc31..006327b 100644
--- a/atest/constants_default.py
+++ b/atest/constants_default.py
@@ -183,7 +183,7 @@
 # be generated when make.
 ATEST_BUILD_ENV = {'SOONG_COLLECT_JAVA_DEPS':'true'}
 
-# Atest cache root and relative dirs/caches.
+# Atest index path and relative dirs/caches.
 INDEX_DIR = os.path.join(os.getenv(ANDROID_HOST_OUT, ''), 'indexes')
 LOCATE_CACHE = os.path.join(INDEX_DIR, 'mlocate.db')
 INT_INDEX = os.path.join(INDEX_DIR, 'integration.idx')
@@ -192,6 +192,7 @@
 PACKAGE_INDEX = os.path.join(INDEX_DIR, 'packages.idx')
 QCLASS_INDEX = os.path.join(INDEX_DIR, 'fqcn.idx')
 MODULE_INDEX = os.path.join(INDEX_DIR, 'modules.idx')
+VERSION_FILE = os.path.join(os.path.dirname(__file__), 'VERSION')
 
 # Regeular Expressions
 CC_EXT_RE = re.compile(r'.*\.(cc|cpp)$')
diff --git a/atest/run_atest_unittests.sh b/atest/run_atest_unittests.sh
index 09310cf..db28ac5 100755
--- a/atest/run_atest_unittests.sh
+++ b/atest/run_atest_unittests.sh
@@ -15,22 +15,20 @@
 # limitations under the License.
 
 # A simple helper script that runs all of the atest unit tests.
-# We have 2 situations we take care of:
-#   1. User wants to invoke this script by itself.
+# There are 2 situations that we take care of:
+#   1. User wants to invoke this script directly.
 #   2. PREUPLOAD hook invokes this script.
 
-ATEST_DIR=`dirname $0`/
+ATEST_DIR=$(dirname $0)
 [ "$(uname -s)" == "Darwin" ] && { realpath(){ echo "$(cd $(dirname $1);pwd -P)/$(basename $1)"; }; }
-ATEST_REAL_PATH=`realpath $ATEST_DIR`
+ATEST_REAL_PATH=$(realpath $ATEST_DIR)
 RED='\033[0;31m'
 GREEN='\033[0;32m'
 NC='\033[0m' # No Color
 COVERAGE=false
 
-function set_pythonpath() {
-    if ! echo $PYTHONPATH | grep -q $ATEST_REAL_PATH; then
-        PYTHONPATH=$ATEST_REAL_PATH:$PYTHONPATH
-    fi
+function get_pythonpath() {
+    echo "$ATEST_REAL_PATH:$PYTHONPATH"
 }
 
 function print_summary() {
@@ -50,7 +48,6 @@
     echo "Running tests..."
     local run_cmd="python"
     local rc=0
-    set_pythonpath
     if [[ $COVERAGE == true ]]; then
         # Clear previously coverage data.
         python -m coverage erase
@@ -59,7 +56,7 @@
     fi
 
     for test_file in $(find $ATEST_DIR -name "*_unittest.py"); do
-        if ! $run_cmd $test_file; then
+        if ! PYTHONPATH=$(get_pythonpath) $run_cmd $test_file; then
           rc=1
           echo -e "${RED}$t failed${NC}"
         fi
diff --git a/remote/Android.bp b/remote/Android.bp
index 2d23c23..523276f 100644
--- a/remote/Android.bp
+++ b/remote/Android.bp
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-tradefed_java_library_host {
+java_library_host {
   name: "tf-remote-client",
   defaults: ["tradefed_defaults"],
 
diff --git a/remote/Android.mk b/remote/Android.mk
new file mode 100644
index 0000000..7c58fc1
--- /dev/null
+++ b/remote/Android.mk
@@ -0,0 +1,25 @@
+# Copyright (C) 2013 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+# makefile rules to copy jars to HOST_OUT/tradefed
+# so tradefed.sh can automatically add to classpath
+DEST_JAR := $(HOST_OUT)/tradefed/tf-remote-client.jar
+BUILT_JAR := $(call intermediates-dir-for,JAVA_LIBRARIES,tf-remote-client,HOST)/javalib.jar
+$(DEST_JAR): $(BUILT_JAR)
+	$(copy-file-to-new-target)
+
+# this dependency ensure the above rule will be executed if jar is built
+$(HOST_OUT_JAVA_LIBRARIES)/tf-remote-client.jar : $(DEST_JAR)
diff --git a/src/com/android/tradefed/device/NativeDevice.java b/src/com/android/tradefed/device/NativeDevice.java
index f6e6043..d7216b0 100644
--- a/src/com/android/tradefed/device/NativeDevice.java
+++ b/src/com/android/tradefed/device/NativeDevice.java
@@ -3703,6 +3703,11 @@
             String prop = getProperty(DeviceProperties.SDK_VERSION);
             apiLevel = Integer.parseInt(prop);
         } catch (NumberFormatException nfe) {
+            CLog.w(
+                    "Unable to get API level from "
+                            + DeviceProperties.SDK_VERSION
+                            + ", falling back to UNKNOWN.",
+                    nfe);
             // ignore, return unknown instead
         }
         return apiLevel;