build.gradle.kts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. api(platform(libs.okhttp3.bom))
  40. api(libs.okhttp3)
  41. api(libs.okhttp3.loggingInterceptor)
  42. implementation(libs.slf4j.api)
  43. implementation(libs.commonsIo)
  44. implementation(libs.eddsa)
  45. implementation(libs.kotlinx.coroutines.android)
  46. implementation(libs.jna)
  47. testImplementation(libs.junit)
  48. testImplementation(libs.mockito.powermock.reflect)
  49. testImplementation(libs.mockk)
  50. testImplementation(libs.kotlinx.coroutines.test)
  51. testImplementation(libs.slf4j.simple)
  52. testImplementation(libs.kotlin.test)
  53. testImplementation(project(":test-helpers"))
  54. }
  55. sourceSets {
  56. assert(file("./protocol/src/common.proto").exists()) {
  57. "Error: Git protobuf submodule missing. Please run `git submodule update --init`.\n"
  58. }
  59. main {
  60. java.srcDir("./build/generated/source/proto/main/java")
  61. java.srcDir("./build/generated/source/proto/main/kotlin")
  62. java.srcDir("./build/generated/source/libthreema")
  63. }
  64. }
  65. tasks.withType<Test> {
  66. // Necessary to load the dynamic libthreema library in unit tests
  67. systemProperty("jna.library.path", "${project.projectDir}/libthreema/target/release")
  68. useJUnitPlatform()
  69. }
  70. tasks.withType<JacocoReport> {
  71. reports {
  72. xml.required = true
  73. html.required = false
  74. }
  75. }
  76. sonarqube {
  77. properties {
  78. property("sonar.projectKey", "android-client")
  79. property("sonar.projectName", "Threema for Android")
  80. property("sonar.sources", "src/main/")
  81. property("sonar.exclusions", "src/main/java/ove/crypto/**")
  82. property("sonar.tests", "src/test/")
  83. property("sonar.sourceEncoding", "UTF-8")
  84. property("sonar.verbose", "true")
  85. property(
  86. "sonar.coverage.jacoco.xmlReportPaths",
  87. "${projectDir.parentFile.path}/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml",
  88. )
  89. }
  90. }
  91. afterEvaluate {
  92. val bindingsDirectory = "../build/generated/source/libthreema"
  93. // Define the task to generate libthreema library (only used to generate bindings for it)
  94. val generateLibthreema = tasks.register<Exec>("generateLibthreema") {
  95. workingDir("${project.projectDir}/libthreema")
  96. commandLine("cargo", "build", "-F", "uniffi", "-p", "libthreema", "--release")
  97. }
  98. // Define the task to generate the uniffi bindings for libthreema
  99. val uniffiBindings = tasks.register("generateUniFFIBindings") {
  100. dependsOn(generateLibthreema)
  101. doLast {
  102. // It seems that the uniffi package generates a "*.so" file on linux and a "*.dylib" on mac
  103. // while using the cargo build command from the gradle task above ("generateLibthreema").
  104. val uniffiLibraryFilePathPrefix = "${project.projectDir}/libthreema/target/release/liblibthreema"
  105. val uniffiLibraryFile = file("$uniffiLibraryFilePathPrefix.so")
  106. .takeIf { it.exists() }
  107. ?: file("$uniffiLibraryFilePathPrefix.dylib")
  108. assert(uniffiLibraryFile.exists()) {
  109. "Error: Missing pre-generated uniffy library file in libthreema/target/*/ directory.\n"
  110. }
  111. val processBuilder = ProcessBuilder(
  112. "cargo",
  113. "run",
  114. "-p",
  115. "uniffi-bindgen",
  116. "generate",
  117. "--library",
  118. uniffiLibraryFile.path,
  119. "--language",
  120. "kotlin",
  121. "--out-dir",
  122. bindingsDirectory,
  123. "--no-format",
  124. )
  125. processBuilder.directory(file("${project.projectDir}/libthreema"))
  126. processBuilder.start().waitFor()
  127. }
  128. }
  129. tasks["compileKotlin"].dependsOn(uniffiBindings)
  130. }
  131. publishing {
  132. publications {
  133. register<MavenPublication>("library") {
  134. from(components["java"])
  135. version = getGitVersion()
  136. }
  137. }
  138. repositories {
  139. maven {
  140. url = run {
  141. val apiV4Url = System.getenv("CI_API_V4_URL")
  142. val projectId = System.getenv("CI_PROJECT_ID")
  143. uri("$apiV4Url/projects/$projectId/packages/maven")
  144. }
  145. name = "Gitlab"
  146. credentials(HttpHeaderCredentials::class.java) {
  147. name = "Job-Token"
  148. value = System.getenv("CI_JOB_TOKEN")
  149. }
  150. authentication {
  151. create<HttpHeaderAuthentication>("header")
  152. }
  153. }
  154. }
  155. }
  156. tasks.register<Exec>("compileProto") {
  157. workingDir(project.projectDir)
  158. commandLine("./compile-proto.sh")
  159. }
  160. tasks.compileKotlin.dependsOn("compileProto")
  161. tasks.register<Exec>("libthreemaCleanUp") {
  162. workingDir("${project.projectDir}/libthreema")
  163. commandLine("cargo", "clean")
  164. }
  165. tasks.clean.dependsOn("libthreemaCleanUp")
  166. java {
  167. sourceCompatibility = JavaVersion.VERSION_11
  168. targetCompatibility = JavaVersion.VERSION_11
  169. }
  170. kotlin {
  171. compilerOptions {
  172. jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11
  173. }
  174. }