Home | History | Annotate | Download | only in tools
      1 #!/bin/bash
      2 #
      3 # Copyright (C) 2013 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #     http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 function makeTempJar ()
     18 {
     19   local tempDir=/tmp
     20   if [ ! -e "${tempDir}" ]; then
     21     tempDir=.
     22   fi
     23   local tempfile="${tempDir}/mainDexClasses-$$.tmp.jar"
     24   if [ -e "${tempfile}" ]; then
     25     echo "Failed to create temporary file" >2
     26     exit 6
     27   fi
     28   echo "${tempfile}"
     29 }
     30 
     31 function cleanTmp ()
     32 {
     33   if [ -e "${tmpOut}" ] ; then
     34     rm "${tmpOut}"
     35   fi
     36 }
     37 
     38 
     39 # Set up prog to be the path of this script, including following symlinks,
     40 # and set up progdir to be the fully-qualified pathname of its directory.
     41 prog="$0"
     42 
     43 while [ -h "${prog}" ]; do
     44     newProg=`/bin/ls -ld "${prog}"`
     45     newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
     46     if expr "x${newProg}" : 'x/' >/dev/null; then
     47         prog="${newProg}"
     48     else
     49         progdir=`dirname "${prog}"`
     50         prog="${progdir}/${newProg}"
     51     fi
     52 done
     53 oldwd=`pwd`
     54 progdir=`dirname "${prog}"`
     55 cd "${progdir}"
     56 progdir=`pwd`
     57 prog="${progdir}"/`basename "${prog}"`
     58 cd "${oldwd}"
     59 
     60 baserules="${progdir}"/mainDexClasses.rules
     61 if [ ! -r ${baserules} ]; then
     62     echo `basename "$prog"`": can't find mainDexClasses.rules" 1>&2
     63     exit 1
     64 fi
     65 
     66 jarfile=dx.jar
     67 libdir="$progdir"
     68 
     69 if [ ! -r "$libdir/$jarfile" ]; then
     70     # set dx.jar location for the SDK case
     71     libdir="$libdir/lib"
     72 fi
     73 
     74 
     75 if [ ! -r "$libdir/$jarfile" ]; then
     76     # set dx.jar location for the Android tree case
     77     libdir=`dirname "$progdir"`/framework
     78 fi
     79 
     80 if [ ! -r "$libdir/$jarfile" ]; then
     81     echo `basename "$prog"`": can't find $jarfile" 1>&2
     82     exit 1
     83 fi
     84 
     85 proguardExec="proguard.sh"
     86 proguard=${PROGUARD_HOME}/bin/${proguardExec}
     87 
     88 if [ ! -r "${proguard}" ]; then
     89   # set proguard location for the SDK case
     90   proguardBaseDir=`dirname "$progdir"`
     91   # "${progdir}"/../..
     92   proguardBaseDir=`dirname "$proguardBaseDir"`
     93   proguard="${proguardBaseDir}"/tools/proguard/bin/${proguardExec}
     94 fi
     95 
     96 if [ ! -r "${proguard}" ]; then
     97   # set proguard location for the Android tree case
     98   proguardBaseDir=`dirname "$proguardBaseDir"`
     99   # "${progdir}"/../../../..
    100   proguardBaseDir=`dirname "$proguardBaseDir"`
    101   proguard="${proguardBaseDir}"/external/proguard/bin/${proguardExec}
    102 fi
    103 
    104 if [ ! -r "${proguard}" ]; then
    105     proguard="`which proguard`"
    106 fi
    107 
    108 if [ -z "${proguard}" -o ! -r "${proguard}" ]; then
    109     proguard="`which ${proguardExec}`"
    110 fi
    111 
    112 if [ -z "${proguard}" -o ! -r "${proguard}" ]; then
    113     echo `basename "$prog"`": can't find ${proguardExec}" 1>&2
    114     exit 1
    115 fi
    116 
    117 shrinkedAndroidJar="${SHRINKED_ANDROID_JAR}"
    118 if [ -z "${shrinkedAndroidJar}" ]; then
    119   shrinkedAndroidJar=shrinkedAndroid.jar
    120 fi
    121 
    122 if [ ! -r "${shrinkedAndroidJar}" ]; then
    123   shrinkedAndroidJar=${libdir}/${shrinkedAndroidJar}
    124 fi
    125 
    126 if [ ! -r "${shrinkedAndroidJar}" ]; then
    127     echo `basename "$prog"`": can't find shrinkedAndroid.jar" 1>&2
    128     exit 1
    129 fi
    130 
    131 if [ "$OSTYPE" = "cygwin" ]; then
    132     # For Cygwin, convert the jarfile path into native Windows style.
    133     jarpath=`cygpath -w "$libdir/$jarfile"`
    134   proguard=`cygpath -w "${proguard}"`
    135   shrinkedAndroidJar=`cygpath -w "${shrinkedAndroidJar}"`
    136 else
    137     jarpath="$libdir/$jarfile"
    138 fi
    139 
    140 if expr "x$1" : 'x--output' >/dev/null; then
    141     exec 1>$2
    142     shift 2
    143 fi
    144 
    145 if [ $# -ne 1 ]; then
    146   echo "Usage : $0 [--output <output file>] <application path>" 1>&2
    147   exit 2
    148 fi
    149 
    150 tmpOut=`makeTempJar`
    151 
    152 trap cleanTmp 0
    153 
    154 ${proguard} -injars ${@} -dontwarn -forceprocessing  -outjars ${tmpOut} \
    155   -libraryjars "${shrinkedAndroidJar}" -dontoptimize -dontobfuscate -dontpreverify \
    156   -include "${baserules}" 1>/dev/null || exit 10
    157 
    158 java -cp "$jarpath" com.android.multidex.ClassReferenceListBuilder "${tmpOut}" ${@} ||  exit 11
    159