Skip to content

Instantly share code, notes, and snippets.

@rokibhasansagar
Last active December 26, 2023 08:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rokibhasansagar/5d2f71af6e31e60fb6df2446132d4e8d to your computer and use it in GitHub Desktop.
Save rokibhasansagar/5d2f71af6e31e60fb6df2446132d4e8d to your computer and use it in GitHub Desktop.
Android ROM Build Environment Setup Helper on GitHub Actions macOS SSH using https://github.com/Area69Lab/macOS-SSH

Cleanup Some Space First

{
  echo "will cite" | parallel --citation
} &>/dev/null
parallel --jobs 200% sudo rm -rf {} 2>/dev/null ::: ~/hostedtoolcache/*
brew update &>/dev/null
brew uninstall -q --force --zap --cask chromedriver firefox google-chrome julia microsoft-auto-update microsoft-edge session-manager-plugin r soundflower 2>/dev/null
brew uninstall -q --force --zap aliyun-cli ant aspell aws-sam-cli azure-cli bazelisk carthage composer fontconfig freetds freetype gcc@8 gd geckodriver gh gradle helm httpd hub jpeg libpq libtiff llvm maven mongodb-community mongodb-database-tools nginx node@14 openjdk packer php pipx postgresql python@3.8 rustup-init selenium-server-standalone subversion tidy-html5 unixodbc webp switchaudio-osx sox go ruby@2.7 2>/dev/null
brew upgrade 2>/dev/null
. ~/.bashrc &>/dev/null || true
brew cleanup -s && rm -rf $(brew --cache)

Cleanup Older XCodes

Note: The /Applications Volume seems to have slow I/O. So it will take much time to delete them.

Some XCode is outdated in macOS VM in GitHub Actions, so it needs to be cleaned and updated.

cd /System/Volumes/Data/Applications/
{
  echo "will cite" | parallel --citation
} &>/dev/null
USED_XCODE=$(ls -lAog Xcode.app | awk -F'/' '{print $NF}')
for i in Xcode_10*.app Xcode_11*.app Xcode_12*.app; do
  if [ $i != "$USED_XCODE" ]; then
    printf "Removing %s...\n" "$i"
    parallel --use-cpus-instead-of-cores --jobs 200% sudo rm -rf {} ::: ${i}/Contents/Developer/Platforms/*
    parallel --use-cpus-instead-of-cores --jobs 200% sudo rm -rf {} ::: ${i}/Contents/*
    sudo rm -rf ${i}
  fi
done
cd -

Cleanup Mono & Xamarin Frameworks and Visual Studio

parallel --jobs 200% sudo rm -rf {} 2>/dev/null ::: "/System/Volumes/Data/Applications/Visual Studio.app" ::: /Library/Frameworks/Mono.framework ::: /Library/Frameworks/Xamarin.Android.framework ::: /Library/Frameworks/Xamarin.Mac.framework ::: /Library/Frameworks/Xamarin.iOS.framework

Cleanup CoreSimulator Caches

parallel --jobs 200% sudo rm -rf {} 2>/dev/null ::: /Users/runner/Library/Developer/CoreSimulator/Caches/dyld/*

Fix MacOSX-SDKs

Older macOS SDKs might be needed for build, so we download them inside latest XCode.

cd /System/Volumes/Data/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
for i in 10 11 12 13 14 15; do
  printf "Fetching MacOSX10.%s SDK...\n" "$i"
  aria2c -x8 -s16 --console-log-level=warn --summary-interval=0 --check-certificate=false "https://github.com/phracker/MacOSX-SDKs/releases/download/11.0-11.1/MacOSX10.$i.sdk.tar.xz"
  printf "Placing MacOSX10.%s.sdk...\n" "$i"
  tar xJf MacOSX10.$i.sdk.tar.xz
  rm -f MacOSX10.$i.sdk.tar.xz
done

Mandatory: Install GNU Utils to replace macOS tools

brew update &>/dev/null
brew install -f -q watch wget wdiff gdb autoconf coreutils binutils diffutils ed findutils gawk gnu-indent gnu-sed gnu-tar gnu-which grep gzip screen bash emacs gpatch less m4 make nano bison flex libressl file-formula git openssh perl python rsync unzip vim
curl -sL https://github.com/fabiomaia/linuxify/raw/master/.linuxify > ~/.linuxify
echo ". ~/.linuxify" >> ~/.bashrc && echo ". ~/.linuxify" >> ~/.zshrc
brew cleanup -s && rm -rf $(brew --cache)

Optional: Install openjdk and ccache

brew install openjdk@8
sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk
export PATH="/usr/local/opt/openjdk@8/bin:$PATH"
# We are setting 12 out of 14 gigabytes of ram for java heap
export JAVA_OPTS=" -Xmx12G "
brew install ccache
export PATH="/usr/local/opt/ccache/libexec:$PATH"

Setting Up Build Environment on macOS in SSH

# Create Case-sensitive SPARSE filesystem, we are creating a filesystem image of 220 gigabytes
sudo hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 220g ~/android.dmg.sparseimage

# Mount the android file image
sudo hdiutil attach ~/android.dmg.sparseimage -mountpoint /Volumes/android

Prepare Repo

# Change Directory to android volume
cd /Volumes/android

# Download repo binary
mkdir -p ~/bin 2>/dev/null
export PATH="~/bin:/usr/local/bin:$PATH"
curl -sL https://gerrit.googlesource.com/git-repo/+/refs/heads/stable/repo?format=TEXT | base64 --decode > ~/bin/repo
chmod a+x ~/bin/repo

# Set git config for authorizing repo, Use your github email address and name
git config --global user.email ${GitHubMail}
git config --global user.name ${GitHubName}
git config --global color.ui true

Repo Init and Sync and Beyond

# Choose your repo manifest (Example: AOSP master branch, shallow)
repo init -u https://android.googlesource.com/platform/manifest --depth=1

# Sync it, make sure to minimize outputs here and while building rom
# We are using 6 parallel threads for syncing
repo sync -c -q --force-sync --no-clone-bundle --no-tags -j6

Build

Well, you know the rest commands.

Keepalive SSH Sessions

Run and add this snippet into the Server before you enter into it. It will try to keep your session alive all the time.

mkdir -p ~/.ssh
cat << EOF > ~/.ssh/config
Host *
    ServerAliveInterval 60
    StrictHostKeyChecking no
Host github.com
    User git
    Port 22
    Hostname github.com
    TCPKeepAlive yes
    IdentitiesOnly yes
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment