droid (3002B)
1 #!/bin/sh 2 # Sane Android development. 3 4 BASEDIR=~/.android-toolkit 5 [ -n "$DROID_BASEDIR" ] && BASEDIR="$DROID_BASEDIR" 6 SDKDIR="$BASEDIR/tools" 7 ADB="$BASEDIR/platform-tools/adb" 8 SDK_URL=https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip 9 GRADLE="$BASEDIR/gradle-2.10" 10 GRADLE_URL="https://services.gradle.org/distributions/gradle-2.10-bin.zip" 11 12 # Remove the need to have sdk.dir in local.properties 13 [ ! -f "local.properties" ] && export ANDROID_HOME="$BASEDIR" 14 15 export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" 16 17 _adb() { 18 pids="$(pgrep -f ^adb)" 19 if [ ${#pids} -eq 0 ]; then 20 echo "WARNING: ADB server is not running, start it with: sudo droid adb start-server." 21 fi 22 apk="./build/outputs/apk/$(basename $(pwd))-debug.apk" 23 $ADB "$@" "$apk" 24 } 25 26 do_setup() { 27 echo "Setting up the development environment..." 28 29 [ ! -d "$BASEDIR" ] && mkdir -p "$BASEDIR" 30 if [ -d "$SDKDIR" ]; then 31 echo "SDK already installed. Skipping..." 32 else 33 echo "Setting up the SDK..." 34 f=/tmp/sdk.zip 35 wget -O "$f" -qc "$SDK_URL" 36 unzip -qq "$f" -d "$BASEDIR" 37 echo "Cleaning up..." 38 rm -f "$f" 39 fi 40 if [ -d "$GRADLE" ]; then 41 echo "Gradle already installed. Skipping..." 42 else 43 echo "Setting up the Gradle build system..." 44 f=/tmp/gradle.zip 45 wget -O "$f" -qc "$GRADLE_URL" 46 unzip -qq "$f" -d "$BASEDIR" 47 echo "Cleaning up..." 48 rm -f "$f" 49 fi 50 cd "$OLDPWD" 51 52 echo "Installing platform tools and system image (android-28)..." 53 # https://stackoverflow.com/a/47346176/508196 54 #JAVA_OPTS="--add-modules java.xml.bind" 55 $SDKDIR/bin/sdkmanager "platform-tools" "platforms;android-28" "build-tools;28.0.3" 56 57 # Avoid sdklib.repository.AndroidSdkHandler class initialization error 58 #export JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64" 59 echo "done." 60 } 61 62 do_install() { 63 _adb install -r 64 } 65 66 do_uninstall() { 67 # XXX Actually doesn't works (Failure) 68 _adb uninstall 69 } 70 71 # XXX real device or emulator 72 do_upload() { 73 # _adb push 74 echo 'To be implemented.' 75 } 76 77 do_build() { 78 rel=$1 79 [ $rel ] && task="assembleRelease" || task="assembleDebug" 80 $GRADLE/bin/gradle $task 81 } 82 83 do_upgrade() { 84 do_build 85 do_install 86 } 87 88 do_clean() { 89 rm -rf build .gradle 90 } 91 92 do_deploy() { 93 do_build 1 94 do_install 95 } 96 97 usage() { 98 echo "Usage: $(basename "$0") <cmd>" 99 echo " 100 Commands: 101 adb\t\t: adb pass-through (run ADB commands) 102 setup\t: creates the development environment 103 install\t: install the app into the device 104 uninstall\t: uninstall the app from the device (broken) 105 upload\t: upload the app into the device 106 build\t: build the app (if no task, assumes assembleDebug) 107 upgrade\t: build and install the app (assembleDebug) 108 deploy\t: build and install the app (assembleRelease) 109 clean\t: removes the builds from the current directory 110 " 111 exit 1 112 } 113 114 main() { 115 case $1 in 116 adb) shift; _adb $@;; 117 setup) do_setup;; 118 install) do_install;; 119 uninstall) do_uninstall;; 120 upload) do_upload;; 121 build) do_build $2;; 122 upgrade) do_upgrade;; 123 deploy) do_deploy;; 124 clean) do_clean;; 125 *) usage;; 126 esac 127 } 128 129 main "$@"