-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapkgen
More file actions
executable file
·301 lines (263 loc) · 8.67 KB
/
apkgen
File metadata and controls
executable file
·301 lines (263 loc) · 8.67 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/data/data/com.termux/files/usr/bin/bash
# ------------------------------
# CONFIGURATION
# ------------------------------
TEMPLATE_REPO="https://github.com/termuxvoid/apkgen-template.git"
DEFAULT_APP_NAME="apkgentemplate"
DEFAULT_PACKAGE="com.apkgen.template"
VERSION="1.0.0"
# ------------------------------
# COLORS
# ------------------------------
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[0;37m'
BOLD='\033[1m'
DIM='\033[2m'
RESET='\033[0m'
# ------------------------------
# HELPERS
# ------------------------------
error() {
echo -e "${RED}${BOLD}Error:${RESET} ${RED}$1${RESET}" >&2
exit 1
}
info() {
echo -e "${WHITE}$1${RESET}"
}
warn() {
echo -e "${YELLOW}${BOLD}Warning:${RESET} ${YELLOW}$1${RESET}"
}
success() {
echo -e "${GREEN}${BOLD}✓${RESET} $1"
}
step() {
echo -e "${CYAN}${BOLD}=>${RESET} $1"
}
spinner() {
local msg="$1"
shift
gum spin --spinner dot --title "$msg" -- "$@"
}
# Ensure gradlew is executable
ensure_gradlew() {
if [ -f "gradlew" ] && [ ! -x "gradlew" ]; then
step "Making gradlew executable..."
chmod +x gradlew
success "gradlew is now executable"
elif [ ! -f "gradlew" ]; then
error "gradlew not found. Are you in a valid Android project?"
fi
}
escape_dots() { echo "$1" | sed 's/\./\\./g'; }
update_text_files() {
local dir="$1" old="$2" new="$3"
local old_esc=$(escape_dots "$old")
local new_esc=$(escape_dots "$new")
find "$dir" -type f -not -path '*/\.git/*' -exec grep -Il '.' {} \; | while read -r f; do
sed -i "s/$old_esc/$new_esc/g" "$f"
done
}
restructure_dirs() {
local dir="$1" old="$2" new="$3"
local old_path="${old//.//}" new_path="${new//.//}"
for src_root in "app/src/main/java" "app/src/main/kotlin" \
"app/src/androidTest/java" "app/src/androidTest/kotlin" \
"app/src/test/java" "app/src/test/kotlin"; do
local root_dir="$dir/$src_root"
[ -d "$root_dir" ] || continue
local old_dir="$root_dir/$old_path"
local new_dir="$root_dir/$new_path"
if [ -z "$old_path" ]; then
find "$root_dir" -maxdepth 1 -type f \( -name "*.java" -o -name "*.kt" \) | while read -r src_file; do
mkdir -p "$new_dir"
mv "$src_file" "$new_dir/"
done
else
if [ -d "$old_dir" ]; then
mkdir -p "$new_dir"
find "$old_dir" -type f \( -name "*.java" -o -name "*.kt" \) | while read -r src_file; do
local rel_path="${src_file#$old_dir/}"
local target="$new_dir/$rel_path"
mkdir -p "$(dirname "$target")"
mv "$src_file" "$target"
done
rm -rf "$old_dir"
local p="$(dirname "$old_dir")"
while [ "$p" != "$root_dir" ] && [ -d "$p" ] && [ -z "$(ls -A "$p" 2>/dev/null)" ]; do
rmdir "$p" 2>/dev/null
p="$(dirname "$p")"
done
fi
fi
done
}
update_build_gradle() {
local f="$1/app/build.gradle"
[ -f "$f" ] || return
local old_esc=$(escape_dots "$2")
local new_esc=$(escape_dots "$3")
sed -i "s/applicationId \"$old_esc\"/applicationId \"$new_esc\"/" "$f"
sed -i "s/namespace \"$old_esc\"/namespace \"$new_esc\"/" "$f"
}
update_manifest() {
local f="$1/app/src/main/AndroidManifest.xml"
[ -f "$f" ] || return
local old_esc=$(escape_dots "$2")
local new_esc=$(escape_dots "$3")
sed -i "s/package=\"$old_esc\"/package=\"$new_esc\"/" "$f"
}
update_app_name() {
local f="$1/app/src/main/res/values/strings.xml"
[ -f "$f" ] && sed -i "s/$2/$3/g" "$f"
}
# ------------------------------
# BANNER
# ------------------------------
show_banner() {
clear
gum style \
--foreground 63 --border double --border-foreground 63 \
--padding "1 2" --margin "1 2" \
"APKGen v${VERSION}" \
"Android APK Generator CLI"
}
# ------------------------------
# COMMANDS
# ------------------------------
create_app() {
local app_dir="$1"
[ -z "$app_dir" ] && error "Usage: apkgen create <app-directory>"
if [ -e "$app_dir" ]; then
error "A project with this name already exists"
fi
echo
step "Cloning template repository..."
spinner "Cloning template repository..." git clone --quiet "$TEMPLATE_REPO" "$app_dir"
if [ $? -ne 0 ]; then
error "Clone failed"
fi
step "Removing .git directory..."
rm -rf "$app_dir/.git"
success ".git directory removed"
cd "$app_dir" || error "Cannot enter $app_dir"
echo
echo -e "${BOLD}Configuration${RESET}"
echo -e "${DIM}Please provide the following information:${RESET}"
echo
echo -e "${CYAN}App name${RESET} (e.g., MyApp)"
new_name=$(gum input \
--placeholder "MyApp" \
--prompt "> " \
--prompt.foreground 39 \
--placeholder.foreground 241 \
--value "$DEFAULT_APP_NAME" \
--width 60)
echo
echo -e "${CYAN}Package name${RESET} (e.g., com.example.myapp)"
new_pkg=$(gum input \
--placeholder "com.example.myapp" \
--prompt "> " \
--prompt.foreground 39 \
--placeholder.foreground 241 \
--value "$DEFAULT_PACKAGE" \
--width 60)
echo
step "Restructuring project..."
update_app_name "$(pwd)" "$DEFAULT_APP_NAME" "$new_name"
update_manifest "$(pwd)" "$DEFAULT_PACKAGE" "$new_pkg"
update_build_gradle "$(pwd)" "$DEFAULT_PACKAGE" "$new_pkg"
update_text_files "$(pwd)" "$DEFAULT_PACKAGE" "$new_pkg"
restructure_dirs "$(pwd)" "$DEFAULT_PACKAGE" "$new_pkg"
echo
success "Project created successfully"
echo
echo -e "${BOLD}Next steps:${RESET}"
echo -e " ${DIM}1.${RESET} ${WHITE}cd $app_dir${RESET}"
echo -e " ${DIM}2.${RESET} ${WHITE}apkgen build debug${RESET}"
echo
}
build_apk() {
local build_type="$1"
ensure_gradlew
if [ "$build_type" = "debug" ]; then
step "Building debug APK..."
./gradlew assembleDebug --no-daemon
if [ $? -ne 0 ]; then
error "Build failed"
fi
local apk=$(find app/build/outputs/apk -name "*debug*.apk" | head -1)
[ -z "$apk" ] && error "APK not found"
success "Build completed"
echo -e " ${DIM}Output:${RESET} ${WHITE}$(realpath "$apk")${RESET}"
elif [ "$build_type" = "release" ]; then
step "Building release APK (unsigned)..."
./gradlew assembleRelease --no-daemon
if [ $? -ne 0 ]; then
error "Build failed"
fi
local apk=$(find app/build/outputs/apk -name "*release*.apk" | head -1)
[ -z "$apk" ] && error "APK not found"
warn "APK is unsigned. Sign it before installing."
echo -e " ${DIM}Output:${RESET} ${WHITE}$(realpath "$apk")${RESET}"
else
error "Invalid build type. Use 'debug' or 'release'"
fi
}
clean_project() {
ensure_gradlew
step "Cleaning project..."
rm -rf build
./gradlew clean --no-daemon
if [ $? -ne 0 ]; then
error "Clean failed"
fi
success "Build artifacts removed"
}
show_help() {
show_banner
echo -e "${WHITE}A CLI tool to quickly generate Android APKs from a template.${RESET}"
echo
echo -e "${BOLD}USAGE:${RESET}"
echo -e " ${WHITE}apkgen${RESET} <command> [arguments]"
echo
echo -e "${BOLD}COMMANDS:${RESET}"
echo -e " ${CYAN}create${RESET} <app-directory> Clone template and customize app"
echo -e " ${CYAN}build${RESET} debug Build debug APK (run from project dir)"
echo -e " ${CYAN}build${RESET} release Build unsigned release APK"
echo -e " ${CYAN}clean${RESET} Clean build artifacts"
echo -e " ${CYAN}help${RESET} Show this help message"
echo
echo -e "${BOLD}EXAMPLES:${RESET}"
echo -e " ${WHITE}apkgen create myapp${RESET}"
echo -e " ${WHITE}cd myapp && apkgen build debug${RESET}"
echo -e " ${WHITE}apkgen build release${RESET}"
echo -e " ${WHITE}apkgen clean${RESET}"
echo
}
# ------------------------------
# MAIN
# ------------------------------
case "$1" in
create) create_app "$2" ;;
build)
if [ "$2" = "debug" ] || [ "$2" = "release" ]; then
build_apk "$2"
else
error "Usage: apkgen build {debug|release}"
fi
;;
clean) clean_project ;;
help|--help|-h) show_help ;;
*)
if [ -z "$1" ]; then
show_help
else
error "Unknown command '$1'. Run 'apkgen help' for usage."
fi
;;
esac