1#!/bin/bash
2# Copyright (c) Meta Platforms, Inc. and affiliates.
3#
4# This source code is licensed under the MIT license found in the
5# LICENSE file in the root directory of this source tree.
6
7# This script validates that Android is set up correctly for the
8# testing environment.
9#
10# In particular, the config in ReactAndroid/build.gradle must match
11# the android sdk that is actually installed. Also, we must have the
12# right version of Java.
13
14# Check that Buck is working.
15if [ -z "$(which buck)" ]; then
16  echo "You need to install Buck."
17  echo "See https://buckbuild.com/setup/getting_started.html for instructions."
18  exit 1
19fi
20
21if [ -z "$(buck --version)" ]; then
22  echo "Your Buck install is broken."
23
24  if [ -d "/opt/facebook" ]; then
25    BUCK_SUGGESTED_COMMIT_FB="b9b76a3a5a086eb440a26d1db9b0731875975099"
26    echo "FB laptops ship with a Buck config that is not compatible with open "
27    echo "source. FB Buck requires the environment to set a buck version, but "
28    echo "the open source version of Buck forbids that."
29    echo
30    echo "You can try setting:"
31    echo
32    echo "export BUCKVERSION=${BUCK_SUGGESTED_COMMIT_FB}"
33    echo
34    echo "in your .bashrc or .bash_profile to fix this."
35    echo
36    echo "If you don't want to alter BUCKVERSION for other things running on"
37    echo "your machine, you can just scope it to a single script, for example"
38    echo "by running something like:"
39    echo
40    echo "BUCKVERSION=${BUCK_SUGGESTED_COMMIT_FB} $0"
41    echo
42  else
43    echo "I don't know what's wrong, but calling 'buck --version' should work."
44  fi
45  exit 1
46else
47  BUCK_EXPECTED_VERSION="buck version d743d2d0229852ce7c029ec257532d8916f6b2b7"
48  if [ "$(buck --version)" != "$BUCK_EXPECTED_VERSION" ]; then
49    if [ ! -d "/opt/facebook" ]; then
50      echo "Warning: The test suite expects ${BUCK_EXPECTED_VERSION} to be installed"
51    fi
52  fi
53fi
54
55# MAJOR is something like "23"
56MAJOR=`grep compileSdkVersion $(dirname $0)/../ReactAndroid/build.gradle | sed 's/[^[:digit:]]//g'`
57
58# Check that we have the right major version of the Android SDK.
59PLATFORM_DIR="$ANDROID_HOME/platforms/android-$MAJOR"
60if [ ! -e "$PLATFORM_DIR" ]; then
61  echo "Error: could not find version $ANDROID_VERSION of the Android SDK."
62  echo "Specifically, the directory $PLATFORM_DIR does not exist."
63  echo "You probably need to specify the right version using the SDK Manager from within Android Studio."
64  echo "See https://reactnative.dev/docs/getting-started.html for details."
65  echo "If you are using Android SDK Tools from the command line, you may need to run:"
66  echo
67  echo "  sdkmanager \"platform-tools\" \"platform-tools;android-$MAJOR\""
68  echo
69  echo "Check out https://developer.android.com/studio/command-line/sdkmanager.html for details."
70  exit 1
71fi
72
73# Check that we have the right version of the build tools.
74BT_DIR="$ANDROID_HOME/build-tools/$ANDROID_SDK_BUILD_TOOLS_REVISION"
75if [ ! -e "$BT_DIR" ]; then
76  echo "Error: could not find version $ANDROID_SDK_BUILD_TOOLS_REVISION of the Android build tools."
77  echo "Specifically, the directory $BT_DIR does not exist."
78  echo "You probably need to explicitly install the correct version of the Android SDK Build Tools from within Android Studio."
79  echo "See https://reactnative.dev/docs/getting-started.html for details."
80  echo "If you are using Android SDK Tools from the command line, you may need to run:"
81  echo
82  echo "  sdkmanager \"platform-tools\" \"build-tools;android-$ANDROID_SDK_BUILD_TOOLS_REVISION\""
83  echo
84  echo "Check out https://developer.android.com/studio/command-line/sdkmanager.html for details."
85  exit 1
86fi
87
88if [ -n "$(which csrutil)" ]; then
89  # This is a SIP-protected machine (recent OSX).
90  # Check that we are not using SIP-protected Java.
91  JAVA=`which java`
92  if [ "$JAVA" = "/usr/bin/java" ]; then
93    echo "Error: we can't use this Java version."
94    echo "Currently, Java runs from $JAVA."
95    echo "The operating-system-provided Java doesn't work with React Native because of SIP protection."
96    echo "Please install the Oracle Java Development Kit 8."
97    if [ -d "/opt/facebook" ]; then
98      echo "See https://our.intern.facebook.com/intern/dex/installing-java-8/ for instructions on installing Java 8 on FB laptops."
99    else
100      echo "Check out http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ."
101      echo "Be sure that you set JAVA_HOME and PATH correctly in your .bashrc or equivalent. Example:"
102      echo
103      echo "  export JAVA_HOME=path/to/java"
104      echo "  export PATH=\$PATH:\$JAVA_HOME/bin"
105      echo
106    fi
107    echo "After installing Java, run 'buck kill' and 'buck clean'."
108    exit 1
109  fi
110fi
111
112if [ -z "$JAVA_HOME" ]; then
113  echo "Error: \$JAVA_HOME is not configured."
114  echo "Try adding export JAVA_HOME=\$(/usr/libexec/java_home) to your .bashrc or equivalent."
115  echo "You will also want to add \$JAVA_HOME/bin to your path."
116  exit 1
117fi
118