-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathVrApp.gradle
executable file
·408 lines (352 loc) · 13.7 KB
/
VrApp.gradle
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import org.gradle.internal.os.OperatingSystem;
import com.android.ddmlib.AndroidDebugBridge
import com.android.ddmlib.IDevice
import com.android.ddmlib.CollectingOutputReceiver
import org.apache.tools.ant.taskdefs.condition.Os
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
}
}
class VrAppPlugin implements Plugin<Project> {
Project project = null
// per-device methods //
boolean isPackageInstalled( IDevice device, String packageName ) {
CollectingOutputReceiver receiver = new CollectingOutputReceiver();
device.executeShellCommand( "pm list packages " + packageName, receiver );
def out = receiver.getOutput();
def ret = false;
out.split('\n').each {
if ( it =~ packageName )
{
ret = true;
}
}
return ret;
}
boolean checkOsig( IDevice device, String deviceSerial, FileTree sigDir ) {
if ( !isPackageInstalled( device, "com.samsung.android.hmt.vrsvc" ) ) {
return true;
}
def oculusSig = "oculussig_${deviceSerial}"
if ( sigDir.matching { include "**/${oculusSig}"}.isEmpty() ) {
return false;
}
return true;
}
void clearAdbLog( IDevice device ) {
CollectingOutputReceiver receiver = new CollectingOutputReceiver()
device.executeShellCommand( "logcat -c", receiver )
}
void installApk( IDevice device, File apkFile, String applicationId ) {
project.logger.quiet "Installing ${applicationId} on device ${device.serialNumber}"
String toinstall = "/data/local/tmp/toinstall.apk"
try {
device.pushFile( apkFile.path, toinstall )
} catch ( Exception e ) {
throw new RuntimeException( "Failed to push ${apkFile.path} to ${toinstall}. ${e}", e )
}
while ( true ) {
try {
device.installRemotePackage( toinstall, true )
break
} catch ( Exception e ) {
project.logger.quiet "Failed to install ${applicationId} on device ${device.serialNumber} (${e}). Trying to uninstall first."
}
// this only needs to happen if we fail to install (i.e. not having matching sigs)
stopApk( device, "com.oculus.horizon" )
try {
device.uninstallPackage( applicationId )
} catch ( Exception e ) {
throw new RuntimeException( "Failed to uninstall ${applicationId}. ${e}", e )
}
}
}
void stopApk( IDevice device, String packageName ) {
CollectingOutputReceiver receiver = new CollectingOutputReceiver()
device.executeShellCommand( "am force-stop ${packageName}", receiver )
}
void runApk( IDevice device, manifestFile ) {
CollectingOutputReceiver receiver = new CollectingOutputReceiver()
def activityClass = new XmlSlurper().parse( manifestFile ).application.activity.find{ it.'intent-filter'.find{ filter ->
return filter.action .find{it.'@android:name'.text() == 'android.intent.action.MAIN' } \
&& ( filter.category.find{it.'@android:name'.text() == 'android.intent.category.LAUNCHER'} \
|| filter.category.find{it.'@android:name'.text() == 'android.intent.category.INFO'} )
}}.'@android:name'
def startActivity = "${project.android.defaultConfig.applicationId}/${activityClass}"
project.logger.quiet "Starting \'$startActivity\' on ${project.deviceMap.size()} devices:"
project.logger.quiet "- ${device.serialNumber}"
device.executeShellCommand( "am start $startActivity", receiver )
}
void apply( Project project ) {
this.project = project
// FIXME: The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
project.task( "cleanWorkAround" ) {
description "Workaround for .externalNativeBuild not being deleted on clean"
}.doLast {
project.delete project.file( ".externalNativeBuild" )
}
project.android {
compileSdkVersion 26
buildToolsVersion '29.0.2'
ndkVersion "21.0.6113669"
defaultConfig {
minSdkVersion 23
targetSdkVersion 23
externalNativeBuild {
ndk {
abiFilters 'arm64-v8a'
}
ndkBuild {
def numProcs = Runtime.runtime.availableProcessors()
arguments "V=0", "-j$numProcs", "-C$project.buildDir.parent", "APP_PLATFORM=android-21", "NDK_TOOLCHAIN_VERSION=clang", "APP_STL=c++_static"
abiFilters 'arm64-v8a'
}
}
}
externalNativeBuild {
ndkBuild {
path 'jni/Android.mk'
}
}
signingConfigs {
def keystorePath = (project.hasProperty('key.store')) ?
new File(project.getProperty('key.store')) :
project.file('android.debug.keystore')
def keystorePassword = (project.hasProperty('key.store.password')) ?
project.getProperty('key.store.password') : 'android'
def keystoreKeyAlias = (project.hasProperty('key.alias')) ?
project.getProperty('key.alias') : 'androiddebugkey'
def keystoreKeyPassword = (project.hasProperty('key.alias.password')) ?
project.getProperty('key.alias.password') : 'android'
debug {
storeFile keystorePath
storePassword keystorePassword
keyAlias keystoreKeyAlias
keyPassword keystoreKeyPassword
v2SigningEnabled true
}
release {
storeFile keystorePath
storePassword keystorePassword
keyAlias keystoreKeyAlias
keyPassword keystoreKeyPassword
v2SigningEnabled true
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
debuggable true
jniDebuggable true
externalNativeBuild {
ndkBuild {
arguments "NDK_DEBUG=1","OVR_DEBUG=1","USE_ASAN=1"
}
}
}
release {
signingConfig signingConfigs.release
debuggable false
jniDebuggable false
externalNativeBuild {
ndkBuild {
arguments "NDK_DEBUG=0","OVR_DEBUG=0","USE_ASAN=0"
}
}
}
}
}
// WORKAROUND: On Mac OS X, running ndk-build clean with a high num of parallel executions
// set may result in the following build error: rm: fts_read: No such file or directory.
// Currently, there isn't a good way to specify numProcs=1 only on clean. So, in order
// to work around the issue, delete the auto-generated .externalNativeBuild artifacts
// (where $numProcs specified) before executing the clean task.
project.clean.dependsOn project.cleanWorkAround
project.clean {
// remove the auto-generated debug keystore (currently generated by python build script)
delete "android.debug.keystore"
}
project.afterEvaluate {
Task initDeviceList = project.task( "initDeviceList()" ).doLast {
project.ext.deviceMap = [ : ]
if (project.hasProperty( "should_install" ) == true || project.hasProperty( "disable_sig_check" ) == false) {
AndroidDebugBridge.initIfNeeded( false )
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge( project.android.getAdbExe().absolutePath, false )
long timeOut = 30000 // 30 sec
int sleepTime = 1000
while ( !bridge.hasInitialDeviceList() && timeOut > 0 ) {
sleep( sleepTime )
timeOut -= sleepTime
}
if ( timeOut <= 0 && !bridge.hasInitialDeviceList() ) {
throw new RuntimeException( "Timeout getting device list.", null )
}
// if a device is connected both physically and over the network, only include the physical ID
if ( project.hasProperty( "should_install" ) == true ) {
bridge.devices.split { it.getProperty( "ro.serialno" ) != it.serialNumber }.each {
it.collectEntries( project.deviceMap, { [ ( it.getProperty( "ro.serialno" )) : it ] } )
}
}
}
}
project.task( "stopApk", dependsOn: initDeviceList ) {
description "Stops app if currently running on device"
}.doLast {
project.deviceMap.each { deviceSerial, device ->
stopApk( device, android.defaultConfig.applicationId )
}
}
project.task ( "OSigPreCheck", dependsOn: initDeviceList ) {
description "Checks for Oculus Signature files"
onlyIf {
project.deviceMap.size() != 0 &&
project.hasProperty( "disable_sig_check" ) == false
}
}.doLast {
def iterator = project.deviceMap.entrySet().iterator()
while ( iterator.hasNext() ) {
def deviceElem = iterator.next()
if ( !deviceElem.value.isOnline() ) {
iterator.remove()
continue
}
String deviceSerial = deviceElem.key
IDevice device = deviceElem.value
if ( !checkOsig( device, deviceSerial, project.fileTree( dir: "../assets" ).plus( project.fileTree( dir: "assets" ) ) ) &&
!checkOsig( device, deviceSerial, project.fileTree( dir: "../../assets" ).plus( project.fileTree( dir: "assets" ) ) ) ) {
throw new RuntimeException( "Missing oculussig_${deviceSerial}. You can generate this file at http://developer.oculus.com/osig/", null )
}
}
}
project.android.applicationVariants.all { variant ->
Task OSigPostCheck = project.task( "OSigPostCheck${variant.name.capitalize()}", dependsOn: variant.assemble ) {
description "Checks for Oculus Signature files in the output apk"
onlyIf {
project.deviceMap.size() != 0 &&
project.hasProperty( "disable_sig_check" ) == false
}
dependsOn project.OSigPreCheck
}.doLast { variant.outputs.each { output ->
def sigfiles = project.zipTree( output.outputFile ).matching{ include "assets/oculussig*" }
def iterator = project.deviceMap.entrySet().iterator()
while ( iterator.hasNext() ) {
def deviceElem = iterator.next()
if ( !deviceElem.value.isOnline() ) {
iterator.remove()
continue
}
String deviceSerial = deviceElem.key
IDevice device = deviceElem.value
checkOsig( device, deviceSerial, sigfiles )
}
}
}
Task installAndRun = project.task( "installAndRun${variant.name.capitalize()}" ) {
dependsOn variant.assemble
dependsOn OSigPostCheck
onlyIf { project.hasProperty( "should_install" ) }
description "Installs and runs the APK file"
}.doLast { variant.outputs.each { output ->
if ( output.outputFile.exists() )
{
if ( project.deviceMap.size() == 0 ) {
project.logger.quiet "Install requested, but no devices found."
} else {
project.deviceMap.each { deviceSerial, device ->
installApk( device, output.outputFile, project.android.defaultConfig.applicationId )
if ( project.hasProperty( "clear_logcat" ) ) {
clearAdbLog( device )
}
runApk( device, new File("${output.processManifest.manifestOutputDirectory}/AndroidManifest.xml") )
}
}
}
}
}
variant.assemble.dependsOn project.OSigPreCheck
variant.assemble.finalizedBy installAndRun
}
}
}
}
// *******************************
// GEN DEBUG KEYSTORE TASK
// *******************************
class GenDebugKeystore extends DefaultTask {
@TaskAction
genDebugKeystore() {
if ( !project.file("android.debug.keystore").exists() ) {
def pythonCmd = 'python'
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
pythonCmd = "${project.rootProject.projectDir}/bin/scripts/build/ovrbuild_keystore.py.bat"
project.exec {
commandLine pythonCmd
}
} else {
project.exec {
commandLine pythonCmd,
"${project.rootProject.projectDir}/bin/scripts/build/ovrbuild_keystore.py"
}
}
}
}
}
project.task('genDebugKeystore', type: GenDebugKeystore ) {
description 'generate debug keystore'
}
// *******************************
// BUILD SDK LIBS TASK
// *******************************
class BuildSDKLibs extends DefaultTask {
String buildType
@TaskAction
buildSdkLibs() {
println "buildSdkLibs() started"
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
def pythonCmd = "${project.rootProject.projectDir}/bin/scripts/build/ovrbuild_sdklibs.py.bat"
project.exec {
commandLine pythonCmd,
buildType
}
} else {
def pythonCmd = 'python'
project.exec {
commandLine pythonCmd,
"${project.rootProject.projectDir}/bin/scripts/build/ovrbuild_sdklibs.py",
buildType
}
}
println "buildSdkLibs() finished"
}
}
project.task('buildSdkLibsDebug', type: BuildSDKLibs) {
description 'build sdk libs'
buildType = "debug"
}
project.task('buildSdkLibsRelease', type: BuildSDKLibs) {
description 'build sdk libs'
buildType = "release"
}
// **************************************************************
// Task interceptor to add dependant tasks
// **************************************************************
project.tasks.whenTaskAdded { task ->
if (task.name == 'validateSigningDebug') {
task.dependsOn 'genDebugKeystore'
}
if (task.name == 'validateSigningRelease') {
task.dependsOn 'genDebugKeystore'
}
if (task.name == 'preDebugBuild') {
task.dependsOn buildSdkLibsDebug
}
if (task.name == 'preReleaseBuild') {
task.dependsOn buildSdkLibsRelease
}
}
apply plugin: VrAppPlugin