compile.sh 11.2 KB
Newer Older
1
#! /bin/sh
2
set -e
3

4 5 6 7
#############
# FUNCTIONS #
#############

8 9 10 11 12
diagnostic()
{
    echo "$@" 1>&2;
}

13 14 15 16 17 18 19 20
checkfail()
{
    if [ ! $? -eq 0 ];then
        diagnostic "$1"
        exit 1
    fi
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
21 22
# Read the Android Wiki http://wiki.videolan.org/AndroidCompile
# Setup all that stuff correctly.
23
# Get the latest Android SDK Platform or modify numbers in configure.sh and libvlc/default.properties.
Rafaël Carré's avatar
Rafaël Carré committed
24

25
RELEASE=0
26 27
while [ $# -gt 0 ]; do
    case $1 in
28
        help|--help|-h)
29
            echo "Use -a to set the ARCH:"
30 31
            echo "  ARM:     (armeabi-v7a|arm)"
            echo "  ARM64:   (arm64-v8a|arm64)"
32
            echo "  X86:     x86, x86_64"
33
            echo "Use --release to build in release mode"
34
            echo "Use --signrelease to build in release mode and sign apk, see vlc-android/build.gradle"
35
            echo "Use -s to set your keystore file and -p for the password"
Thomas Guillem's avatar
Thomas Guillem committed
36
            echo "Use -c to get a ChromeOS build"
37
            echo "Use -l to build only LibVLC"
38
            exit 0
39 40 41 42 43 44 45 46
            ;;
        a|-a)
            ANDROID_ABI=$2
            shift
            ;;
        -r|release|--release)
            RELEASE=1
            ;;
47 48 49 50
        signrelease|--signrelease)
            SIGNED_RELEASE=1
            RELEASE=1
            ;;
51 52 53 54 55 56 57 58
        -s|--signature)
            KEYSTORE_FILE=$2
            shift
            ;;
        -p|--password)
            PASSWORD_KEYSTORE=$2
            shift
            ;;
59 60
        -l)
            BUILD_LIBVLC=1
61
            NO_ML=1
62
            ;;
63 64 65
        -ml)
            BUILD_MEDIALIB=1
            ;;
66 67 68
        run)
            RUN=1
            ;;
Duncan McNAMARA's avatar
Duncan McNAMARA committed
69 70 71
        test)
            TEST=1
            ;;
72 73 74
        --no-ml)
            NO_ML=1
            ;;
75 76 77 78
        --publish)
            RELEASE=1
            PUBLISH=1
            ;;
79 80 81
        --init)
            GRADLE_SETUP=1
            ;;
82
        *)
83 84
            diagnostic "$0: Invalid option '$1'."
            diagnostic "$0: Try --help for more information."
85 86
            exit 1
            ;;
87
    esac
88
    shift
89 90
done

91
if [ -z "$ANDROID_NDK" -o -z "$ANDROID_SDK" ]; then
92 93
   diagnostic "You must define ANDROID_NDK, ANDROID_SDK before starting."
   diagnostic "They must point to your NDK and SDK directories."
94 95 96
   exit 1
fi

97
if [ -z "$ANDROID_ABI" ]; then
98
   diagnostic "*** No ANDROID_ABI defined architecture: using ARMv7"
99
   ANDROID_ABI="armeabi-v7a"
100 101
fi

102
if [ "$ANDROID_ABI" = "armeabi-v7a" -o "$ANDROID_ABI" = "arm" ]; then
103
    ANDROID_ABI="armeabi-v7a"
104
    GRADLE_ABI="ARMv7"
105
elif [ "$ANDROID_ABI" = "arm64-v8a" -o "$ANDROID_ABI" = "arm64" ]; then
106
    ANDROID_ABI="arm64-v8a"
107 108 109 110 111
    GRADLE_ABI="ARMv8"
elif [ "$ANDROID_ABI" = "x86" ]; then
    GRADLE_ABI="x86"
elif [ "$ANDROID_ABI" = "x86_64" ]; then
    GRADLE_ABI="x86_64"
112 113
elif [ "$ANDROID_ABI" = "all" ]; then
    GRADLE_ABI="all"
114 115 116 117 118 119
else
    diagnostic "Invalid arch specified: '$ANDROID_ABI'."
    diagnostic "Try --help for more information"
    exit 1
fi

120 121 122
####################
# Configure gradle #
####################
123

124 125 126 127 128
if [ -z "$KEYSTORE_FILE" ]; then
    KEYSTORE_FILE="$HOME/.android/debug.keystore"
    STOREALIAS="androiddebugkey"
else
    if [ -z "$PASSWORD_KEYSTORE" ]; then
129
        diagnostic "No password"
130 131 132 133 134 135
        exit 1
    fi
    rm -f gradle.properties
    STOREALIAS="vlc"
fi

136
if [ ! -f gradle.properties ]; then
137 138
    echo android.enableJetifier=true > gradle.properties
    echo android.useAndroidX=true >> gradle.properties
139 140 141
    echo kapt.incremental.apt=true >> gradle.properties
    echo kapt.use.worker.api=true >> gradle.properties
    echo kapt.include.compile.classpath=false >> gradle.properties
142
    echo keyStoreFile=$KEYSTORE_FILE >> gradle.properties
143
    echo storealias=$STOREALIAS >> gradle.properties
Geoffrey Métais's avatar
Geoffrey Métais committed
144
    if [ -z "$PASSWORD_KEYSTORE" ]; then
145 146
        echo storepwd=android >> gradle.properties
    fi
147
fi
148 149 150 151 152 153 154 155 156 157 158 159 160 161

init_local_props() {
    (
    # initialize the local.properties file,
    # or fix it if it was modified (by Android Studio, for example).
    echo_props() {
        echo "sdk.dir=$ANDROID_SDK"
        echo "ndk.dir=$ANDROID_NDK"
    }
    # first check if the file just needs to be created for the first time
    if [ ! -f "$1" ]; then
        echo_props > "$1"
        return 0
    fi
162 163 164 165 166 167
    # escape special chars to get regex that matches string
    make_regex() {
        echo "$1" | sed -e 's/\([[\^$.*]\)/\\\1/g' -
    }
    android_sdk_regex=`make_regex "${ANDROID_SDK}"`
    android_ndk_regex=`make_regex "${ANDROID_NDK}"`
168 169 170
    # check for lines setting the SDK directory
    sdk_line_start="^sdk\.dir="
    total_sdk_count=`grep -c "${sdk_line_start}" "$1"`
171
    good_sdk_count=`grep -c "${sdk_line_start}${android_sdk_regex}\$" "$1"`
172 173 174
    # check for lines setting the NDK directory
    ndk_line_start="^ndk\.dir="
    total_ndk_count=`grep -c "${ndk_line_start}" "$1"`
175
    good_ndk_count=`grep -c "${ndk_line_start}${android_ndk_regex}\$" "$1"`
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
    # if one of each is found and both match the environment vars, no action needed
    if [ "$total_sdk_count" -eq "1" -a "$good_sdk_count" -eq "1" \
	    -a "$total_ndk_count" -eq "1" -a "$good_ndk_count" -eq "1" ]
    then
        return 0
    fi
    # if neither property is set they can simply be appended to the file
    if [ "$total_sdk_count" -eq "0" -a "$total_ndk_count" -eq "0" ]; then
        echo_props >> "$1"
        return 0
    fi
    # if a property is set incorrectly or too many times,
    # remove all instances of both properties and append correct ones.
    replace_props() {
        temp_props="$1.tmp"
        while IFS= read -r LINE || [ -n "$LINE" ]; do
            line_sdk_dir="${LINE#sdk.dir=}"
            line_ndk_dir="${LINE#ndk.dir=}"
            if [ "x$line_sdk_dir" = "x$LINE" -a "x$line_ndk_dir" = "x$LINE" ]; then
                echo "$LINE"
            fi
        done <"$1" >"$temp_props"
        echo_props >> "$temp_props"
        mv -f -- "$temp_props" "$1"
    }
    echo "local.properties: Contains incompatible sdk.dir and/or ndk.dir properties. Replacing..."
    replace_props "$1"
    echo "local.properties: Finished replacing sdk.dir and/or ndk.dir with current environment variables."
    )
}
init_local_props local.properties || { echo "Error initializing local.properties"; exit $?; }

208 209
if [ ! -d "$ANDROID_SDK/licenses" ]; then
    mkdir "$ANDROID_SDK/licenses"
210
    echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_SDK/licenses/android-sdk-license"
211
    echo "d56f5187479451eabf01fb78af6dfcb131a6481e" >> "$ANDROID_SDK/licenses/android-sdk-license"
212
fi
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
##########
# GRADLE #
##########

if [ ! -d "gradle/wrapper" ]; then
    diagnostic "Downloading gradle"
    GRADLE_VERSION=5.1.1
    GRADLE_URL=https://download.videolan.org/pub/contrib/gradle/gradle-${GRADLE_VERSION}-bin.zip
    wget ${GRADLE_URL} 2>/dev/null || curl -O ${GRADLE_URL}
    checkfail "gradle: download failed"

    unzip -o gradle-${GRADLE_VERSION}-bin.zip
    checkfail "gradle: unzip failed"

    ./gradle-${GRADLE_VERSION}/bin/gradle wrapper
    checkfail "gradle: wrapper failed"

    ./gradlew -version
    checkfail "gradle: wrapper failed"
    chmod a+x gradlew
    rm -rf gradle-${GRADLE_VERSION}-bin.zip
fi

237
if [ "$GRADLE_SETUP" = 1 ]; then
238 239
    exit 0
fi
240 241 242
####################
# Fetch VLC source #
####################
243

244
TESTED_HASH=ee139ff
245
if [ ! -d "vlc" ]; then
246
    diagnostic "VLC source not found, cloning"
247
    git clone https://git.videolan.org/git/vlc/vlc-3.0.git vlc
248
    checkfail "vlc source: git clone failed"
249 250 251
    if [ "$RELEASE" != 1 ]; then
        cd vlc && git am ../libvlc/patches/vlc3/*.patch && cd ..
    fi
252 253 254 255 256
fi
diagnostic "VLC source found"
cd vlc
if ! git cat-file -e ${TESTED_HASH}; then
    cat 1>&2 << EOF
257 258 259 260
***
*** Error: Your vlc checkout does not contain the latest tested commit: ${TESTED_HASH}
***
EOF
261
    exit 1
262
fi
263 264
if [ "$RELEASE" = 1 ]; then
    git reset --hard ${TESTED_HASH}
265
    git am ../libvlc/patches/vlc3/*.patch
266 267 268
fi
cd ..

269

270 271 272
############
# Make VLC #
############
273
diagnostic "Configuring"
274
compile() {
275 276
    # Build LibVLC if asked for it, or needed by medialibrary
    copy_tmp="$1"
277

278 279 280
    OUT_DBG_DIR=.dbg/${ANDROID_ABI}
    mkdir -p $OUT_DBG_DIR

281
    if [ "$BUILD_MEDIALIB" != 1 -o ! -d "libvlc/jni/libs/$1" ]; then
282 283
        AVLC_SOURCED=1 . ./compile-libvlc.sh
        avlc_build
284

Thomas Guillem's avatar
Thomas Guillem committed
285
        $NDK_BUILD -C libvlc \
286 287 288 289 290 291 292 293 294 295 296 297 298 299
            VLC_SRC_DIR="$VLC_SRC_DIR" \
            VLC_BUILD_DIR="$VLC_BUILD_DIR" \
            VLC_OUT_LDLIBS="$VLC_OUT_LDLIBS" \
            APP_BUILD_SCRIPT=jni/Android.mk \
            APP_PLATFORM=android-${ANDROID_API} \
            APP_ABI=${ANDROID_ABI} \
            NDK_PROJECT_PATH=jni \
            NDK_TOOLCHAIN_VERSION=clang \
            NDK_DEBUG=${NDK_DEBUG}

        if [ "$copy_tmp" = "--copy-tmp=libvlc" ];then
            cp -r $VLC_OUT_PATH/libs/${ANDROID_ABI} libvlc/jni/libs/${ANDROID_ABI} build/tmp
        fi

300 301
        cp -a $VLC_OUT_PATH/obj/local/${ANDROID_ABI}/*.so ${OUT_DBG_DIR}
        cp -a ./libvlc/jni/obj/local/${ANDROID_ABI}/*.so ${OUT_DBG_DIR}
302
    fi
303

304
    if [ "$NO_ML" != 1 ]; then
305 306 307 308
        ANDROID_ABI=$ANDROID_ABI RELEASE=$RELEASE ./compile-medialibrary.sh
        if [ "$copy_tmp" = "--copy-tmp=medialibrary" ];then
            cp -r medialibrary/jni/libs/${ANDROID_ABI} build/tmp
        fi
309 310

        cp -a medialibrary/jni/obj/local/${ANDROID_ABI}/*.so ${OUT_DBG_DIR}
311 312
    fi
}
313

314
if [ "$ANDROID_ABI" = "all" ]; then
315 316
    if [ -d build/tmp ]; then
        rm -rf build/tmp
317
    fi
318
    mkdir -p build/tmp
319 320 321 322
    LIB_DIR="libvlc"
    if [ "$NO_ML" != 1 ]; then
        LIB_DIR="medialibrary"
    fi
323 324 325 326 327 328 329 330 331 332 333 334
    copy_tmp="--copy-tmp=$LIB_DIR"

    # The compile function is sourcing ./compile-libvlc.sh and is configured
    # with env variables (ANDROID_ABI), therefore it need to be run from a new
    # context for each ABI

    (ANDROID_ABI=armeabi-v7a RELEASE=$RELEASE compile $copy_tmp)
    (ANDROID_ABI=arm64-v8a RELEASE=$RELEASE compile $copy_tmp)
    (ANDROID_ABI=x86 RELEASE=$RELEASE compile $copy_tmp)
    (ANDROID_ABI=x86_64 RELEASE=$RELEASE compile $copy_tmp)
    rm -rf $LIB_DIR/jni/libs/
    mv build/tmp $LIB_DIR/jni/libs/
335 336

    GRADLE_VLC_SRC_DIRS="''"
337
else
338
    compile
339
    GRADLE_VLC_SRC_DIRS="$VLC_OUT_PATH/libs"
340
fi
341

342 343 344
##################
# Compile the UI #
##################
345
BUILDTYPE="Dev"
346 347 348
if [ "$SIGNED_RELEASE" = 1 ]; then
    BUILDTYPE="signedRelease"
elif [ "$RELEASE" = 1 ]; then
349 350
    BUILDTYPE="Release"
fi
351

352
if [ "$BUILD_LIBVLC" = 1 ];then
353
    GRADLE_VLC_SRC_DIRS="$GRADLE_VLC_SRC_DIRS" GRADLE_ABI=$GRADLE_ABI ./gradlew -p libvlc assemble${BUILDTYPE}
354
    RUN=0
355
    if [ "$PUBLISH" = 1 ];then
356
        GRADLE_VLC_SRC_DIRS="$GRADLE_VLC_SRC_DIRS" GRADLE_ABI=$GRADLE_ABI ./gradlew -p libvlc install bintrayUpload
357
    fi
358
elif [ "$BUILD_MEDIALIB" = 1 ]; then
359
    GRADLE_ABI=$GRADLE_ABI ./gradlew -p medialibrary assemble${BUILDTYPE}
360
    RUN=0
361 362 363
    if [ "$PUBLISH" = 1 ];then
        GRADLE_ABI=$GRADLE_ABI ./gradlew -p medialibrary install bintrayUpload
    fi
364
else
365 366 367 368 369
    if [ "$RUN" = 1 ]; then
        ACTION="install"
    else
        ACTION="assemble"
    fi
370
    TARGET="${ACTION}${BUILDTYPE}"
371
    GRADLE_VLC_SRC_DIRS="$GRADLE_VLC_SRC_DIRS" CLI="" GRADLE_ABI=$GRADLE_ABI ./gradlew $TARGET
372
fi
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
373

374 375 376 377
#######
# RUN #
#######
if [ "$RUN" = 1 ]; then
378
    export PATH="${ANDROID_SDK}/platform-tools/:$PATH"
Duncan McNAMARA's avatar
Duncan McNAMARA committed
379 380 381 382
    EXTRA=""
    if [ "$TEST" = 1 ]; then
        EXTRA="--ez 'extra_test_stubs' true"
    fi
383 384
    adb wait-for-device
    if [ "$RELEASE" = 1 ]; then
Duncan McNAMARA's avatar
Duncan McNAMARA committed
385
        adb shell am start -n org.videolan.vlc/org.videolan.vlc.StartActivity $EXTRA
386
    else
Duncan McNAMARA's avatar
Duncan McNAMARA committed
387
        adb shell am start -n org.videolan.vlc.debug/org.videolan.vlc.StartActivity $EXTRA
388 389
    fi
fi