Skip to content

Instantly share code, notes, and snippets.

@Lattay
Created January 17, 2020 16:18
Show Gist options
  • Save Lattay/bbf1c3f369967b6da03d7a68e590df2d to your computer and use it in GitHub Desktop.
Save Lattay/bbf1c3f369967b6da03d7a68e590df2d to your computer and use it in GitHub Desktop.
A makefile to compile Android projects from command line.
# reverse URL parameters
DOMAIN=example
SUB_DOMAIN=helloandroid
# Name
SHORT_PROGNAME=helloandroid
# SDK path for version 29.0.2
SDK_PATH=/opt/android-sdk
BUILD_TOOLS_PATH=$(SDK_PATH)/build-tools/29.0.2
AAPT=$(BUILD_TOOLS_PATH)/aapt
DX=$(BUILD_TOOLS_PATH)/dx
ADB=$(SDK_PATH)/platform-tools/adb
ZIPALIGN=$(BUILD_TOOLS_PATH)/zipalign
APKSIGNER=$(BUILD_TOOLS_PATH)/apksigner
PLATFORM=$(SDK_PATH)/platforms/android-29/android.jar
.PHONY: compile clean wipe test
# test by installing app on a connected device
test: bin/$(SHORT_PROGNAME).apk
@echo "Launching..."
$(ADB) install -r $<
$(ADB) shell am start -n com.$(DOMAIN).$(SUB_DOMAIN)/.MainActivity
src/com/$(DOMAIN)/$(SUB_DOMAIN)/R.java:
@echo "Generating R.java file..."
$(AAPT) package -f -m -J src -M AndroidManifest.xml -S res -I $(PLATFORM)
obj/com/$(DOMAIN)/$(SUB_DOMAIN)/%.class: src/com/$(DOMAIN)/$(SUB_DOMAIN)/%.java
javac -d obj -classpath src -bootclasspath $(PLATFORM) -source 1.7 -target 1.7 $<
classes.dex: obj/com/$(DOMAIN)/$(SUB_DOMAIN)/R.class obj/com/$(DOMAIN)/$(SUB_DOMAIN)/MainActivity.class
$(DX) --dex --output=classes.dex obj
bin/$(SHORT_PROGNAME).unaligned.apk: classes.dex
@echo "Making APK..."
$(AAPT) package -f -m -F bin/$(SHORT_PROGNAME).unaligned.apk -M AndroidManifest.xml -S res -I $(PLATFORM)
$(AAPT) add bin/$(SHORT_PROGNAME).unaligned.apk classes.dex
bin/$(SHORT_PROGNAME).apk: bin/$(SHORT_PROGNAME).unaligned.apk release.jks
@echo "Aligning and signing APK..."
$(ZIPALIGN) -f 4 $< $@
$(APKSIGNER) sign --ks release.jks $@
release.jks:
keytool -genkeypair -validity 365 -keystore $@ -keysize 2048 -keyalg RSA -sigalg SHA256withRSA
clean:
@echo "Cleaning..."
rm -rf classes.dex
rm -rf obj/*
rm -rf src/com/$(DOMAIN)/$(SUB_DOMAIN)/R.java
wipe: clean
@echo "Wiping..."
rm -rf bin/*
rm -rf release.jks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment