File: build-android.gradle

package info (click to toggle)
onnxruntime 1.23.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 340,744 kB
  • sloc: cpp: 3,222,135; python: 188,267; ansic: 114,318; asm: 37,927; cs: 36,849; java: 10,962; javascript: 6,811; pascal: 4,126; sh: 2,996; xml: 705; objc: 281; makefile: 67
file content (241 lines) | stat: -rw-r--r-- 7,934 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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']
boolean enableTrainingApis = (System.properties['ENABLE_TRAINING_APIS'] ?: "0") == "1"
def releaseVersionSuffix = System.properties['releaseVersionSuffix'] ?: ""
// Expected format for qnnVersion: major.minor.patch (e.g., 2.26.0)
// QNN package version does not follow Semantic Versioning (SemVer) format.
// For non qnn builds, qnnVersion will be null
def qnnVersion = System.properties['qnnVersion']

// Since Android requires higher numbers indicating more recent versions
// This function assumes ORT version number will be in the format of A.B.C[-rc/beta/alpha.D] such as 1.20.0 or 1.20.0-rc.1
// We generate version code A[0{0,1}]B[0{0,1}]C[0{0,1}]{1,2,3,4}D[01-99]
// for example '1.20.0' -> 12000400, '1.20.0-rc.1 ' -> 12000301
// '1.20.0-beta.1' -> 12000201, '1.20.0-alpha.1' -> 12000101
def getVersionCode(String version) {
	String[] versionAndRelSufx = version.split('-')
	String[] codes = versionAndRelSufx[0].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)

	if (versionAndRelSufx.length > 1) {
		String suffixType = versionAndRelSufx[1].split('\\.')[0]
		String suffixNumber = versionAndRelSufx[1].split('\\.')[1]
		def suffixMap = ['alpha': '1', 'beta': '2', 'rc': '3']
		versionCodeStr += suffixMap[suffixType] + String.format("%02d", suffixNumber as int)
	} else {
		versionCodeStr += "400" // For a normal release version without suffix, get the highest version code
	}
	println "Version code for $version is $versionCodeStr"
	return versionCodeStr as int
}

project.buildDir = buildDir
def project_version = rootProject.file('../VERSION_NUMBER').text.trim()
project.version = releaseVersionSuffix ? "${project_version}${releaseVersionSuffix}" : project_version
project.group = "com.microsoft.onnxruntime"

def tmpArtifactId = enableTrainingApis ? project.name + "-training" : project.name
def mavenArtifactId = tmpArtifactId + '-android' + (qnnVersion != null ? '-qnn' : '')

//should the mavenArtifactId be read from the packageName variable as
//that's how it's used in the build_aar_copy_artifacts.sh while copying the artifacts

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.'
def trainingDescription = 'The onnxruntime-training android package is designed to efficiently train and infer a ' +
	'wide range of ONNX models on edge devices, such as mobile phones, tablets, and other portable devices with ' +
	'a focus on minimizing resource usage and maximizing accuracy.' +
	'See https://github.com/microsoft/onnxruntime-training-examples/tree/master/on_device_training for more details.'
def qnnDescription = '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 with the QNN Execution Provider.' +
	'It includes support for all types and operators, for ONNX format models. All standard ONNX models can be executed' +
	'with this package.'

buildscript {
	repositories {
		google()
		mavenCentral()
	}
	dependencies {
		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
	}
}

allprojects {
	repositories {
		google()
		mavenCentral()
	}
}

android {
	compileSdkVersion 34

	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_17
		targetCompatibility = JavaVersion.VERSION_17
	}

	sourceSets {
		main {
			jniLibs.srcDirs = [jniLibsDir]
			java {
				srcDirs = ['src/main/java', 'src/main/android']
			}
		}
	}

	namespace 'ai.onnxruntime'
}

task sourcesJar(type: Jar) {
	archiveClassifier = "sources"
	from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc) {
	source = android.sourceSets.main.java.srcDirs
	classpath += project.files(android.getBootClasspath())
}

task javadocJar(type: Jar, dependsOn: javadoc) {
	archiveClassifier = '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.25.5'
}

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 = qnnVersion != null ? 'onnxruntime-qnn' : (enableTrainingApis ? 'onnxruntime-training' : 'onnx-runtime')
				description = qnnVersion != null ? qnnDescription : (enableTrainingApis ? trainingDescription : 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'
					}
				}

				if (qnnVersion != null) {
					println "Modifying the POM XML to include QNN dependency"
					withXml {
						def dependencynode = asNode().appendNode('dependencies').appendNode('dependency')
						dependencynode.appendNode('groupId', 'com.qualcomm.qti')
						dependencynode.appendNode('artifactId', 'qnn-runtime')
						dependencynode.appendNode('version', qnnVersion)
					}
				}
			}
		}
	}

	//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
}