apkbuilder

用于生成未签名的APK

apkbuidler是个shell脚本,它的的实现如下:

#!/bin/sh
# Copyright 2005-2007, The Android Open Source Project
#
# 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.

# Set up prog to be the path of this script, including following symlinks,
# and set up progdir to be the fully-qualified pathname of its directory.
prog="$0"
while [ -h "${prog}" ]; do
    newProg=`/bin/ls -ld "${prog}"`
    newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
    if expr "x${newProg}" : 'x/' >/dev/null; then
        prog="${newProg}"
    else
        progdir=`dirname "${prog}"`
        prog="${progdir}/${newProg}"
    fi
done
oldwd=`pwd`
progdir=`dirname "${prog}"`
cd "${progdir}"
progdir=`pwd`
prog="${progdir}"/`basename "${prog}"`
cd "${oldwd}"

jarfile=sdklib.jar
frameworkdir="$progdir"
if [ ! -r "$frameworkdir/$jarfile" ]
then
    frameworkdir=`dirname "$progdir"`/tools/lib
fi
if [ ! -r "$frameworkdir/$jarfile" ]
then
    frameworkdir=`dirname "$progdir"`/framework
fi
if [ ! -r "$frameworkdir/$jarfile" ]
then
    echo `basename "$prog"`": can't find $jarfile"
    exit 1
fi


# Check args.
if [ debug = "$1" ]; then
    # add this in for debugging
    java_debug=-agentlib:jdwp=transport=dt_socket,server=y,address=8050,suspend=y
    shift 1
else
    java_debug=
fi

if [ "$OSTYPE" = "cygwin" ] ; then
    jarpath=`cygpath -w  "$frameworkdir/$jarfile"`
else
    jarpath="$frameworkdir/$jarfile"
fi

# might need more memory, e.g. -Xmx128M
exec java -Xmx128M $java_debug -classpath "$jarpath" com.android.sdklib.build.ApkBuilderMain "$@"

需要注意的是:这个脚本从sdk r17开始不再提供了。 但是从apkbuilder的实现我们知道,apkbuilder最终调用的是sdklib.jar中的com.android.sdklib.build.ApkBuilderMain类来做事情的,所以,我们可以将这个脚本复制一份,放到原来的位置, 或者直接使用java命令执行这个类:

查看shell脚本的帮助,如下:

使用Jar包的执行效果与shell脚本是一样的。

用法:

apkbuilder <apkfile> [option]

参数说明:

参数是否可选说明
outArchive必选表示要输出的apk路径。例如:~/HelloWorld/bin/my.apk
-v可选输出日志
-d可选生成的文件中包含debug信息。
-u可选表示创建一个未签名的apk
-storetype可选忽略storetype校验失败
-z可选指定编译好的资源包。例如:~/HelloWorld/bin/resources.ap_
-f可选指定dex文件的路径。例如:~/HelloWorld/bin/classes.dex
--rf可选指定源文件的路径。例如:~/HelloWorld/src
--rj可选指定引用的库的路径。例如:~/HelloWorld/libs

示例:

apkbuilder Prism.apk -v -u -z bin/ressources.ap_ -f bin/classes.dex -rj libs