Repository: bazelbuild/bazel
Issue: Android, Windows: mobile-install fails with Python3
Labels: type: bug
P2
team-Android

### Description of the problem / feature request:

> Replace this line with your answer.

### Feature requests: what underlying problem are you trying to solve with this feature?

> Replace this line with your answer.

### Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Follow the Android Tutorial on bazel.build until "Run the App": https://docs.bazel.build/versions/master/tutorial/android-app.html#run-the-app

If you have Python3 on the `PATH`, it will fail like so:

```
C:\src\bazel-examples\android\tutorial>bazel mobile-install //src/main:app
INFO: Reading 'startup' options from c:\users\laszlocsomor\.bazelrc: --output_user_root=c:/_bazel
INFO: Options provided by the client:
  Inherited 'common' options: --isatty=1 --terminal_columns=95
INFO: Options provided by the client:
  Inherited 'build' options: --python_path=c:/Python37/python.exe
INFO: Reading rc options for 'mobile-install' from c:\users\laszlocsomor\.bazelrc:
  Inherited 'build' options: --verbose_failures --announce_rc
INFO: Analysed target //src/main:app (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: C:/src/bazel-examples/android/tutorial/src/main/BUILD:1:1: Injecting mobile install stub application failed (Exit 1): stubify_manifest.exe failed: error executing command
  cd C:/_bazel/t5swfuxe/execroot/__main__
bazel-out/host/bin/external/bazel_tools/tools/android/stubify_manifest.exe --mode=mobile_install --input_manifest bazel-out/x64_windows-fastbuild/bin/src/main/_merged/app/AndroidManifest.xml --output_manifest bazel-out/x64_windows-fastbuild/bin/src/main/app_files/mobile_install/AndroidManifest.xml --output_datafile bazel-out/x64_windows-fastbuild/bin/src/main/app_files/stub_application_data/stub_application_data.txt
Execution platform: @bazel_tools//platforms:host_platform
Traceback (most recent call last):
  File "\\?\c:\tmp\Bazel.runfiles_j8q3jcn7\runfiles\bazel_tools\tools\android\stubify_manifest.py", line 167, in <module>
    main()
  File "\\?\c:\tmp\Bazel.runfiles_j8q3jcn7\runfiles\bazel_tools\tools\android\stubify_manifest.py", line 154, in main
    output_file.write("\n".join([old_application, app_package]))
TypeError: a bytes-like object is required, not 'str'
Target //src/main:app failed to build
INFO: Elapsed time: 2.607s, Critical Path: 1.37s
INFO: 7 processes: 5 local, 2 worker.
FAILED: Build did NOT complete successfully
```

It works with Python2.

### What operating system are you running Bazel on?

Windows 10

### What's the output of `bazel info release`?

`release 0.23.0`

### What's the output of `git remote get-url origin ; git rev-parse master ; git rev-parse HEAD` ?

```
$ git remote get-url origin ; git rev-parse master ; git rev-parse HEAD
git@github.com:bazelbuild/examples.git
b76d50e6719fe76cc6190d73aabb88822b71c4a5
b76d50e6719fe76cc6190d73aabb88822b71c4a5
```

Issue comments:
Similar error in `incremental_install.py`:
```
  File "\\?\c:\tmp\Bazel.runfiles_fbx84su6\runfiles\bazel_tools\tools\android\incremental_install.py", line 839, in <module>
    main()
  File "\\?\c:\tmp\Bazel.runfiles_fbx84su6\runfiles\bazel_tools\tools\android\incremental_install.py", line 828, in main
    extra_adb_args=FLAGS.extra_adb_arg)
  File "\\?\c:\tmp\Bazel.runfiles_fbx84su6\runfiles\bazel_tools\tools\android\incremental_install.py", line 756, in IncrementalInstall
    UploadDexes(adb, execroot, app_dir, temp_dir, dexmanifest, bool(apk))
  File "\\?\c:\tmp\Bazel.runfiles_fbx84su6\runfiles\bazel_tools\tools\android\incremental_install.py", line 367, in UploadDexes
    adb.Mkdir(dex_dir)
  File "\\?\c:\tmp\Bazel.runfiles_fbx84su6\runfiles\bazel_tools\tools\android\incremental_install.py", line 288, in Mkdir
    self._Shell("mkdir -p %s" % d)
  File "\\?\c:\tmp\Bazel.runfiles_fbx84su6\runfiles\bazel_tools\tools\android\incremental_install.py", line 312, in _Shell
    return self._Exec(["shell", cmd])
  File "\\?\c:\tmp\Bazel.runfiles_fbx84su6\runfiles\bazel_tools\tools\android\incremental_install.py", line 182, in _Exec
    elif MultipleDevicesError.CheckError(stderr):
  File "\\?\c:\tmp\Bazel.runfiles_fbx84su6\runfiles\bazel_tools\tools\android\incremental_install.py", line 102, in CheckError
    return re.search("more than one (device and emulator|device|emulator)", s)
  File "c:\Python37\lib\re.py", line 183, in search
    return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object
Target //src/main:app failed to build
INFO: Elapsed time: 6.184s, Critical Path: 5.19s
INFO: 1 process: 1 local.
FAILED: Build did NOT complete successfully
```

Pull request review comments:

Patch that resolved the issue:
diff --git a/tools/android/incremental_install.py b/tools/android/incremental_install.py
index 0638c1b34a4235..d7b9c48d7f29cc 100644
--- a/tools/android/incremental_install.py
+++ b/tools/android/incremental_install.py
@@ -173,6 +173,8 @@ def _Exec(self, adb_args):
 
     # Check these first so that the more specific error gets raised instead of
     # the more generic AdbError.
+    stdout = stdout.decode()
+    stderr = stderr.decode()
     if "device not found" in stderr:
       raise DeviceNotFoundError()
     elif "device unauthorized" in stderr:
diff --git a/tools/android/stubify_manifest.py b/tools/android/stubify_manifest.py
index d07287e3fdf201..f2e8a48f0fe89e 100644
--- a/tools/android/stubify_manifest.py
+++ b/tools/android/stubify_manifest.py
@@ -151,7 +151,7 @@ def main():
       output_xml.write(new_manifest)
 
     with open(FLAGS.output_datafile, "wb") as output_file:
-      output_file.write("\n".join([old_application, app_package]))
+      output_file.write("\n".join([old_application, app_package]).encode())
 
   elif FLAGS.mode == "instant_run":
     with open(FLAGS.input_manifest, "rb") as input_manifest:


Test patch: