onnxruntime/java/build-android.gradle
Adrian Lizarraga abfdb63e31
Update protobuf-java to version 3.21.7 (#13630)
### Description
Update protobuf-java to version 3.21.7. This change only impact tests.

### Motivation and Context
The current version exhibits CVE-2022-3509
2022-11-17 15:04:42 -08:00

204 lines
5.8 KiB
Groovy

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
def jniLibsDir = System.properties['jniLibsDir']
def buildDir = System.properties['buildDir']
def headersDir = System.properties['headersDir']
def publishDir = System.properties['publishDir']
def minSdkVer = System.properties['minSdkVer']
def targetSdkVer = System.properties['targetSdkVer']
def buildVariant = System.properties['buildVariant'] ?: "Full"
boolean isMobileBuild = (buildVariant == "Mobile")
// Since Android requires a higher numbers indicating more recent versions
// This function assume ORT version number will be in formart of A.B.C such as 1.7.0
// We generate version code A[0{0,1}]B[0{0,1}]C,
// for example '1.7.0' -> 10700, '1.6.15' -> 10615
def getVersionCode(String version){
String[] codes = version.split('\\.');
// This will have problem if we have 3 digit [sub]version number, such as 1.7.199
// but it is highly unlikely to happen
String versionCodeStr = String.format("%d%02d%02d", codes[0] as int, codes[1] as int, codes[2] as int);
return versionCodeStr as int;
}
project.buildDir = buildDir
project.version = rootProject.file('../VERSION_NUMBER').text.trim()
project.group = "com.microsoft.onnxruntime"
def mavenArtifactId = isMobileBuild ? project.name + '-mobile' : project.name + '-android'
def mobileDescription = 'The ONNX Runtime Mobile package is a size optimized inference library for executing ONNX ' +
'(Open Neural Network Exchange) models on Android. This package is built from the open source inference engine ' +
'but with reduced disk footprint targeting mobile platforms. To minimize binary size this library supports a ' +
'reduced set of operators and types aligned to typical mobile applications. The ONNX model must be converted to ' +
'ORT format in order to use it with this package. ' +
'See https://onnxruntime.ai/docs/reference/ort-model-format.html for more details.'
def defaultDescription = 'ONNX Runtime is a performance-focused inference engine for ONNX (Open Neural Network ' +
'Exchange) models. This package contains the Android (aar) build of ONNX Runtime. It includes support for all ' +
'types and operators, for ONNX format models. All standard ONNX models can be executed with this package. ' +
'As such the binary size and memory usage will be larger than the onnxruntime-mobile package.'
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
minSdkVersion minSdkVer
targetSdkVersion targetSdkVer
versionCode = getVersionCode(project.version)
versionName = project.version
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
android {
lintOptions {
abortOnError false
}
}
buildTypes {
release {
minifyEnabled false
debuggable false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
sourceSets {
main {
jniLibs.srcDirs = [jniLibsDir]
}
}
}
task sourcesJar(type: Jar) {
classifier "sources"
from android.sourceSets.main.java.srcDirs
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
testImplementation 'com.google.protobuf:protobuf-java:3.21.7'
}
publishing {
publications {
maven(MavenPublication) {
groupId = project.group
artifactId = mavenArtifactId
version = project.version
// Three artifacts, the `aar`, the sources and the javadoc
artifact("$buildDir/outputs/aar/${project.name}-release.aar")
artifact javadocJar
artifact sourcesJar
pom {
name = 'onnx-runtime'
description = isMobileBuild ? mobileDescription : defaultDescription
url = 'https://microsoft.github.io/onnxruntime/'
licenses {
license {
name = 'MIT License'
url = 'https://opensource.org/licenses/MIT'
}
}
organization {
name = 'Microsoft'
url = 'http://www.microsoft.com'
}
scm {
connection = 'scm:git:git://github.com:microsoft/onnxruntime.git'
developerConnection = 'scm:git:ssh://github.com/microsoft/onnxruntime.git'
url = 'http://github.com/microsoft/onnxruntime'
}
developers {
developer {
id = 'onnxruntime'
name = 'ONNX Runtime'
email = 'onnxruntime@microsoft.com'
}
}
}
}
}
//publish to filesystem repo
repositories{
maven {
url "$publishDir"
}
}
}
// Add ORT C and C++ API headers to the AAR package, after task bundleDebugAar or bundleReleaseAar
// Such that developers using ORT native API can extract libraries and headers from AAR package without building ORT
tasks.whenTaskAdded { task ->
if (task.name.startsWith("bundle") && task.name.endsWith("Aar")) {
doLast {
addFolderToAar("addHeadersTo" + task.name, task.archivePath, headersDir, 'headers')
}
}
}
def addFolderToAar(taskName, aarPath, folderPath, folderPathInAar) {
def tmpDir = file("${buildDir}/${taskName}")
tmpDir.mkdir()
def tmpDirFolder = file("${tmpDir.path}/${folderPathInAar}")
tmpDirFolder.mkdir()
copy {
from zipTree(aarPath)
into tmpDir
}
copy {
from fileTree(folderPath)
into tmpDirFolder
}
ant.zip(destfile: aarPath) {
fileset(dir: tmpDir.path)
}
delete tmpDir
}