• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1buildscript {
2    repositories {
3        google()
4        jcenter()
5    }
6    dependencies {
7        classpath libraries.android_tools
8    }
9}
10
11description = 'Conscrypt: Android Benchmarks'
12
13ext {
14    androidHome = "$System.env.ANDROID_HOME"
15    androidSdkInstalled = file("$androidHome").exists()
16    androidVersionCode = 1
17    androidVersionName = "$version"
18    androidMinSdkVersion = 26
19    androidTargetSdkVersion = 26
20}
21
22if (androidSdkInstalled) {
23    apply plugin: 'com.android.library'
24
25    android {
26        compileSdkVersion androidTargetSdkVersion
27
28        compileOptions {
29            sourceCompatibility androidMinJavaVersion
30            targetCompatibility androidMinJavaVersion
31        }
32
33        defaultConfig {
34            minSdkVersion androidMinSdkVersion
35            targetSdkVersion androidTargetSdkVersion
36            versionCode androidVersionCode
37            versionName androidVersionName
38        }
39        lintOptions {
40            // Some Caliper classes reference packages that don't exist on Android
41            disable 'InvalidPackage'
42        }
43        sourceSets.main {
44            java {
45                srcDirs = [
46                        "src/main/java"
47                ]
48            }
49        }
50    }
51
52    configurations {
53        // For the depsJar task, we need to create a config we can pull libraries from to
54        // make the complete JAR. Some we do not want the transitive dependencies because
55        // they are already included on the Android system.
56        depsJarApi
57        depsJarApi.transitive = true
58
59        depsJarImplementation
60        depsJarImplementation.transitive = false
61
62        implementation.extendsFrom(depsJarApi)
63        implementation.extendsFrom(depsJarImplementation)
64    }
65
66    dependencies {
67        depsJarApi project(path: ':conscrypt-android'),
68                   libraries.bouncycastle_provider,
69                   libraries.bouncycastle_apis
70
71        depsJarImplementation project(':conscrypt-benchmark-base'),
72                              project(':conscrypt-testing'),
73                              project(':conscrypt-libcore-stub')
74
75        implementation 'com.google.caliper:caliper:1.0-beta-2'
76    }
77
78    // This task bundles up everything we're going to send to the device into a single jar.
79    // We need to include all the Conscrypt code plus the Bouncy Castle jar because the platform
80    // version of Bouncy Castle is jarjared.
81    //
82    // Since we're examining the contents of the archive files, we need to prevent evaluation of
83    // the .aar and .jar contents before the actual archives are built. To do this we create a
84    // configure task where the "from" contents is set inside a doLast stanza to ensure it is run
85    // after the execution phase of the "assemble" task.
86    task configureDepsJar {
87        dependsOn assemble, \
88                  configurations.depsJarApi.artifacts, \
89                  configurations.depsJarImplementation.artifacts
90        doLast {
91            depsJar.from {
92                [
93                    configurations.depsJarApi,
94                    configurations.depsJarImplementation,
95                    configurations.archives.artifacts.file
96                ].collect { config ->
97                    config.findResults { archive ->
98                        // For Android library archives (.aar), we need to expand the classes.jar
99                        // inside as well as including all the jni libraries.
100                        if (archive.name.endsWith(".aar")) {
101                            [
102                                zipTree(archive).matching {
103                                    include 'classes.jar'
104                                }.collect { file ->
105                                    zipTree(file)
106                                },
107                                zipTree(archive).matching {
108                                    include '**/*.so'
109                                }
110                            ]
111                        } else if (archive.name.endsWith(".jar")) {
112                            // Bouncy Castle signs their jar, which causes our combined jar to fail
113                            // to verify.  Just strip out the signature files.
114                            zipTree(archive).matching {
115                                exclude 'META-INF/*.SF'
116                                exclude 'META-INF/*.DSA'
117                                exclude 'META-INF/*.EC'
118                                exclude 'META-INF/*.RSA'
119                            }
120                        }
121                    }
122                }
123            }
124        }
125    }
126
127    task depsJar(type: Jar, dependsOn: configureDepsJar) {
128        archiveName = 'bundled-deps.jar'
129    }
130
131    task getAndroidDeviceAbi {
132        doLast {
133            new ByteArrayOutputStream().withStream { os ->
134                def result = exec {
135                    executable android.adbExecutable
136                    args 'shell', 'getprop', 'ro.product.cpu.abi'
137                    standardOutput = os
138                }
139                project.ext.androidDeviceAbi = os.toString().trim()
140                project.ext.androidDevice64Bit = androidDeviceAbi.contains('64')
141            }
142        }
143    }
144
145    task configureExtractNativeLib {
146        dependsOn getAndroidDeviceAbi, depsJar
147        doLast {
148            extractNativeLib.from {
149                zipTree(depsJar.archivePath).matching {
150                    include "jni/${androidDeviceAbi}/*.so"
151                }.collect {
152                    // Using collect flattens out the directory.
153                    it
154                }
155            }
156        }
157    }
158
159    task extractNativeLib(type: Copy, dependsOn: configureExtractNativeLib) {
160        into "$buildDir/extracted-native-libs"
161    }
162
163    task configurePushNativeLibrary {
164        dependsOn extractNativeLib
165        doLast {
166            project.ext.nativeLibPath = "/system/lib${androidDevice64Bit ? '64' : ''}/libconscrypt_jni.so"
167            pushNativeLibrary.args 'push', "${extractNativeLib.destinationDir}/libconscrypt_jni.so", nativeLibPath
168        }
169    }
170
171    task pushNativeLibrary(type: Exec, dependsOn: configurePushNativeLibrary) {
172        pushNativeLibrary.executable android.adbExecutable
173    }
174
175    task runBenchmarks(dependsOn: [depsJar, pushNativeLibrary]) {
176        doLast {
177            // Execute the benchmarks
178            exec {
179                workingDir "${rootDir}"
180                environment PATH: "${android.sdkDirectory}/build-tools/${android.buildToolsVersion}:$System.env.PATH"
181                environment JACK_JAR: "${android.sdkDirectory}/build-tools/${android.buildToolsVersion}/jack.jar"
182
183                executable 'java'
184                args '-cp', 'benchmark-android/vogar.jar', 'vogar.Vogar'
185                args '--classpath', depsJar.archivePath
186                args '--benchmark'
187                args '--language=JN'
188                args '--mode=app_process'
189                args 'org.conscrypt.CaliperAlpnBenchmark'
190                args 'org.conscrypt.CaliperClientSocketBenchmark'
191                args 'org.conscrypt.CaliperEngineHandshakeBenchmark'
192                args 'org.conscrypt.CaliperEngineWrapBenchmark'
193            }
194            // Clean up the native library
195            exec {
196                executable android.adbExecutable
197                args 'shell', 'rm', '-f', nativeLibPath
198            }
199        }
200    }
201} else {
202    logger.warn('Android SDK has not been detected. The Android Benchmark module will not be built.')
203
204    // Disable all tasks
205    tasks.collect {
206        it.enabled = false
207    }
208}
209