diff --git a/.gitignore b/.gitignore index 739ec17ca2f..61fc6e474b8 100644 --- a/.gitignore +++ b/.gitignore @@ -41,9 +41,6 @@ onnxprofile_profile_test_*.json /csharp/src/Microsoft.ML.OnnxRuntime/targets/**/*.props cmake/external/FeaturizersLibrary/ # Java specific ignores -java/gradlew -java/gradlew.bat -java/gradle java/.gradle java/hs_*.log onnxruntime/python/version_info.py diff --git a/cmake/onnxruntime_java.cmake b/cmake/onnxruntime_java.cmake index 223f54f60cd..54f57fe24c3 100644 --- a/cmake/onnxruntime_java.cmake +++ b/cmake/onnxruntime_java.cmake @@ -19,17 +19,7 @@ else() set(JAVA_DEPENDS onnxruntime) endif() -# use the gradle wrapper if it exists -if(EXISTS "${JAVA_ROOT}/gradlew") - set(GRADLE_EXECUTABLE "${JAVA_ROOT}/gradlew") -else() - # fall back to gradle on our PATH - find_program(GRADLE_EXECUTABLE gradle) - if(NOT GRADLE_EXECUTABLE) - message(SEND_ERROR "Gradle installation not found") - endif() -endif() -message(STATUS "Using gradle: ${GRADLE_EXECUTABLE}") +set(GRADLE_EXECUTABLE "${JAVA_ROOT}/gradlew") # Specify the Java source files file(GLOB_RECURSE onnxruntime4j_gradle_files "${JAVA_ROOT}/*.gradle") @@ -219,7 +209,16 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Android") add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $ ${ANDROID_PACKAGE_ABI_DIR}/$) add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $ ${ANDROID_PACKAGE_ABI_DIR}/$) # Generate the Android AAR package - add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${GRADLE_EXECUTABLE} -b build-android.gradle -c settings-android.gradle build -DjniLibsDir=${ANDROID_PACKAGE_JNILIBS_DIR} -DbuildDir=${ANDROID_PACKAGE_OUTPUT_DIR} -DminSdkVer=${ANDROID_MIN_SDK} -DheadersDir=${ANDROID_HEADERS_DIR} WORKING_DIRECTORY ${JAVA_ROOT}) + add_custom_command(TARGET onnxruntime4j_jni + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E echo "Generating Android AAR package..." + COMMAND ${GRADLE_EXECUTABLE} + build + -b build-android.gradle -c settings-android.gradle + -DjniLibsDir=${ANDROID_PACKAGE_JNILIBS_DIR} -DbuildDir=${ANDROID_PACKAGE_OUTPUT_DIR} + -DminSdkVer=${ANDROID_MIN_SDK} -DheadersDir=${ANDROID_HEADERS_DIR} + --stacktrace + WORKING_DIRECTORY ${JAVA_ROOT}) if (onnxruntime_BUILD_UNIT_TESTS) set(ANDROID_TEST_PACKAGE_ROOT ${JAVA_ROOT}/src/test/android) @@ -232,7 +231,13 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Android") # Copy the built Android AAR package to libs folder of our test app add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ANDROID_PACKAGE_OUTPUT_DIR}/outputs/aar/onnxruntime-debug.aar ${ANDROID_TEST_PACKAGE_LIB_DIR}/onnxruntime-mobile.aar) # Build Android test apk for java package - add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${GRADLE_EXECUTABLE} clean WORKING_DIRECTORY ${ANDROID_TEST_PACKAGE_DIR}) - add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${GRADLE_EXECUTABLE} assembleDebug assembleDebugAndroidTest -DminSdkVer=${ANDROID_MIN_SDK} WORKING_DIRECTORY ${ANDROID_TEST_PACKAGE_DIR}) + add_custom_command(TARGET onnxruntime4j_jni + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E echo "Building and running Android test for Android AAR package..." + COMMAND ${GRADLE_EXECUTABLE} + clean assembleDebug assembleDebugAndroidTest + -DminSdkVer=${ANDROID_MIN_SDK} + --stacktrace + WORKING_DIRECTORY ${ANDROID_TEST_PACKAGE_DIR}) endif() endif() diff --git a/cmake/onnxruntime_unittests.cmake b/cmake/onnxruntime_unittests.cmake index e656c5f2a9f..06812157473 100644 --- a/cmake/onnxruntime_unittests.cmake +++ b/cmake/onnxruntime_unittests.cmake @@ -1477,9 +1477,7 @@ if (NOT onnxruntime_BUILD_WEBASSEMBLY) add_custom_command(TARGET custom_op_library POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $ ${JAVA_NATIVE_TEST_DIR}/$) # On windows ctest requires a test to be an .exe(.com) file - # So there are two options 1) Install Chocolatey and its gradle package - # That package would install gradle.exe shim to its bin so ctest could run gradle.exe - # 2) With standard installation we get gradle.bat. We delegate execution to a separate .cmake file + # With gradle wrapper we get gradlew.bat. We delegate execution to a separate .cmake file # That can handle both .exe and .bat add_test(NAME onnxruntime4j_test COMMAND ${CMAKE_COMMAND} -DGRADLE_EXECUTABLE=${GRADLE_EXECUTABLE} diff --git a/java/README.md b/java/README.md index 903747b4b37..2ce9a8bf62e 100644 --- a/java/README.md +++ b/java/README.md @@ -15,12 +15,10 @@ Use the main project's [build instructions](https://www.onnxruntime.ai/docs/how- #### Requirements JDK version 8 or later is required. -The [Gradle](https://gradle.org/) build system is required and used here to manage the Java project's dependency management, compilation, testing, and assembly. -You may use your system Gradle installation installed on your PATH. -Version 6 or newer is recommended. -Optionally, you may use your own Gradle [wrapper](https://docs.gradle.org/current/userguide/gradle_wrapper.html) which will be locked to a version specified in the `build.gradle` configuration. -This can be done once by using system Gradle installation to invoke the wrapper task in the java project's directory: `cd REPO_ROOT/java && gradle wrapper` -Any installed wrapper is gitignored. + +The [Gradle](https://gradle.org/) build system is used here to manage the Java project's dependency management, compilation, testing, and assembly. +In particular, the Gradle [wrapper](https://docs.gradle.org/current/userguide/gradle_wrapper.html) at `java/gradlew[.bat]` is used, locking the Gradle version to the one specified in the `java/gradle/wrapper/gradle-wrapper.properties` configuration. +Using the Gradle wrapper removes the need to have the right version of Gradle installed on the system. #### Build Output @@ -30,7 +28,7 @@ The build will generate output in `$REPO_ROOT/build/$OS/$CONFIGURATION/java/buil * `reports/` - detailed test results and other reports * `libs/onnxruntime-VERSION.jar` - JAR with compiled classes, platform-specific JNI shared library, and platform-specific onnxruntime shared library. -#### Build System Overview +#### Build System Overview The main CMake build system delegates building and testing to Gradle. This allows the CMake system to ensure all of the C/C++ compilation is achieved prior to the Java build. diff --git a/java/build-android.gradle b/java/build-android.gradle index a6648c1f6ee..3b1da3cb221 100644 --- a/java/build-android.gradle +++ b/java/build-android.gradle @@ -44,7 +44,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:4.0.1' + classpath 'com.android.tools.build:gradle:7.4.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -59,8 +59,7 @@ allprojects { } android { - compileSdkVersion 29 - buildToolsVersion "29.0.2" + compileSdkVersion 32 defaultConfig { minSdkVersion minSdkVer @@ -96,10 +95,11 @@ android { } } + namespace 'ai.onnxruntime' } task sourcesJar(type: Jar) { - classifier "sources" + archiveClassifier = "sources" from android.sourceSets.main.java.srcDirs } @@ -109,7 +109,7 @@ task javadoc(type: Javadoc) { } task javadocJar(type: Jar, dependsOn: javadoc) { - classifier = 'javadoc' + archiveClassifier = 'javadoc' from javadoc.destinationDir } diff --git a/java/build.gradle b/java/build.gradle index 1e6c04e2bd6..3b76a2e13d1 100644 --- a/java/build.gradle +++ b/java/build.gradle @@ -62,10 +62,6 @@ task javadocJar(type: Jar, dependsOn: javadoc) { } } -wrapper { - gradleVersion = '6.1.1' -} - spotless { java { removeUnusedImports() diff --git a/java/gradle/wrapper/gradle-wrapper.jar b/java/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000000..ccebba7710d Binary files /dev/null and b/java/gradle/wrapper/gradle-wrapper.jar differ diff --git a/java/gradle/wrapper/gradle-wrapper.properties b/java/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000000..f396aaac2d3 --- /dev/null +++ b/java/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionSha256Sum=1b6b558be93f29438d3df94b7dfee02e794b94d9aca4611a92cdb79b6b88e909 +distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.1-bin.zip +networkTimeout=10000 +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/java/gradlew b/java/gradlew new file mode 100755 index 00000000000..79a61d421cc --- /dev/null +++ b/java/gradlew @@ -0,0 +1,244 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# 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 +# +# https://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. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/java/gradlew.bat b/java/gradlew.bat new file mode 100644 index 00000000000..93e3f59f135 --- /dev/null +++ b/java/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/java/src/main/AndroidManifest.xml b/java/src/main/AndroidManifest.xml index 7803ad9963f..94cbbcfc396 100644 --- a/java/src/main/AndroidManifest.xml +++ b/java/src/main/AndroidManifest.xml @@ -1,2 +1 @@ - + diff --git a/java/src/test/android/README.md b/java/src/test/android/README.md index 874ba7ba729..b84021669c9 100644 --- a/java/src/test/android/README.md +++ b/java/src/test/android/README.md @@ -19,8 +19,9 @@ This android application is mainly aimed for testing: ### Requirements -- JDK version 8 or later is required. -- The [Gradle](https://gradle.org/) build system is required for building the APKs used to run [android instrumentation tests](https://source.android.com/compatibility/tests/development/instrumentation). Version 6 or newer is required. +- JDK version 11 or later is required. +- The [Gradle](https://gradle.org/) build system is required for building the APKs used to run [android instrumentation tests](https://source.android.com/compatibility/tests/development/instrumentation). Version 7.5 or newer is required. + The Gradle wrapper at `java/gradlew[.bat]` may be used. ### Building diff --git a/java/src/test/android/app/build.gradle b/java/src/test/android/app/build.gradle index be4a495e8f4..ccb24e92a1b 100644 --- a/java/src/test/android/app/build.gradle +++ b/java/src/test/android/app/build.gradle @@ -6,13 +6,12 @@ plugins { def minSdkVer = System.properties.get("minSdkVer")?:24 android { - compileSdkVersion 30 - buildToolsVersion "30.0.3" + compileSdkVersion 32 defaultConfig { applicationId "ai.onnxruntime.example.javavalidator" minSdkVersion minSdkVer - targetSdkVersion 30 + targetSdkVersion 32 versionCode 1 versionName "1.0" @@ -32,6 +31,7 @@ android { kotlinOptions { jvmTarget = '1.8' } + namespace 'ai.onnxruntime.example.javavalidator' } dependencies { diff --git a/java/src/test/android/app/src/main/AndroidManifest.xml b/java/src/test/android/app/src/main/AndroidManifest.xml index 500346db3be..2938b7e8bf4 100644 --- a/java/src/test/android/app/src/main/AndroidManifest.xml +++ b/java/src/test/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,5 @@ - + - + diff --git a/java/src/test/android/build.gradle b/java/src/test/android/build.gradle index daa964ea226..d7672e1e15a 100644 --- a/java/src/test/android/build.gradle +++ b/java/src/test/android/build.gradle @@ -1,13 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = "1.4.32" + ext.kotlin_version = '1.6.21' repositories { google() mavenCentral() } dependencies { - classpath "com.android.tools.build:gradle:4.0.1" + classpath 'com.android.tools.build:gradle:7.4.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/java/src/test/android/gradle.properties b/java/src/test/android/gradle.properties index 98bed167dc9..aa69f30e630 100644 --- a/java/src/test/android/gradle.properties +++ b/java/src/test/android/gradle.properties @@ -6,7 +6,7 @@ # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. -org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 +org.gradle.jvmargs=-Xmx4096m -Dfile.encoding=UTF-8 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects @@ -18,4 +18,4 @@ android.useAndroidX=true # Automatically convert third-party libraries to use AndroidX android.enableJetifier=true # Kotlin code style for this project: "official" or "obsolete": -kotlin.code.style=official \ No newline at end of file +kotlin.code.style=official diff --git a/js/react_native/android/gradle/wrapper/gradle-wrapper.properties b/js/react_native/android/gradle/wrapper/gradle-wrapper.properties index 442d9132ea3..51d930a381f 100644 --- a/js/react_native/android/gradle/wrapper/gradle-wrapper.properties +++ b/js/react_native/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionSha256Sum=7faa7198769f872826c8ef4f1450f839ec27f0b4d5d1e51bade63667cbccd205 distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/js/react_native/e2e/android/gradle/wrapper/gradle-wrapper.properties b/js/react_native/e2e/android/gradle/wrapper/gradle-wrapper.properties index 442d9132ea3..51d930a381f 100644 --- a/js/react_native/e2e/android/gradle/wrapper/gradle-wrapper.properties +++ b/js/react_native/e2e/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionSha256Sum=7faa7198769f872826c8ef4f1450f839ec27f0b4d5d1e51bade63667cbccd205 distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/tools/android_custom_build/Dockerfile b/tools/android_custom_build/Dockerfile index f98b160ee40..e2151c94cd6 100644 --- a/tools/android_custom_build/Dockerfile +++ b/tools/android_custom_build/Dockerfile @@ -53,6 +53,8 @@ RUN CMAKE_VERSION=3.25.2 && \ tar -zxf /tmp/cmake-${CMAKE_VERSION}-linux-x86_64.tar.gz --strip=1 -C /usr # gradle +# Note: A system-installed gradle is not required in more recent versions of ORT which use an included gradle wrapper, +# but keep it around to support building older versions. RUN GRADLE_VERSION=6.8.3 && \ aria2c -q -d /tmp -o gradle-${GRADLE_VERSION}-bin.zip \ --checksum=sha-256=7faa7198769f872826c8ef4f1450f839ec27f0b4d5d1e51bade63667cbccd205 \ diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index 7617a8afad1..4b3ad8f896a 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -1580,7 +1580,7 @@ def run_android_tests(args, source_dir, build_dir, config, cwd): with contextlib.ExitStack() as context_stack: if args.android_run_emulator: avd_name = "ort_android" - system_image = "system-images;android-{};google_apis;{}".format(args.android_api, args.android_abi) + system_image = "system-images;android-{};default;{}".format(args.android_api, args.android_abi) android.create_virtual_device(sdk_tool_paths, system_image, avd_name) emulator_proc = context_stack.enter_context( @@ -1603,11 +1603,8 @@ def run_android_tests(args, source_dir, build_dir, config, cwd): run_adb_shell("{0}/onnxruntime_test_all".format(device_dir)) if args.build_java: - gradle_executable = "gradle" - # use the gradle wrapper if it exists, the gradlew should be setup under /java - gradlew_path = os.path.join(source_dir, "java", "gradlew.bat" if is_windows() else "gradlew") - if os.path.exists(gradlew_path): - gradle_executable = gradlew_path + # use the gradle wrapper under /java + gradle_executable = os.path.join(source_dir, "java", "gradlew.bat" if is_windows() else "gradlew") android_test_path = os.path.join(cwd, "java", "androidtest", "android") run_subprocess( [ diff --git a/tools/ci_build/github/android/build_aar_package.py b/tools/ci_build/github/android/build_aar_package.py index c0cacb42316..c36d74402d4 100644 --- a/tools/ci_build/github/android/build_aar_package.py +++ b/tools/ci_build/github/android/build_aar_package.py @@ -134,9 +134,11 @@ def _build_aar(args): aar_publish_dir = os.path.join(build_dir, "aar_out", build_config) os.makedirs(aar_publish_dir, exist_ok=True) + gradle_path = os.path.join(JAVA_ROOT, "gradlew" if not is_windows() else "gradlew.bat") + # get the common gradle command args gradle_command = [ - "gradle", + gradle_path, "--no-daemon", "-b=build-android.gradle", "-c=settings-android.gradle", @@ -149,13 +151,10 @@ def _build_aar(args): "-DbuildVariant=" + str(build_settings["build_variant"]), ] - # If not using shell on Window, will not be able to find gradle in path - use_shell = True if is_windows() else False - # clean, build, and publish to a local directory - subprocess.run(gradle_command + ["clean"], env=temp_env, shell=use_shell, check=True, cwd=JAVA_ROOT) - subprocess.run(gradle_command + ["build"], env=temp_env, shell=use_shell, check=True, cwd=JAVA_ROOT) - subprocess.run(gradle_command + ["publish"], env=temp_env, shell=use_shell, check=True, cwd=JAVA_ROOT) + subprocess.run(gradle_command + ["clean"], env=temp_env, shell=False, check=True, cwd=JAVA_ROOT) + subprocess.run(gradle_command + ["build"], env=temp_env, shell=False, check=True, cwd=JAVA_ROOT) + subprocess.run(gradle_command + ["publish"], env=temp_env, shell=False, check=True, cwd=JAVA_ROOT) def parse_args(): diff --git a/tools/ci_build/github/azure-pipelines/android-x86_64-crosscompile-ci-pipeline.yml b/tools/ci_build/github/azure-pipelines/android-x86_64-crosscompile-ci-pipeline.yml index 4fd8d915f84..95152add331 100644 --- a/tools/ci_build/github/azure-pipelines/android-x86_64-crosscompile-ci-pipeline.yml +++ b/tools/ci_build/github/azure-pipelines/android-x86_64-crosscompile-ci-pipeline.yml @@ -46,8 +46,6 @@ stages: - template: "templates/use-android-ndk.yml" - - template: templates/set-up-gradle-wrapper-step.yml - # We build the host protoc to /protobuf_install - script: | /bin/bash $(Build.SourcesDirectory)/tools/ci_build/github/apple/build_host_protoc.sh \ @@ -130,8 +128,6 @@ stages: - template: "templates/use-android-ndk.yml" - - template: templates/set-up-gradle-wrapper-step.yml - # We build the host protoc to /protobuf_install - script: | /bin/bash $(Build.SourcesDirectory)/tools/ci_build/github/apple/build_host_protoc.sh \ @@ -233,12 +229,10 @@ stages: - template: "templates/use-android-ndk.yml" - - template: templates/set-up-gradle-wrapper-step.yml - - script: | python3 tools/python/run_android_emulator.py \ --android-sdk-root ${ANDROID_SDK_ROOT} \ - --create-avd --system-image "system-images;android-30;google_apis;x86_64" \ + --create-avd --system-image "system-images;android-31;default;x86_64" \ --start --emulator-extra-args="-partition-size 4096" \ --emulator-pid-file $(Build.BinariesDirectory)/emulator.pid displayName: Start Android emulator @@ -305,12 +299,10 @@ stages: - template: "templates/use-android-ndk.yml" - - template: templates/set-up-gradle-wrapper-step.yml - - script: | python3 tools/python/run_android_emulator.py \ --android-sdk-root ${ANDROID_SDK_ROOT} \ - --create-avd --system-image "system-images;android-30;google_apis;x86_64" \ + --create-avd --system-image "system-images;android-31;default;x86_64" \ --start --emulator-extra-args="-partition-size 4096" \ --emulator-pid-file $(Build.BinariesDirectory)/emulator.pid displayName: Start Android emulator @@ -387,8 +379,6 @@ stages: - template: "templates/use-android-ndk.yml" - - template: templates/set-up-gradle-wrapper-step.yml - # used by Build Minimal ORT - script: brew install coreutils ninja displayName: Install coreutils and ninja @@ -404,7 +394,7 @@ stages: - script: | python3 tools/python/run_android_emulator.py \ --android-sdk-root ${ANDROID_SDK_ROOT} \ - --create-avd --system-image "system-images;android-29;google_apis;x86_64" \ + --create-avd --system-image "system-images;android-31;default;x86_64" \ --start --emulator-extra-args="-partition-size 4096" \ --emulator-pid-file $(Build.BinariesDirectory)/emulator.pid displayName: Start Android emulator diff --git a/tools/ci_build/github/azure-pipelines/linux-ci-pipeline.yml b/tools/ci_build/github/azure-pipelines/linux-ci-pipeline.yml index 34c9a3a7ab1..728502552a6 100644 --- a/tools/ci_build/github/azure-pipelines/linux-ci-pipeline.yml +++ b/tools/ci_build/github/azure-pipelines/linux-ci-pipeline.yml @@ -105,7 +105,7 @@ stages: cp $(Build.SourcesDirectory)/tools/ci_build/github/linux/docker/scripts/training/ortmodule/stage1/requirements_torch_cpu/requirements.txt $(Build.BinariesDirectory)/requirements_torch_cpu/requirements.txt python3 -m pip install -r $(Build.BinariesDirectory)/requirements_torch_cpu/requirements.txt cd $(Build.SourcesDirectory)/java - /usr/local/gradle/bin/gradle "cmakeCheck" "-DcmakeBuildDir=$(Build.BinariesDirectory)/Release" + $(Build.SourcesDirectory)/java/gradlew "cmakeCheck" "-DcmakeBuildDir=$(Build.BinariesDirectory)/Release" - task: CmdLine@2 displayName: 'Install Release python package' @@ -187,7 +187,7 @@ stages: devtoolset_rootpath: /opt/rh/devtoolset-10/root ld_library_path_arg: /opt/rh/devtoolset-10/root/usr/lib64:/opt/rh/devtoolset-10/root/usr/lib:/opt/rh/devtoolset-10/root/usr/lib64/dyninst:/opt/rh/devtoolset-10/root/usr/lib/dyninst:/usr/local/lib64 prepend_path: '/opt/rh/devtoolset-10/root/usr/bin:' - + - stage: arm64_test dependsOn: ['arm64_build'] jobs: @@ -195,4 +195,4 @@ stages: parameters: arch: 'aarch64' machine_pool: 'onnxruntime-linux-ARM64-CPU-2019' - device: 'CPU' \ No newline at end of file + device: 'CPU' diff --git a/tools/ci_build/github/azure-pipelines/linux-gpu-ci-pipeline.yml b/tools/ci_build/github/azure-pipelines/linux-gpu-ci-pipeline.yml index 6652eb6f400..6fcd846548c 100644 --- a/tools/ci_build/github/azure-pipelines/linux-gpu-ci-pipeline.yml +++ b/tools/ci_build/github/azure-pipelines/linux-gpu-ci-pipeline.yml @@ -133,7 +133,7 @@ jobs: # Restore file permissions xargs -a $(Build.BinariesDirectory)/Release/perms.txt chmod a+x cd $(Build.SourcesDirectory)/java - /usr/local/gradle/bin/gradle "cmakeCheck" "-DcmakeBuildDir=$(Build.BinariesDirectory)/Release" "-DUSE_CUDA=1" + $(Build.SourcesDirectory)/java/gradlew "cmakeCheck" "-DcmakeBuildDir=$(Build.BinariesDirectory)/Release" "-DUSE_CUDA=1" cd /tmp python3 $(Build.SourcesDirectory)/tools/ci_build/build.py \ --build_dir $(Build.BinariesDirectory) --cmake_generator Ninja \ diff --git a/tools/ci_build/github/azure-pipelines/orttraining-linux-ci-pipeline.yml b/tools/ci_build/github/azure-pipelines/orttraining-linux-ci-pipeline.yml index 3793c9ed8f6..c47de7075e5 100644 --- a/tools/ci_build/github/azure-pipelines/orttraining-linux-ci-pipeline.yml +++ b/tools/ci_build/github/azure-pipelines/orttraining-linux-ci-pipeline.yml @@ -77,7 +77,7 @@ jobs: python3 -m pip install -r $(Build.BinariesDirectory)/requirements_torch_cpu/requirements.txt ln -sf /data/models $(Build.BinariesDirectory) cd $(Build.SourcesDirectory)/java - /usr/local/gradle/bin/gradle "cmakeCheck" "-DcmakeBuildDir=$(Build.BinariesDirectory)/Release" + $(Build.SourcesDirectory)/java/gradlew "cmakeCheck" "-DcmakeBuildDir=$(Build.BinariesDirectory)/Release" - task: CmdLine@2 displayName: 'Install Release python package' diff --git a/tools/ci_build/github/azure-pipelines/templates/android-java-api-aar-test.yml b/tools/ci_build/github/azure-pipelines/templates/android-java-api-aar-test.yml index 445a633d89d..be0bc01f482 100644 --- a/tools/ci_build/github/azure-pipelines/templates/android-java-api-aar-test.yml +++ b/tools/ci_build/github/azure-pipelines/templates/android-java-api-aar-test.yml @@ -37,14 +37,19 @@ jobs: artifactName: '${{parameters.artifactName}}' targetPath: '$(Build.BinariesDirectory)/final-android-aar' - - template: use-android-ndk.yml + - task: JavaToolInstaller@0 + displayName: Use jdk 11 + inputs: + versionSpec: '11' + jdkArchitectureOption: 'x64' + jdkSourceOption: 'PreInstalled' - - template: set-up-gradle-wrapper-step.yml + - template: use-android-ndk.yml - script: | python3 $(Build.SourcesDirectory)/tools/python/run_android_emulator.py \ --android-sdk-root ${ANDROID_SDK_ROOT} \ - --create-avd --system-image "system-images;android-30;google_apis;x86_64" \ + --create-avd --system-image "system-images;android-31;default;x86_64" \ --start --emulator-extra-args="-partition-size 4096" \ --emulator-pid-file $(Build.BinariesDirectory)/emulator.pid displayName: Start Android emulator @@ -57,7 +62,7 @@ jobs: cd ./android mkdir -p app/libs cp $(Build.BinariesDirectory)/final-android-aar/${{parameters.packageName}}-$(OnnxRuntimeVersion).aar app/libs/onnxruntime-mobile.aar - $(Build.SourcesDirectory)/java/gradlew --no-daemon clean connectedDebugAndroidTest + $(Build.SourcesDirectory)/java/gradlew --no-daemon clean connectedDebugAndroidTest --stacktrace displayName: Run E2E test using Emulator workingDirectory: $(Build.BinariesDirectory) diff --git a/tools/ci_build/github/azure-pipelines/templates/mac-ci.yml b/tools/ci_build/github/azure-pipelines/templates/mac-ci.yml index ea2f413b2b8..280c3210330 100644 --- a/tools/ci_build/github/azure-pipelines/templates/mac-ci.yml +++ b/tools/ci_build/github/azure-pipelines/templates/mac-ci.yml @@ -50,8 +50,6 @@ jobs: - template: set-version-number-variables-step.yml - - template: set-up-gradle-wrapper-step.yml - - script: | brew install ccache echo "##vso[task.prependpath]/usr/local/opt/ccache/libexec" diff --git a/tools/ci_build/github/azure-pipelines/templates/react-native-ci.yml b/tools/ci_build/github/azure-pipelines/templates/react-native-ci.yml index 905f2ec2f99..f6cd93552c1 100644 --- a/tools/ci_build/github/azure-pipelines/templates/react-native-ci.yml +++ b/tools/ci_build/github/azure-pipelines/templates/react-native-ci.yml @@ -60,8 +60,6 @@ jobs: brew install coreutils ninja npm yarn displayName: Install coreutils, ninja, npm, and yarn - - template: set-up-gradle-wrapper-step.yml - - script: | python3 -m pip install -q flatbuffers displayName: Install python modules @@ -131,7 +129,7 @@ jobs: - script: | python3 tools/python/run_android_emulator.py \ --android-sdk-root $(ANDROID_SDK_ROOT) \ - --create-avd --system-image "system-images;android-30;google_apis;x86_64" \ + --create-avd --system-image "system-images;android-31;default;x86_64" \ --start --emulator-extra-args="-partition-size 4096" \ --emulator-pid-file $(Build.BinariesDirectory)/emulator.pid displayName: Start Android Emulator diff --git a/tools/ci_build/github/azure-pipelines/templates/set-up-gradle-wrapper-step.yml b/tools/ci_build/github/azure-pipelines/templates/set-up-gradle-wrapper-step.yml deleted file mode 100644 index 84092882463..00000000000 --- a/tools/ci_build/github/azure-pipelines/templates/set-up-gradle-wrapper-step.yml +++ /dev/null @@ -1,10 +0,0 @@ -parameters: -- name: GradleWrapperVersion - type: string - default: "6.8.3" - -steps: -- script: | - gradle wrapper --gradle-version="${{ parameters.GradleWrapperVersion }}" --no-daemon --no-watch-fs - displayName: Set up gradle ${{ parameters.GradleWrapperVersion }} wrapper - workingDirectory: $(Build.SourcesDirectory)/java diff --git a/tools/ci_build/github/azure-pipelines/templates/win-ci.yml b/tools/ci_build/github/azure-pipelines/templates/win-ci.yml index dad7d23f57b..576db634a74 100644 --- a/tools/ci_build/github/azure-pipelines/templates/win-ci.yml +++ b/tools/ci_build/github/azure-pipelines/templates/win-ci.yml @@ -52,7 +52,7 @@ parameters: type: string default: 'onnxruntime' -- name: UseIncreasedTimeoutForTests +- name: UseIncreasedTimeoutForTests displayName: Increase timeout for tests? Set it to false if you are doing an Onnx Runtime release. type: boolean default: false @@ -63,9 +63,9 @@ jobs: clean: all pool: ${{ parameters.ort_build_pool_name }} ${{ if eq(parameters['UseIncreasedTimeoutForTests'], 'true') }}: - timeoutInMinutes: 1200 + timeoutInMinutes: 1200 ${{ else }}: - timeoutInMinutes: 300 + timeoutInMinutes: 300 steps: - checkout: self @@ -73,11 +73,11 @@ jobs: submodules: none - template: telemetry-steps.yml - + - task: UsePythonVersion@0 - inputs: - versionSpec: '3.7' - addToPath: true + inputs: + versionSpec: '3.7' + addToPath: true architecture: ${{ parameters.buildArch }} - task: NodeTool@0 @@ -119,7 +119,7 @@ jobs: ${{ else }}: arguments: '--config RelWithDebInfo --enable_lto --disable_rtti --build_dir $(Build.BinariesDirectory) --skip_submodule_sync --build_shared_lib --update --cmake_generator "Visual Studio 16 2019" --enable_onnx_tests $(TelemetryOption) ${{ parameters.buildparameter }} ' workingDirectory: '$(Build.BinariesDirectory)' - + - task: VSBuild@1 displayName: 'Build' inputs: @@ -144,7 +144,7 @@ jobs: dir *.dll mkdir $(Build.ArtifactStagingDirectory)\testdata workingDirectory: '$(Build.BinariesDirectory)/RelWithDebInfo/RelWithDebInfo' - displayName: 'List built DLLs' + displayName: 'List built DLLs' - template: c-api-artifacts-package-and-publish-steps-windows.yml parameters: @@ -166,7 +166,7 @@ jobs: condition: and(succeeded(), eq('${{ parameters.packageName}}', 'x64')) inputs: targetPath: '$(Build.BinariesDirectory)\RelWithDebInfo\installed\bin\protoc.exe' - artifactName: 'drop-extra' + artifactName: 'drop-extra' - task: CopyFiles@2 @@ -187,11 +187,12 @@ jobs: - task: CmdLine@2 condition: and(succeeded(), eq('${{ parameters.buildJava}}', true)) + displayName: 'Add symbols and notices to Java' inputs: script: | @echo on cd $(Build.SourcesDirectory)\java - gradle cmakeCheck -DcmakeBuildDir=$(Build.BinariesDirectory)\RelWithDebInfo + call $(Build.SourcesDirectory)\java\gradlew.bat cmakeCheck -DcmakeBuildDir=$(Build.BinariesDirectory)\RelWithDebInfo if %errorlevel% neq 0 exit /b %errorlevel% cd $(Build.BinariesDirectory)\RelWithDebInfo set NATIVE_FOLDER=$(Build.BinariesDirectory)\onnxruntime-java-win-${{ parameters.msbuildPlatform }}\stage\ai\onnxruntime\native\win-x64 @@ -225,14 +226,12 @@ jobs: popd rd /s /q $(Build.BinariesDirectory)\onnxruntime-java-win-${{ parameters.msbuildPlatform }}\stage dir /s /b $(Build.BinariesDirectory)\onnxruntime-java-win-${{ parameters.msbuildPlatform }} - workingDirectory: '$(Build.BinariesDirectory)\RelWithDebInfo' - displayName: 'Add symbols and notices to Java' - + - task: PublishBuildArtifacts@1 condition: and(succeeded(), eq('${{ parameters.buildJava}}', true)) displayName: 'Publish Java temp binaries' inputs: - pathtoPublish: '$(Build.BinariesDirectory)\onnxruntime-java-win-${{ parameters.msbuildPlatform }}' + pathtoPublish: '$(Build.BinariesDirectory)\onnxruntime-java-win-${{ parameters.msbuildPlatform }}' artifactName: 'drop-onnxruntime-java-win-${{ parameters.packageName }}' - ${{ if eq(parameters['DoCompliance'], 'true') }}: @@ -241,8 +240,8 @@ jobs: inputs: sourceCodeDirectory: '$(Build.SourcesDirectory)\java' language: 'java' - cleanupBuildCommands: 'C:\ProgramData\chocolatey\bin\gradle.exe -Dorg.gradle.daemon=false clean' - buildCommands: 'C:\ProgramData\chocolatey\bin\gradle.exe -Dorg.gradle.daemon=false jar' + cleanupBuildCommands: '$(Build.SourcesDirectory)\java\gradlew.bat -Dorg.gradle.daemon=false clean' + buildCommands: '$(Build.SourcesDirectory)\java\gradlew.bat -Dorg.gradle.daemon=false jar' querySuite: 'Recommended' timeout: '7200' ram: '16384' @@ -268,7 +267,7 @@ jobs: **/*.obj **/*.pdb **/*.dll - + #Manually set msBuildCommandline so that we can also set CAExcludePath - task: SDLNativeRules@3 displayName: 'Run the PREfast SDL Native Rules for MSBuild' diff --git a/tools/python/run_android_emulator.py b/tools/python/run_android_emulator.py index 31793fbbc13..cecb83b68e0 100755 --- a/tools/python/run_android_emulator.py +++ b/tools/python/run_android_emulator.py @@ -28,7 +28,7 @@ def parse_args(): parser.add_argument("--android-sdk-root", required=True, help="Path to the Android SDK root.") parser.add_argument( "--system-image", - default="system-images;android-29;google_apis;x86_64", + default="system-images;android-31;default;x86_64", help="The Android system image package name.", ) parser.add_argument("--avd-name", default="ort_android", help="The Android virtual device name.")