1/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * This file was created using the `create_project.py` script located in the
19 * `<AndroidX root>/development/project-creator` directory.
20 *
21 * Please use that script when creating a new project, rather than copying an existing project and
22 * modifying its settings.
23 */
24import androidx.build.SoftwareType
25import androidx.build.AndroidXConfig
26
27plugins {
28    id("AndroidXPlugin")
29    id("com.android.library")
30    id("org.jetbrains.kotlin.android")
31}
32
33/**
34 * When `reusePrebuiltsAar` is set to `true` reuses the AAR file from the ../../prebuilts directory.
35 * When `reusePrebuiltsAar` is set to `false` builds the project from scratch (from CXX) files.
36 *
37 * This was introduced to address the following issues that occurred when always building
38 * the project from scratch:
39 * - no stripping of debug symbols in 'test' configurations resulting in very large
40 *   files (200+ MB total) being added to e.g. each Benchmark project (b/228627720).
41 * - no caching due to the way CMake builds are currently handled in Android Gradle Plugin
42 *   resulting in always building the CXX files from scratch, which is very slow due to the
43 *   size of Perfetto SDK (b/230790969).
44 *
45 *  Additionally, using the prebuilts reference directly (instead of the project reference)
46 *  resulted in the project not getting published to Maven or Snapshot Builds (androidx.dev).
47 */
48
49def reusePrebuiltsAar = Boolean.parseBoolean(
50        System.getProperty("TRACING_PERFETTO_REUSE_PREBUILTS_AAR", "true")
51)
52def prebuiltsAarVersion = androidx.LibraryVersions.TRACING_PERFETTO
53def unzippedPrebuiltsAarDir = "$buildDir/unzipped-aar"
54
55if (reusePrebuiltsAar) {
56    def unzipTask = tasks.register("unzipPrebuiltsAarDir", Copy) {
57        def zipFile = AndroidXConfig.getPrebuiltsRoot(project).toPath().resolve(
58                "androidx/internal/androidx/tracing/tracing-perfetto-binary/$prebuiltsAarVersion/" +
59                        "tracing-perfetto-binary-${prebuiltsAarVersion}.aar"
60        )
61        from zipTree(zipFile)
62        into file(unzippedPrebuiltsAarDir)
63    }
64
65    tasks.findByName("preBuild").dependsOn(unzipTask)
66}
67
68android {
69    if (reusePrebuiltsAar) {
70        sourceSets {
71            main.jniLibs.srcDirs += new File(unzippedPrebuiltsAarDir, "jni")
72        }
73    } else { // build .so files from scratch
74        externalNativeBuild {
75            cmake {
76                path "src/main/cpp/CMakeLists.txt"
77                version = libs.versions.cmake.get()
78            }
79        }
80    }
81    namespace = "androidx.tracing.perfetto.binary"
82}
83
84dependencies {
85    androidTestImplementation(libs.kotlinStdlib)
86    androidTestImplementation(libs.testExtJunit)
87    androidTestImplementation(libs.testRunner)
88    androidTestImplementation(project(":benchmark:benchmark-common")) // for supported ABI checks
89}
90
91androidx {
92    name = "Tracing Perfetto Binary"
93    type = SoftwareType.PUBLISHED_LIBRARY
94    inceptionYear = "2022"
95    description = "Provides native binaries required by AndroidX Tracing: Perfetto SDK " +
96        "and is not intended to be used outside of that context."
97    doNotDocumentReason = "No public API"
98    deviceTests {
99        enableAlsoRunningOnPhysicalDevices = true
100    }
101}
102