到哪里最烦的就是配置环境,如果遇到一个docker pull死慢,下载gradle死慢的网络,在有android SDK的情况下如何能快速build出一个apk呢?我就看一眼这个apk在android上长啥样…就当理解一下gradle做了啥吧…
比如我随便找一个sample app:
https://android.googlesource.com/platform/development/+/master/samples
有人铺好了路,就直接用起来吧:
https://stackoverflow.com/questions/41132753/how-can-i-build-an-android-apk-without-gradle-on-the-command-line
https://github.com/WanghongLin/miscellaneous/blob/master/tools/build-apk-manually.sh
先说一些问题,如果直接这样编译,Java 8有时候是有问题的,一些特性android实际机器上不一定有……比如lambda表达式,比如module的支持,method reference等…需要d8加持…中文注释都在代码里了…当然最麻烦的应该是dep的jar包们…
#!/bin/bash
set -xe
# A Simple Script to build a simple APK without ant/gradle
# Copyright 2016 Wanghong Lin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# create a simple Android application to test this script
#
# $ android create project -n MyApplication -p MyApplication -k com.example -a MainActivity --target 8
#
# copy this script to the root of your Android project and run
[ x$ANDROID_SDK_ROOT == x ] && {
printf '\e[31mANDROID_SDK_ROOT not set\e[30m\n'
exit 1
}
[ ! -d $ANDROID_SDK_ROOT ] && {
printf "\e[31mInvalid ANDROID_SDK_ROOT ---> $ANDROID_SDK_ROOT\e[30m\n"
exit 1
}
# use the latest build tool version
# and the oldest platform version for compatibility
# 这个是找下载过的最新的build tools,比如29.0.3
_BUILD_TOOLS_VERSION=$(ls $ANDROID_SDK_ROOT/build-tools | sort -n |tail -1)
# 这个是找下载过的最老的platform,比如android-22
_PLATFORM=$(ls $ANDROID_SDK_ROOT/platforms | sort -nr |tail -1)
_APK_BASENAME=这是一个名字
# 加更多的库在这里
_ANDROID_CP=$ANDROID_SDK_ROOT/platforms/$_PLATFORM/android.jar
_AAPT=$ANDROID_SDK_ROOT/build-tools/$_BUILD_TOOLS_VERSION/aapt
_DX=$ANDROID_SDK_ROOT/build-tools/$_BUILD_TOOLS_VERSION/dx
# 我擅自添加了这个,后面说
_D8=$ANDROID_SDK_ROOT/build-tools/$_BUILD_TOOLS_VERSION/d8
_ZIPALIGN=$ANDROID_SDK_ROOT/build-tools/$_BUILD_TOOLS_VERSION/zipalign
_ADB=$ANDROID_SDK_ROOT/platform-tools/adb
_INTERMEDIATE="bin gen ${_APK_BASENAME}.apk.unaligned"
printf "\e[32mBuild with configuration: \n\tbuild tools version: $_BUILD_TOOLS_VERSION \n\tplatform: $_PLATFORM\e[0m\n"
rm -rf $_INTERMEDIATE
mkdir bin gen
$_AAPT package -f -m -J gen -M AndroidManifest.xml -S res -I $_ANDROID_CP
javac -classpath $_ANDROID_CP \
-sourcepath 'src:gen' \
-d 'bin' -target 1.7 -source 1.7 \
`find . -name "*.java"`
# 如有需要,请配置各种参数,比如--min-sdk-version=26
$_DX --dex --output=classes.dex bin
$_AAPT package -f -M AndroidManifest.xml -S res -I $_ANDROID_CP -F ${_APK_BASENAME}.apk.unaligned
$_AAPT add ${_APK_BASENAME}.apk.unaligned classes.dex
jarsigner -keystore ~/.android/debug.keystore -storepass 'android' ${_APK_BASENAME}.apk.unaligned androiddebugkey
# create a release version with your keys
# jarsigner -keystore /path/to/your/release/keystore -storepass 'yourkeystorepassword' ${_APK_BASENAME}.apk.unaligned yourkeystorename
$_ZIPALIGN -f 4 ${_APK_BASENAME}.apk.unaligned ${_APK_BASENAME}-debug.apk
rm -rf $_INTERMEDIATE
# 想要Java 8的支持,除了使用d8以外,还要加额外的jar包…
# ref: https://developer.android.com/studio/write/java8-support
# ref: https://developer.android.com/studio/build/building-cmdline
#find bin -type f | xargs -I {} $_D8 --lib $_ANDROID_CP --min-api 25 {}
#$_D8 --lib $_ANDROID_CP --min-api 26 ${_APK_BASENAME}-debug.apk
$_ADB get-state 1>/dev/null 2>&1 && $_ADB install -r ${_APK_BASENAME}-debug.apk || printf '\e[31mNo Android device attach\e[30m\n'