Skip to content

Instantly share code, notes, and snippets.

@kamathln
Last active October 13, 2018 23:40
Show Gist options
  • Save kamathln/9e3ea07309e79e90fb6009429510348d to your computer and use it in GitHub Desktop.
Save kamathln/9e3ea07309e79e90fb6009429510348d to your computer and use it in GitHub Desktop.
p4a compile-install-run-debug cycle
#!/bin/bash
# Copyright (c) 2017 Laxminarayan Kamath<[email protected]>
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
# This script eases the compile-install-run-debug cycle of
# python-for-android or Android kivy apps. Once configured and
# executed with proper command line options, it waits for your
# command. Single letter commands (at least for now) fire
# compiling, installing to phone, running on phone, and
# running logcat.
# For detailed help: Visit (strongly recommended)
# https://lankerisms.blogspot.in/2017/07/python-for-android-compile-run-install.html
# Usage:
# p4a-build projectname version [source directory]
name="$1"
VERSION="$2"
argsok=0
if [ $# -eq 2 ]
then
dir="$name"
argsok=1
fi
if [ $# -eq 3 ]
then
dir="$3"
argsok=1
else
dir="src"
argsok=1
fi
if [ $argsok -eq 0 ]
then
echo "Usage: $0 name version [privatedir]"
exit 1
fi
#Settings
if [ -e "$HOME/.p4a-build.conf" ]
then
echo Sourcing Home config
. "$HOME/.p4a-build.conf"
fi
if [ -e "p4a-build.conf" ]
then
echo Sourcing project config
. p4a-build.conf
fi
JAVA_HOME="${JAVA_HOME:="/usr/lib/jvm/java-8-openjdk-amd64/"}"
JDK_HOME="${JAVA_HOME:="/usr/lib/jvm/java-8-openjdk-amd64/"}"
ANT_PATH="${ANT_PATH:="$HOME/.buildozer/android/platform/apache-ant-1.9.4/"}"
NDK_VER="${NDK_VER:=r13b}"
NDK_PATH="${NDK_PATH:="$HOME/.buildozer/android/platform/android-ndk-r13b"}"
SDK_PATH="${SDK_PATH:="$HOME/.buildozer/android/platform/android-sdk-20"}"
API_VER="${API_VER:="23"}"
REQUIREMENTS="${REQUIREMENTS:=kivy}"
BOOTSTRAP="${BOOTSTRAP:=sdl2}"
DOMAIN="${DOMAIN:=kivy.org}"
VERSION="${VERSION:=0.1}"
PERMISSIONS="${PERMISSIONS:-""}"
ORIENTATION="${ORIENTATION:=sensor}"
# End of Settings
if echo "$RELEASE" |egrep -iq '^y'
then
RELEASESTRING="--release"
echo "RELEASE=$RELEASE, so running a release build"
else
RELEASESTRING=""
fi
if [ -e blacklist.txt ]
then
BLACKLISTSTRING="--blacklist $PWD/blacklist.txt"
else
BLACKLISTSTRING=""
fi
if [ -n "$PERMISSIONS" ]
then
PERMISSIONSTRING="$(echo ${PERMISSIONS/,/ }|xargs -l printf " --permission android.permission.%s" )"
else
PERMISSIONSTRING=""
fi
echo BOOTSTRAP=$BOOTSTRAP
echo REQUIREMENTS=$REQUIREMENTS
export JDK_HOME JAVA_HOME ANT_PATH NDK_VER NDK_PATH SDK_PATH API_VER REQUIREMENTS BOOTSTRAP DOMAIN VERSION
echo "--------------------------------8<-------------------------8<-------------------------"
apkname="$name-$VERSION-debug.apk"
apkfile="$HOME/.local/share/python-for-android/dists/$name/bin/$apkname"
response=""
if [ -z "$RELEASESTRING" ]
then
while [ ! "$response" == "q" ]
do
echo
echo c to Compile
echo l to list private.mp3 contents from "$apkname"
echo i to install "$apkname to phone"
echo r to run installed app
echo d to run logcat
echo q to quit
echo -n '> '
read response
echo
case "$response" in
c)
python -m compileall -q "$dir"
if [ $? -ne 0 ]
then
echo -e "\nERRORS WERE DETECTED during compilation\n"
else
echo -e "Local compilation successfull"
echo running "PATH=\"$JAVA_HOME/bin:$ANT_PATH/bin:$PATH\"" python3.6 "`which p4a`" apk --ndk-version="$ANT_PATH" --arch=armeabi-v7a --sdk-dir="$SDK_PATH" --ndk-dir="$NDK_PATH" --android-api="$API_VER" --requirements="$REQUIREMENTS" --bootstrap "$BOOTSTRAP" --name "$name" --package "$DOMAIN"."$name" --version "$VERSION" --private "$dir" --dist-name "$name" $RELEASESTRING $BLACKLISTSTRING \" "$PERMISSIONSTRING" --orientation "$ORIENTATION" --debug --force-build
PATH="$JAVA_HOME/bin:$ANT_PATH/bin:$PATH" python3.6 "`which p4a`" apk --ndk-version="$ANT_PATH" --arch=armeabi-v7a --sdk-dir="$SDK_PATH" --ndk-dir="$NDK_PATH" --android-api="$API_VER" --requirements="$REQUIREMENTS" --bootstrap "$BOOTSTRAP" --name "$name" --package "$DOMAIN"."$name" --version "$VERSION" --private "$dir" --dist-name "$name" $RELEASESTRING $BLACKLISTSTRING $PERMISSIONSTRING --orientation "$ORIENTATION" --debug --force-build
fi
echo "--------------------------------8<-------------------------8<-------------------------"
ls -lth "$apkfile"
echo "--------------------------------8<-------------------------8<-------------------------"
;;
l)
echo running: "unzip -p \"$apkfile\" assets/private.mp3 | tar -zt"
unzip -p "$apkfile" assets/private.mp3 | tar -zt
;;
i)
echo running: "adb install -r \"$apkfile\""
adb install -r "$apkfile"
;;
r)
echo running: "adb shell am start ${domain}.${name}/org.kivy.android.PythonActivity"
adb shell am start "${DOMAIN}.${name}/org.kivy.android.PythonActivity"
;;
d)
echo "running: adb logcat -bmain -d |egrep '(python|ActivityManager|PythonActivity|SDL).*:'"
adb logcat -bmain -d |egrep '(python|ActivityManager|PythonActivity|SDL).*:'
;;
q)
echo quitting
;;
*)
echo 'unknown option!'
esac
done
else
echo running "PATH=\"$JAVA_HOME/bin:$ANT_PATH/bin:$PATH\"" python3.6 "`which p4a`" apk --ndk-version="$ANT_PATH" --arch=armeabi-v7a --sdk-dir="$SDK_PATH" --ndk-dir="$NDK_PATH" --android-api="$API_VER" --requirements="$REQUIREMENTS" --bootstrap "$BOOTSTRAP" --name "$name" --package "$DOMAIN"."$name" --version "$VERSION" --private "$dir" --dist-name "$name" $RELEASESTRING $BLACKLISTSTRING --force-build\" "$PERMISSIONSTRING" --orientation "$ORIENTATION" --debug --force-build
PATH="$JAVA_HOME/bin:$ANT_PATH/bin:$PATH" python3.6 "`which p4a`" apk --ndk-version="$ANT_PATH" --arch=armeabi-v7a --sdk-dir="$SDK_PATH" --ndk-dir="$NDK_PATH" --android-api="$API_VER" --requirements="$REQUIREMENTS" --bootstrap "$BOOTSTRAP" --name "$name" --package "$DOMAIN"."$name" --version "$VERSION" --private "$dir" --dist-name "$name" $RELEASESTRING $BLACKLISTSTRING $PERMISSIONSTRING --orientation "$ORIENTATION" --debug --force-build
ls -lth "$HOME/.local/share/python-for-android/dists/$name/bin/"*.apk
echo "--------------------------------8<-------------------------8<-------------------------"
fi
@jleaders
Copy link

Looks similar to what buildozer does

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