forked from PeterCxy/BlackLight
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
202 lines (180 loc) · 5.67 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Makefile for BlackLight
# Build tools
AAPT := aapt # Android Asset Packaging Tool
JAVAC := javac # Java Compiler
DX := dx # Dex tool
JARSIGNER := jarsigner # Jar signing tool
ZIPALIGN := zipalign # Zip aligning tool
MAKE := make # GNU Make tool
ADB := adb # Android Debug Bridge
PM := /system/bin/pm # Package Manager on Android
# You do not need PM if you are building on PC.
# Build configs
BUILD_DIR := build
BIN_DIR := $(BUILD_DIR)/bin
GEN_DIR := $(BUILD_DIR)/gen
CLASSES_DIR := $(BIN_DIR)/classes
APK_NAME := build.apk
DEX_NAME := classes.dex
OUT_DEX := $(BIN_DIR)/$(DEX_NAME)
OUT_APK := $(BIN_DIR)/$(APK_NAME)
# Path to directories that contain source
# Including source directories of library projects
SRC_DIR := \
src \
libs/SlidingUpPanel/src \
libs/SystemBarTint/src \
libs/SwipeBackLayout/library/src/main/java
# Timestamp file of java sources
# Just a fake "target", doesn't matter in fact
SRC_TS := $(BUILD_DIR)/sources.ts
# Path to directories containing resources
# Including library projects
RES_DIR := \
res \
libs/SlidingUpPanel/res \
libs/SwipeBackLayout/library/src/main/res
# Timestamp file of resources
RES_TS := $(BUILD_DIR)/resources.ts
# External packages that need to generate R.java under.
# Usually these are library projects' package names.
# If a library does not contain any resource
# We do not need to put it here.
EXT_PKG := \
com.sothree.slidinguppanel.library \
me.imid.swipebacklayout.lib
# Include all jar libraries needed
# Including android.jar
# Please set the $ANDROID_JAR environment variable
# Pointing to your android.jar
JAR_LIB := \
$(ANDROID_JAR) \
libs/android-support-v4.jar \
libs/gson-2.2.2.jar \
libs/SlidingUpPanel/libs/nineoldandroids-2.4.0.jar
# Asset directory
ASSET := assets
# Packages that need to generate BuildConfig.java for.
# If a library needs BuildConfig.java,
# Please put it here also.
PACKAGE := us.shandian.blacklight
# Timestamp file of BuildConfig
PKG_TS := $(BUILD_DIR)/buildconfig.ts
# The main AndroidManifest
MANIFEST := AndroidManifest.xml
# Keystores
KEY_DEBUG := keystore/debug.keystore # Provided by Android SDK
KEY_RELEASE := keystore/publish.keystore
KEY_ALIAS := peter # Key alias for relase keystore
# Source list
SRC := \
$(foreach dir, \
$(SRC_DIR), \
$(foreach srcdir, \
$(shell find $(dir) -maxdepth 10 -type d), \
$(wildcard $(srcdir)/*.java) \
) \
)
GEN := $(foreach srcdir, $(shell find $(GEN_DIR) -maxdepth 10 -type d),$(wildcard $(srcdir)/*.java))
RES := \
$(foreach dir, \
$(RES_DIR), \
$(foreach srcdir, \
$(shell find $(dir) -maxdepth 10 -type d), \
$(wildcard $(srcdir)/*.*) \
) \
)
# Some stuff
EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
TAB := $(EMPTY) $(EMPTY)
COLON := $(EMPTY):$(EMPTY)
POINT := $(EMPTY).$(EMPTY)
SLASH := $(EMPTY)/$(EMPTY)
# Resource arguments for aapt
AAPT_RES := $(addprefix -S , $(RES_DIR))
AAPT_EXT := $(subst $(TAB),$(EMPTY),\
$(subst $(SPACE),$(COLON),$(EXT_PKG)))
# Classpath arguments for javac
JAVAC_CLASS := $(subst $(TAB),$(EMPTY),\
$(subst $(SPACE),$(COLON),$(JAR_LIB)))
# Default DEBUG Flag
ifndef DEBUG
DEBUG := true
endif
# Make rules
define gen-cfg
@mkdir -p $(GEN_DIR)/$1
@echo -e "package $(PACKAGE);\npublic class BuildConfig {\n public static final boolean DEBUG=$(DEBUG);\n}" > "$(GEN_DIR)/$1/BuildConfig.java"
endef
define target
@echo -e "\033[36mBuilding target:\033[0m $1"
endef
define build-info
@echo -e "\033[33mNOTICE: Please always do 'make clean' before you build release package!\033[0m"
@echo -e "\033[32mNOTICE: Ignore any warnings reported by 'find'. That doesn't matter.\033[0m"
@echo -e "\033[36mTarget apk path:\033[0m $(OUT_APK)"
endef
.PHONY: clean pre merge debug_make release_make debug release install
# Clean up
clean:
$(call target, Clean)
@rm -rf $(BUILD_DIR)
# Prepare build dir
pre:
$(call build-info)
$(call target, Environment)
@mkdir -p $(BIN_DIR)
@mkdir -p $(GEN_DIR)
@mkdir -p $(CLASSES_DIR)
# Generate resources
$(RES_TS): $(RES) $(MANIFEST)
$(call target, Resources)
@$(AAPT) p -m -M $(MANIFEST) -A $(ASSET) -I $(ANDROID_JAR) $(AAPT_RES) --extra-packages $(AAPT_EXT) --auto-add-overlay -J $(GEN_DIR) -F $(OUT_APK) -f
@echo $(shell date) > $@
# Generate build config
$(PKG_TS):
$(call target, BuildConfig)
$(foreach pkg, $(PACKAGE), $(call gen-cfg,$(subst $(POINT),$(SLASH),$(pkg))))
@echo $(shell date) > $@
# Call javac to build classes
$(SRC_TS): $(SRC) $(GEN)
$(call target, Classes)
@$(JAVAC) -encoding utf-8 -cp $(JAVAC_CLASS) -d $(CLASSES_DIR) $(SRC) $(GEN)
@echo $(shell date) > $@
# Convert the classes to dex format
$(OUT_DEX): $(SRC_TS)
$(call target, Dex)
@$(DX) --dex --no-strict --output=$(OUT_DEX) $(CLASSES_DIR) $(subst $(ANDROID_JAR) ,$(EMPTY),$(JAR_LIB))
# Merge the dex into apk
merge: $(OUT_DEX)
$(call target, Merge)
$(shell $(AAPT) r $(OUT_APK) $(DEX_NAME) > /dev/null)
@cd $(BIN_DIR) && $(AAPT) a $(APK_NAME) $(DEX_NAME)
# Debug package (do not zipalign)
debug_make: pre $(RES_TS) $(PKG_TS)
@$(MAKE) merge DEBUG=true
$(call target, Debug)
@$(JARSIGNER) -keystore $(KEY_DEBUG) -storepass android -sigalg MD5withRSA -digestalg SHA1 $(OUT_APK) my_alias
# Release package (zipalign)
release_make: pre $(RES_TS) $(PKG_TS)
@$(MAKE) merge DEBUG=false
$(call target, Release)
@$(JARSIGNER) -keystore $(KEY_RELEASE) -sigalg MD5withRSA -digestalg SHA1 $(OUT_APK) $(KEY_ALIAS)
@$(ZIPALIGN) 4 $(OUT_APK) $(OUT_APK)_zipalign
@rm -r $(OUT_APK)
@mv $(OUT_APK)_zipalign $(OUT_APK)
# Wrapper for debug build
debug:
@$(MAKE) debug_make DEBUG=true
# Wrapper for release build
release:
@$(MAKE) release_make DEBUG=false
# Install on phone
install:
$(call target, Install)
@if [ -f $(PM) ]; then \
$(PM) install -r $(OUT_APK);\
else \
$(ADB) install -r $(OUT_APK);\
fi