Skip to content

Instantly share code, notes, and snippets.

@eveliotc
Last active February 5, 2019 06:10
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eveliotc/7b86bbb8645dcb99d993 to your computer and use it in GitHub Desktop.
Save eveliotc/7b86bbb8645dcb99d993 to your computer and use it in GitHub Desktop.
My (android) dev aliases and scripts
#! /bin/bash
# from http://dtmilano.blogspot.com/2012/03/selecting-adb-device.html
# selects an android device
PROGNAME=$(basename $0)
UNAME=$(uname)
DEVICE_OPT=
for opt in "$@"
do
case "$opt" in
-d|-e|-s)
DEVICE_OPT=$opt
;;
esac
done
[ -n "$DEVICE_OPT" ] && exit 0
DEV=$(adb devices 2>&1 | tail -n +2 | sed '/^$/d')
if [ -z "$DEV" ]
then
echo "$PROGNAME: ERROR: There's no connected devices." >&2
exit 1
fi
N=$(echo "$DEV" | wc -l | sed 's/ //g')
case $N in
1)
# only one device detected
D=$DEV
;;
*)
# more than one device detected
OLDIFS=$IFS
IFS="
"
PS3="Select the device to use, <Q> to quit: "
select D in $DEV
do
[ "$REPLY" = 'q' -o "$REPLY" = 'Q' ] && exit 2
[ -n "$D" ] && break
done
IFS=$OLDIFS
;;
esac
if [ -z "$D" ]
then
echo "$PROGNAME: ERROR: target device coulnd't be determined" >&2
exit 1
fi
# this didn't work on Darwin
# echo "-s ${D%% *}"
echo "-s $(echo ${D} | sed 's/ .*$//')"
# This should be included from your ~/.zshrc or ~/.bash_profile like
# source ~/some/path/common.sh
# Requires to be preinstalled for certain functions/aliases: git, ack, ditto, imagemagick, jq, icdiff
# Aliases
alias g='git'
alias status='git status'
alias cutediff='git icdiff'
alias cutelog="git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
alias log='git log'
alias newfeature='git checkout -b'
alias develop='git checkout develop'
alias master='git checkout master'
alias amend='git commit --amend'
alias commit='git commit -m'
alias add-all='git add .'
alias rebase='git rebase'
alias deletelocalbranch='git branch -d'
alias changebranch='git checkout'
alias cdapp='cd '$MAIN_APP_PATH
alias resditto='ditto -V res '$MAIN_APP_PATH'/app/res'
alias cdlibrary='cd '$MAIN_LIB_PATH
# requires my-adb script below
alias adb='my-adb'
alias gc='./gradlew clean'
alias gcad='./gradlew clean assembleDebug'
alias gcar='./gradlew clean assembleRelease'
myforkstr() {
git remote show -n $GITHUB_USERNAME | ack Push | ack -o '/(.*?).git' | sed -e '1s/^.//' -e 's/\.git$//g'
}
currentbranchstr()
{
git rev-parse --abbrev-ref HEAD;
}
_github-projects() {
git remote -v | grep fetch | ack -o 'github.com:(.*?)/.*\.' | sed -e 's/.$//g' -e 's/:/\//g' | awk '{print "https://"$1}';
}
_as_open_menu() {
n="$#";
args=("$@") ;
case $n in
0)
return 1;
;;
1)
selection="$1";
;;
*)
OLDIFS=$IFS
IFS="
"
PS3="Select an option to open, <Q> to quit: "
select selection in $args
do
[ "$REPLY" = 'q' -o "$REPLY" = 'Q' ] && return 2
[ -n "$selection" ] && break
done
IFS=$OLDIFS
;;
esac
if [ -z "$selection" ]
then
return 1;
fi
open $selection
}
projectpage() {
remotes=(`_github-projects`);
_as_open_menu $remotes;
}
pullrequest () {
currentBranch=`currentbranchstr`;
echo "Pushing to "$GITHUB_USERNAME"/"$currentBranch
git push $GITHUB_USERNAME $currentBranch
myfork=`myforkstr`;
prUrl="https://github.com/"$GITHUB_USERNAME"/"$myfork"/pull/new/"$currentBranch
echo "Opening in browser "$prUrl
open $prUrl
}
finishfeature () {
currentBranch=`currentbranchstr`;
develop
echo "Fetching "$MAIN_REMOTE_NAME
git fetch $MAIN_REMOTE_NAME
echo "Pulling "$MAIN_REMOTE_NAME" develop"
git pull $MAIN_REMOTE_NAME develop
echo "Removing feature branch "$currentBranch
deletelocalbranch $currentBranch
echo "Pushing to "$GITHUB_USERNAME" develop"
git push $GITHUB_USERNAME develop
}
function gi() { curl http://www.gitignore.io/api/$@ ;}
alias gitignore='gi'
# adb stuff
adb-screenshot () {
if [[ -z "$1" ]]; then
echo "You must provide an image file path.";
return 1;
fi
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > "$1"
}
# requests
get () {
if [[ -z "$1" ]]; then
echo "You must provide an URL to GET.";
return 1;
fi
curl -s "$1" | jq '.'
}
alias GET='get'
# resources
rmresource () {
if [[ -z "$1" ]]; then
echo "You must provide a resource id, name or pattern.";
return 1;
fi
RESID="$1"
RESDIR="res";
if [[ -d "$2" ]]; then
RESDIR="$2";
fi
if [[ ! -d "$RESDIR" ]]; then
echo "res directory not found.";
return 1;
fi
echo "Using resource directory "$RESDIR;
echo "Finding resources like "$RESID;
for item in $( find $RESDIR -type f -maxdepth 2 -iname "*"$RESID"*" ); do
rm -iv $item
done
}
# navigation
# Up from http://daniele.livejournal.com/76011.html
function up()
{
dir=""
if [ -z "$1" ]; then
dir=..
elif [[ $1 =~ ^[0-9]+$ ]]; then
x=0
while [ $x -lt ${1:-1} ]; do
dir=${dir}../
x=$(($x+1))
done
else
dir=${PWD%/$1/*}/$1
fi
cd "$dir";
}
function upstr()
{
echo "$(up "$1" && pwd)";
}
#!/usr/bin/python
import sys, subprocess, os
APKS_PATH="/data/app/"
UNZIP_DIR_SUFFIX="-unzip/"
def shell(args):
return subprocess.Popen(args, stdout=subprocess.PIPE, universal_newlines=True).communicate()[0]
def listPackages():
lines = shell(["adb", "shell", "su", "-c", "ls " + APKS_PATH]).split(os.linesep)
result = []
for line in lines:
if "apk" in line:
result.append(line)
return result
def listPackagesCli():
print "List of packages installed"
for package in listPackages():
print package.replace(".apk", "").split("-")[0]
def pull(packageFile):
print "Pulling " + packageFile + "..."
print shell(["adb", "pull", APKS_PATH + packageFile])
def decompile(packageFile):
print "Decompiling " + packageFile + "..."
print shell(["apktool", "d", packageFile])
def getUnzipDir(packageFile):
return packageFile + UNZIP_DIR_SUFFIX;
def unzip(packageFile):
print "Unzipping " + packageFile + "..."
targetDir = getUnzipDir(packageFile)
os.makedirs(targetDir)
print shell(["unzip", packageFile, "-d", targetDir])
def undex(packageFile):
print "Undexing " + packageFile + "..."
print shell(["dex2jar.sh", packageFile])
def all(package, dir):
print "Decompiling " + package + " in " + dir
os.makedirs(dir)
os.chdir(dir)
decompiled = 0
for packageFile in listPackages():
if package in packageFile:
pull(packageFile)
decompile(packageFile)
unzip(packageFile)
undex(packageFile)
decompiled += 1
if (decompiled == 0):
print "No package " + package + " found."
else:
shell(["cd", "'" + dir + "'"])
shell(["open", "."])
print "Done :D"
def main():
argLen = len(sys.argv)
if argLen < 2:
listPackagesCli()
return 1
package = sys.argv[1]
if argLen == 2:
dir = package
else:
dir = sys.argv[2]
all(package, dir)
if __name__ == "__main__":
sys.exit(main())
#!/bin/zsh
# Requires ImageMagick
DPI="$1"
if [[ -z "$1" ]]; then
DPI='xhdpi'
echo "$(tput setaf 3)WARN: Assuming $DPI$(tput sgr0)"
fi
typeset -A densities
densities=( "xxxhdpi" 4.0 "xxhdpi" 3.0 "xhdpi" 2.0 "hdpi" 1.5 "mdpi" 1.0 )
ratio=$densities[$DPI]
if [[ -z "$ratio" ]]; then
echo "$(tput setaf 1)ERROR: Unknown ratio for density $DPI$(tput sgr0)"
exit
fi
SRC_DIR=`pwd`'/';
RES_DIR=$SRC_DIR'res/';
# copy to DPI
echo "$(tput setaf 4)Copying $DPI...$(tput sgr0)"
DEST_DIR=$RES_DIR'drawable-'$DPI'/';
mkdir -p $DEST_DIR;
for item in $( find . -type f -maxdepth 1 \( ! -iname ".png" \) | sed 's@./@@' ); do
echo Copying: $item
cp $SRC_DIR$item $DEST_DIR$item
done
SRC_DIR=$DEST_DIR
for density in ${(k)densities}; do
if [[ $density == $DPI ]]; then
echo "$(tput setaf 4)Not resizing $density as already was copied...$(tput sgr0)"
continue
fi
current_ratio=$densities[$density]
((percentage=current_ratio*100.0/ratio))
# resize density
echo "$(tput setaf 4)Start resizing $density...$(tput sgr0)"
DEST_DIR=$RES_DIR"drawable-$density/";
mkdir -p $DEST_DIR;
resize.sh $SRC_DIR $DEST_DIR $percentage'%'
done
echo "$(tput setaf 2)Done :)$(tput sgr0)"
#! /bin/bash
# This command can be used as an alias for adb and it will prompt for the
# device selection if needed
# alias adb=my-adb
set +x
set -e
if [ $# == 0 ]
then
# no arguments
exec adb
elif [ "$1" == 'devices' ]
then
# adb devices should not accept -s, -e or -d
exec adb devices
elif [ "$1" == 'kill-server' ]
then
# adb kill-server should not accept -s, -e or -d
exec adb kill-server
elif [ "$1" == 'connect' ]
then
exec adb "$@"
else
# because of the set -e, if selecting the device fails it exits
S=$(android-select-device "$@")
exec adb $S "$@"
fi
#!/bin/bash
# Requires ImageMagick
SRC_DIR=$1; # e.g. res/drawable-xhdpi/
DEST_DIR=$2; # e.g. res/drawable-mdpi/
RESIZE_BY=$3; # e.g. 50%
echo "From $SRC_DIR to $DEST_DIR resize by $RESIZE_BY";
for item in $( find . -type f -maxdepth 1 \( ! -iname ".png" \) | sed 's@./@@' ); do
echo Resizing: $item
convert $SRC_DIR$item -resize $RESIZE_BY $DEST_DIR$item
done
@felipeska
Copy link

please rename my-adb to my-adb.sh ;) or recoding in brainfuck

@RobGThai
Copy link

RobGThai commented Feb 5, 2015

Thanks @eveliotc. I'm trying to use decompile but getting error in subprocess. Any suggestion?

› sudo python decompile.py com.robgthai.spoon.hellospoon
Decompiling com.robgthai.spoon.hellospoon in com.robgthai.spoon.hellospoon
Pulling com.robgthai.spoon.hellospoon-2.apk...
4679 KB/s (940587 bytes in 0.196s)

Decompiling com.robgthai.spoon.hellospoon-2.apk...
Traceback (most recent call last):
  File "decompile.py", line 76, in <module>
    sys.exit(main())
  File "decompile.py", line 73, in main
    all(package, dir)
  File "decompile.py", line 52, in all
    decompile(packageFile)
  File "decompile.py", line 29, in decompile
    print shell(["apktool", "d", packageFile])
  File "decompile.py", line 8, in shell
    return subprocess.Popen(args, stdout=subprocess.PIPE, universal_newlines=True).communicate()[0]
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

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