Skip to content

Instantly share code, notes, and snippets.

@J-Swift
Last active April 11, 2022 01:19
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save J-Swift/539aaf82ffac21d5515274a24379667f to your computer and use it in GitHub Desktop.
Save J-Swift/539aaf82ffac21d5515274a24379667f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
##########
# Config #
##########
readonly GIT_BRANCH='android-8.0.0_r4'
readonly API_LEVEL='26'
###########
# Helpers #
###########
readonly CUR_DIR=$( pwd )
readonly WORKING_DIR=$( mktemp -d )
setup() {
pushd ${WORKING_DIR}
mkdir -p frameworks/base
}
teardown() {
popd
rm -rf "${WORKING_DIR}"
}
fetch_sources() {
local -r target_branch="${1}"
# Fetch repositories that contain the sources we're interested in
git clone --depth 1 https://android.googlesource.com/platform/frameworks/base -b "${target_branch}" frameworks/base
git clone --depth 1 https://android.googlesource.com/platform/libcore -b "${target_branch}"
git clone --depth 1 https://android.googlesource.com/platform/development -b "${target_branch}"
}
package_sources() {
local -r zip_name="${1}"
# Create a basic source.properties file
echo -e "Pkg.UserSrc=false\nPkg.Revision=1\nAndroidVersion.ApiLevel=${API_LEVEL}" > source.properties
# Modify the script to create a sources ZIP to use "android-26" as top-level directory
cat development/build/tools/mk_sources_zip.py | sed -e "s/TOP_FOLDER = .*/TOP_FOLDER = \"android-${API_LEVEL}\"/" > my_mk_sources_zip.py
# Run the script to create android-26-sources.zip
python my_mk_sources_zip.py -z source.properties "${zip_name}" .
}
maybe_install_sources() {
local -r zip_name="${1}"
local -r sources_dir="${ANDROID_HOME}/sources"
local -r target_path="${sources_dir}/android-${API_LEVEL}"
if [ -d "${target_path}" ]; then
echo ""
echo "WARN: directory exists [${target_path}] copying instead..."
local -r zip_path="${CUR_DIR}/${zip_name}"
if [ -f "${zip_path}" ]; then
echo "WARN: nevermind, file exists [${zip_path}] skipping..."
else
cp "${zip_name}" "${CUR_DIR}/"
fi
else
unzip "${zip_name}" -d "${sources_dir}"
fi
}
###############
# DO WORK SON #
###############
main() {
local -r zip_name="android-${API_LEVEL}-sources.zip"
setup
fetch_sources "${GIT_BRANCH}"
package_sources "${zip_name}"
maybe_install_sources "${zip_name}"
teardown
}
main
@strooooke
Copy link

strooooke commented Sep 22, 2017

I had to change the first line to #!/usr/bin/env bash (set -o pipefail is bash-specific). Also: love it, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment