build.gradle.kts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2021-2025 Threema GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. import com.android.build.gradle.internal.tasks.factory.dependsOn
  22. import utils.getGitVersion
  23. plugins {
  24. alias(libs.plugins.sonarqube)
  25. alias(libs.plugins.java.library)
  26. alias(libs.plugins.java.testFixtures)
  27. alias(libs.plugins.kotlin.jvm)
  28. alias(libs.plugins.mavenPublish)
  29. alias(libs.plugins.jacoco)
  30. }
  31. dependencies {
  32. implementation(project(":common"))
  33. api(libs.kotlin.stdlib)
  34. api(libs.kotlinx.coroutines.core)
  35. api(libs.libphonenumber)
  36. api(libs.androidx.annotation)
  37. api(libs.streamsupport.flow)
  38. api(libs.protobuf.kotlin.lite)
  39. implementation(libs.slf4j.api)
  40. implementation(libs.commonsIo)
  41. implementation(libs.eddsa)
  42. implementation(libs.kotlinx.coroutines.android)
  43. implementation(libs.jna)
  44. testImplementation(libs.junit)
  45. testImplementation(libs.mockk)
  46. testImplementation(libs.kotlinx.coroutines.test)
  47. testImplementation(libs.slf4j.simple)
  48. testImplementation(libs.kotlin.test)
  49. testImplementation(project(":test-helpers"))
  50. }
  51. sourceSets {
  52. assert(file("./protocol/src/common.proto").exists()) {
  53. "Error: Git protobuf submodule missing. Please run `git submodule update --init`.\n"
  54. }
  55. main {
  56. java.srcDir("./build/generated/source/proto/main/java")
  57. java.srcDir("./build/generated/source/proto/main/kotlin")
  58. java.srcDir("./build/generated/source/libthreema")
  59. }
  60. }
  61. tasks.withType<Test> {
  62. // Necessary to load the dynamic libthreema library in unit tests
  63. systemProperty("jna.library.path", "${project.projectDir}/libthreema/target/release")
  64. useJUnitPlatform()
  65. }
  66. tasks.withType<JacocoReport> {
  67. reports {
  68. xml.required = true
  69. html.required = false
  70. }
  71. }
  72. sonarqube {
  73. properties {
  74. property("sonar.projectKey", "android-client")
  75. property("sonar.projectName", "Threema for Android")
  76. property("sonar.sources", "src/main/")
  77. property("sonar.exclusions", "src/main/java/ove/crypto/**")
  78. property("sonar.tests", "src/test/")
  79. property("sonar.sourceEncoding", "UTF-8")
  80. property("sonar.verbose", "true")
  81. property(
  82. "sonar.coverage.jacoco.xmlReportPaths",
  83. "${projectDir.parentFile.path}/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml",
  84. )
  85. }
  86. }
  87. afterEvaluate {
  88. val bindingsDirectory = "../build/generated/source/libthreema"
  89. // Define the task to generate libthreema library (only used to generate bindings for it)
  90. val generateLibthreema = tasks.register<Exec>("generateLibthreema") {
  91. workingDir("${project.projectDir}/libthreema")
  92. commandLine("cargo", "build", "-F", "uniffi", "-p", "libthreema", "--release", "--locked")
  93. }
  94. // Define the task to generate the uniffi bindings for libthreema
  95. val uniffiBindings = tasks.register("generateUniFFIBindings") {
  96. dependsOn(generateLibthreema)
  97. doLast {
  98. // It seems that the uniffi package generates a "*.so" file on linux and a "*.dylib" on mac
  99. // while using the cargo build command from the gradle task above ("generateLibthreema").
  100. val uniffiLibraryFilePathPrefix = "${project.projectDir}/libthreema/target/release/liblibthreema"
  101. val uniffiLibraryFile = file("$uniffiLibraryFilePathPrefix.so")
  102. .takeIf { it.exists() }
  103. ?: file("$uniffiLibraryFilePathPrefix.dylib")
  104. assert(uniffiLibraryFile.exists()) {
  105. "Error: Missing pre-generated uniffy library file in libthreema/target/*/ directory.\n"
  106. }
  107. val processBuilder = ProcessBuilder(
  108. "cargo",
  109. "run",
  110. "-p",
  111. "uniffi-bindgen",
  112. "generate",
  113. "--library",
  114. uniffiLibraryFile.path,
  115. "--language",
  116. "kotlin",
  117. "--out-dir",
  118. bindingsDirectory,
  119. "--no-format",
  120. )
  121. processBuilder.directory(file("${project.projectDir}/libthreema"))
  122. processBuilder.start().waitFor()
  123. }
  124. }
  125. tasks["compileKotlin"].dependsOn(uniffiBindings)
  126. }
  127. publishing {
  128. publications {
  129. register<MavenPublication>("library") {
  130. from(components["java"])
  131. version = getGitVersion()
  132. }
  133. }
  134. repositories {
  135. maven {
  136. url = run {
  137. val apiV4Url = System.getenv("CI_API_V4_URL")
  138. val projectId = System.getenv("CI_PROJECT_ID")
  139. uri("$apiV4Url/projects/$projectId/packages/maven")
  140. }
  141. name = "Gitlab"
  142. credentials(HttpHeaderCredentials::class.java) {
  143. name = "Job-Token"
  144. value = System.getenv("CI_JOB_TOKEN")
  145. }
  146. authentication {
  147. create<HttpHeaderAuthentication>("header")
  148. }
  149. }
  150. }
  151. }
  152. tasks.register<Exec>("compileProto") {
  153. group = "build"
  154. description = "generate class bindings from protobuf files in the 'protocol/src' directory"
  155. workingDir(project.projectDir)
  156. commandLine("./compile-proto.sh")
  157. }
  158. tasks.compileKotlin.dependsOn("compileProto")
  159. tasks.register<Exec>("libthreemaCleanUp") {
  160. workingDir("${project.projectDir}/libthreema")
  161. commandLine("cargo", "clean")
  162. }
  163. tasks.clean.dependsOn("libthreemaCleanUp")
  164. java {
  165. sourceCompatibility = JavaVersion.VERSION_11
  166. targetCompatibility = JavaVersion.VERSION_11
  167. }
  168. kotlin {
  169. compilerOptions {
  170. jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11
  171. }
  172. }