-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaproject
executable file
·490 lines (403 loc) · 12 KB
/
aproject
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/system/bin/sh
TOPDIR=$PWD
if [ "$TOPDIR" = "" ]; then
TOPDIR="."
fi
TOPDIR=$(realpath $TOPDIR)
error() {
echo "ERROR: $@"
exit 1
}
usage_create() {
echo
echo "Options for create:"
echo "-n --name - Project name. [required]"
echo "-t --target - Target ID of the new project."
echo "-p --path - The new project's directory. [required]"
echo "-k --package - Android package name. [required]"
echo "-a --activity - Name of the default Activity that is created."
echo " [required for application project]"
echo "-N --appname - Application name. [application project only]"
echo
exit 1
}
usage() {
echo
echo "Usage:"
echo "create project [options] - Create a new Android project."
echo "create lib-project [options] - Create a new Android library project."
echo "build [debug][-i|--install] - Debug build."
echo "build release[-i|--install] - Release build."
echo "clean - Cleanup project files."
usage_create
}
if [ "$1" = "create" ]; then
PROJECT_LIBRARY=
if [ "$2" = "lib-project" ]; then
PROJECT_LIBRARY="yes"
elif [ ! "$2" = "project" ]; then
usage
fi
shift 2
USE_GUIBOX=
O=$(getopt -n aproject -l path:,package:,activity:,name:,appname:,target:,gui -o p:k:a:n:N:t:g -- "$@") || exit 1
eval set -- "$O"
while true; do
case "$1" in
-p|--path)
PROJECT_PATH="$2"; shift 2;;
-k|--package)
PROJECT_PACKAGE="$2"; shift 2;;
-a|--activity)
PROJECT_ACTIVITY="$2";shift 2;;
-n|--name)
PROJECT_NAME="$2"; shift 2;;
-N|--appname)
PROJECT_APPNAME="$2"; shift 2;;
-t|--target)
PROJECT_TARGET="$2"; shift 2;;
-g|--gui)
USE_GUIBOX=yes; shift;;
--)
shift; break;;
*)
echo Error; exit 1;;
esac
done
if [ ! "$@" = "" ]; then
echo "aproject: unrecognized options: $@"
usage_create
fi
if [ "$PROJECT_PATH" = "" -o "$PROJECT_PACKAGE" = "" -o "$PROJECT_NAME" = "" ]; then
if [ "$USE_GUIBOX" = "yes" ]; then
adialog --msgbox --title "Error!" --message "Cannot create a project" --text "Check project path, package or name."
exit 1
else
usage_create
fi
fi
if [ "$PROJECT_LIBRARY" = "" -a "$PROJECT_ACTIVITY" = "" ]; then
if [ "$USE_GUIBOX" = "yes" ]; then
adialog --msgbox --title "Error!" --message "Cannot create a project" --text "Empty activity name."
exit 1
else
usage_create
fi
fi
if [ "$PROJECT_APPNAME" = "" ]; then
PROJECT_APPNAME=$PROJECT_NAME
fi
if [ "$PROJECT_TARGET" = "" ]; then
ANDROID_SDK_VER=$(getprop ro.build.version.sdk)
else
ANDROID_SDK_VER=$PROJECT_TARGET
fi
ANDROID_SDK=${CCTOOLSDIR}/sdk/android-${ANDROID_SDK_VER}
if [ ! -d $ANDROID_SDK ]; then
#
# FIXME: get available version from sdk directory
#
ANDROID_SDK_VER="19"
ANDROID_SDK=${CCTOOLSDIR}/sdk/android-${ANDROID_SDK_VER}
fi
if [ ! -d $ANDROID_SDK ]; then
if [ "$USE_GUIBOX" = "yes" ]; then
adialog --msgbox --title "Error!" --message "Cannot create a project" --text "No SDK Android-${ANDROID_SDK_VER} found."
exit 1
else
echo "ERROR: No SDK Android-${ANDROID_SDK_VER} found!"
exit 1
fi
fi
p="${PROJECT_PATH}/${PROJECT_NAME}"
p1="${p}/src/$(echo $PROJECT_PACKAGE | tr '.' '/')"
for d in drawable-hdpi drawable-ldpi drawable-mdpi drawable-xhdpi drawable-xxhdpi layout menu values xml; do
mkdir -p ${p}/res/$d
done
mkdir -p $p1
cp -f ${ANDROID_SDK}/template/AndroidManifest.template ${p}/AndroidManifest.xml
cp -f ${ANDROID_SDK}/template/layout.template ${p}/res/layout/main.xml
cp -f ${ANDROID_SDK}/template/strings.template ${p}/res/values/strings.xml
cp -f ${ANDROID_SDK}/template/ic_launcher_ldpi.png ${p}/res/drawable-ldpi/ic_launcher.png
cp -f ${ANDROID_SDK}/template/ic_launcher_mdpi.png ${p}/res/drawable-mdpi/ic_launcher.png
cp -f ${ANDROID_SDK}/template/ic_launcher_hdpi.png ${p}/res/drawable-hdpi/ic_launcher.png
if [ "$PROJECT_LIBRARY" = "yes" ]; then
cp -f ${ANDROID_SDK}/template/java_file.template ${p1}/${PROJECT_NAME}.java
sed -i -e "s|PACKAGE|$PROJECT_PACKAGE|" \
-e "s|ACTIVITY_ENTRY_NAME|$PROJECT_NAME|" \
-e "s|ACTIVITY_CLASS_NAME|$PROJECT_NAME|" \
-e "s|ICON|android:icon=\"@drawable/ic_launcher\"|" ${p}/AndroidManifest.xml ${p1}/${PROJECT_NAME}.java
else
cp -f ${ANDROID_SDK}/template/java_file.template ${p1}/${PROJECT_ACTIVITY}.java
sed -i -e "s|PACKAGE|$PROJECT_PACKAGE|" \
-e "s|ACTIVITY_ENTRY_NAME|$PROJECT_ACTIVITY|" \
-e "s|ACTIVITY_CLASS_NAME|$PROJECT_ACTIVITY|" \
-e "s|ICON|android:icon=\"@drawable/ic_launcher\"|" ${p}/AndroidManifest.xml ${p1}/${PROJECT_ACTIVITY}.java
fi
sed -i -e "s|PACKAGE|$PROJECT_PACKAGE|" \
-e "s|ACTIVITY_ENTRY_NAME|$PROJECT_APPNAME|" \
-e "s|ACTIVITY_CLASS_NAME|$PROJECT_APPNAME|" \
-e "s|ICON|android:icon=\"@drawable/ic_launcher\"|" ${p}/res/layout/main.xml ${p}/res/values/strings.xml
sed -i -e "s|<application|<uses-sdk android:minSdkVersion=\"5\" android:targetSdkVersion=\"$ANDROID_SDK_VER\" />\n <application|" ${p}/AndroidManifest.xml
cat > ${p}/project.properties << EOF
# This file is automatically generated by aproject.
target=android-${ANDROID_SDK_VER}
EOF
if [ "$PROJECT_LIBRARY" = "yes" ]; then
echo "android.library=true" >> ${p}/project.properties
fi
if [ "$USE_GUIBOX" = "yes" ]; then
adialog --msgbox --title "Congratulation!" --message "New project created!" --text "Project path is ${p}."
fi
exit 0
fi
get_ext_libdirs() {
for f in project.properties default.properties; do
if [ -e ${TOPDIR}/$f ]; then
cat ${TOPDIR}/$f | grep android.library.reference | cut -f2 -d=
break
fi
done
}
is_library() {
for f in project.properties default.properties; do
if [ -e ${TOPDIR}/$f ]; then
cat ${TOPDIR}/$f | grep android.library | head -n1 | cut -f2 -d=
break
fi
done
}
check_android_manifest() {
if [ ! -f AndroidManifest.xml ]; then
echo "Not android project directory!"
exit 1
fi
}
get_project_sdk_version() {
local SDK
local VERS=$(aproject-helper TargetSDK)
if [ "$VERS" = "" ]; then
VERS=$(aproject-helper MinSDK)
fi
if [ "$VERS" = "" ]; then
VERS=$(getprop ro.build.version.sdk)
fi
SDK="${CCTOOLSDIR}/sdk/android-${VERS}"
if [ ! -d "$SDK" ]; then
#
# FIXME: get all available versions from sdk directory
#
VERS="19"
fi
echo "$VERS"
}
PROJECTNAME=$(aproject-helper AppName 2>/dev/null)
if [ "$PROJECTNAME" = "" ]; then
PROJECTNAME=$(basename $TOPDIR)
fi
APROJECT_BIN=$(realpath $0)
VERBOSE_JAVAC=
VERBOSE_JAR=
VERBOSE_DEX=
VERBOSE_APKBUILDER=
VERBOSE_AAPT=
if [ "$1" = "-v" ]; then
VERBOSE_JAVAC="-verbose"
VERBOSE_JAR="v"
VERBOSE_DEX="--verbose"
VERBOSE_APKBUILDER="-v"
VERBOSE_AAPT="-v"
shift
fi
if [ "$1" = "clean" ]; then
check_android_manifest
echo "Cleaning $PROJECTNAME"
rm -rf bin gen
for d in $(get_ext_libdirs); do
cd $d && $APROJECT_BIN $VERBOSE_AAPT clean
cd $TOPDIR
done
exit 0
fi
if [ ! "$1" = "build" ]; then
usage
fi
shift
check_android_manifest
for d in $(get_ext_libdirs); do
cd $d
$APROJECT_BIN $VERBOSE_AAPT build $@ || error "external library"
cd $TOPDIR
done
echo "Build $PROJECTNAME"
PROJECT_RELEASE="no"
if [ "$1" = "release" ]; then
PROJECT_RELEASE="yes"
shift
elif [ "$1" = "debug" ]; then
shift
fi
mkdir -p bin/classes gen
mkdir -p bin/res
cp -f AndroidManifest.xml bin/
ANDROID_SDK_VER=$(get_project_sdk_version)
ANDROID_SDK=${CCTOOLSDIR}/sdk/android-${ANDROID_SDK_VER}
if [ ! -d $ANDROID_SDK ]; then
echo "ERROR: No SDK Android-${ANDROID_SDK_VER} found!"
exit 1
fi
find_libs_for_aapt() {
local d=
for d in $@; do
test -d $d && find $d -name "*.jar" -type f -exec printf "-I {} " \;
done
}
find_aidl_for_aidl() {
local d=
for d in $@; do
test -d $d && find $d -name "*.aidl" -type f -exec printf "-p{} " \;
done
}
find_libs_for_javac() {
local d=
local x=
for d in $@; do
test -d $d && find $d -name "*.jar" -type f -exec printf "{}:" \;
done
}
find_libs_for_dex() {
local d=
local x=
for d in $@; do
test -d $d && find $d -name "*.jar" -type f -exec printf "{} " \;
done
}
get_ext_resources() {
local d
for d in $@; do
printf "-S ${d}/bin/res -S ${d}/res "
done
}
find_ext_libs_for_javac() {
local d
for d in $@; do
find_libs_for_javac ${d}/bin ${d}/libs
done
}
find_ext_libs_for_dex() {
local d
for d in $@; do
find_libs_for_dex ${d}/bin ${d}/libs
done
}
ACTIVITIES=$(aproject-helper Activities)
EXT_LIBDIRS=$(get_ext_libdirs)
if [ "$PROJECT_RELEASE" = "yes" ]; then
aproject-helper BuildConfig release
else
aproject-helper BuildConfig debug
fi
if [ ! "$EXT_LIBDIRS" = "" ]; then
AAPT_OVERLAY="--auto-add-overlay"
fi
echo "Starting aapt..."
if [ "$IS_LIBRARY" = "true" -o "$ACTIVITIES" = "" ]; then
aapt p \
--non-constant-id \
-f $VERBOSE_AAPT \
-m \
--output-text-symbols ${TOPDIR}/bin \
${AAPT_OVERLAY} \
-M ${TOPDIR}/bin/AndroidManifest.xml \
-F ${TOPDIR}/bin/resources.res \
-S ${TOPDIR}/bin/res \
-S ${TOPDIR}/res \
$(get_ext_resources $EXT_LIBDIRS) \
$(find_libs_for_aapt $ANDROID_SDK libs $ANDROID_LIBS) \
-J ${TOPDIR}/gen \
--generate-dependencies \
|| error "aapt"
else
aapt p \
-f $VERBOSE_AAPT \
-m \
--output-text-symbols ${TOPDIR}/bin \
${AAPT_OVERLAY} \
-M ${TOPDIR}/bin/AndroidManifest.xml \
-F ${TOPDIR}/bin/resources.res \
-S ${TOPDIR}/bin/res \
-S ${TOPDIR}/res \
$(get_ext_resources $EXT_LIBDIRS) \
$(find_libs_for_aapt $ANDROID_SDK libs $ANDROID_LIBS) \
-J ${TOPDIR}/gen \
--generate-dependencies \
|| error "aapt"
fi
echo "Starting aidl..."
find . -name "*.aidl" -exec aidl -Isrc $(find_aidl_for_aidl $ANDROID_SDK $ANDROID_AIDL) -ogen {} \;
echo "Starting javac..."
javac $VERBOSE_JAVAC \
-d ${TOPDIR}/bin/classes \
-classpath ${TOPDIR}/bin/classes:$(find_ext_libs_for_javac $EXT_LIBDIRS):$(find_libs_for_javac $ANDROID_SDK libs $ANDROID_LIBS) \
-sourcepath ${TOPDIR}/src:${TOPDIR}/gen \
-target 1.5 \
-bootclasspath ${ANDROID_SDK}/android.jar \
-encoding UTF-8 \
-source 1.5 \
$(find src -name "*.java") \
$(find gen -name "*.java") \
|| error "javac"
IS_LIBRARY=$(is_library)
if [ "$IS_LIBRARY" = "true" -o "$ACTIVITIES" = "" ]; then
echo "Build library..."
jar c${VERBOSE_JAR}f bin/${PROJECTNAME}.jar -C bin/classes .
else
mkdir -p ${TOPDIR}/bin/dexedLibs
DEX_LIBS=
DEXED_LIBS=
for f in $(find_ext_libs_for_dex $EXT_LIBDIRS) $(find_libs_for_dex libs); do
for v in $DEX_LIBS; do
if cmp -s $f $v; then
f=""
break
fi
done
if [ ! "$f" = "" ]; then
DEX_LIBS="$DEX_LIBS $f"
fname=$(basename $f)
echo "Pre-dexing library ${fname}..."
dx --dex \
$VERBOSE_DEX \
--output ${TOPDIR}/bin/dexedLibs/${fname} \
$f \
|| error "dx"
DEXED_LIBS="$DEXED_LIBS ${TOPDIR}/bin/dexedLibs/${fname}"
fi
done
echo "Starting dx..."
dx --dex \
$VERBOSE_DEX \
--no-strict \
--output=bin/${PROJECTNAME}.dex \
$(find bin/classes -maxdepth 1 -not -path "bin/classes") \
$DEXED_LIBS \
|| error "dx"
NATIVE_LIBS=
if [ -d libs ]; then
NATIVE_LIBS="-nf libs"
fi
echo "Starting apkbuilder..."
apkbuilder bin/${PROJECTNAME}-unsigned.apk \
$VERBOSE_APKBUILDER \
-u \
-z ./bin/resources.res \
-f ./bin/${PROJECTNAME}.dex \
$NATIVE_LIBS \
|| error "apkbuilder"
echo "Starting apksigner..."
apksigner bin/${PROJECTNAME}-unsigned.apk bin/${PROJECTNAME}.apk || error "apksigner"
if [ "$1" = "-i" -o "$1" = "--install" ]; then
#am start $(am 2>&1| grep -q '\-\-user' && echo '--user 0') -a android.intent.action.VIEW -t application/vnd.android.package-archive -d "file://${PWD}/bin/${PROJECTNAME}.apk"
adialog --install "${PWD}/bin/${PROJECTNAME}.apk"
fi
fi