build.gradle 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Copyright (c) Facebook, Inc. and its affiliates.
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. plugins {
  5. id("com.android.library")
  6. id("maven")
  7. id("de.undercouch.download")
  8. }
  9. import de.undercouch.gradle.tasks.download.Download
  10. import org.apache.tools.ant.taskdefs.condition.Os
  11. import org.apache.tools.ant.filters.ReplaceTokens
  12. // We download various C++ open-source dependencies into downloads.
  13. // We then copy both the downloaded code and our custom makefiles and headers into third-party-ndk.
  14. // After that we build native code from src/main/jni with module path pointing at third-party-ndk.
  15. def customDownloadsDir = System.getenv("REACT_NATIVE_DOWNLOADS_DIR")
  16. def downloadsDir = customDownloadsDir ? new File(customDownloadsDir) : new File("$buildDir/downloads")
  17. def thirdPartyNdkDir = new File("$buildDir/third-party-ndk")
  18. // You need to have following folders in this directory:
  19. // - boost_1_63_0
  20. // - double-conversion-1.1.6
  21. // - folly-deprecate-dynamic-initializer
  22. // - glog-0.3.5
  23. def dependenciesPath = System.getenv("REACT_NATIVE_DEPENDENCIES")
  24. // The Boost library is a very large download (>100MB).
  25. // If Boost is already present on your system, define the REACT_NATIVE_BOOST_PATH env variable
  26. // and the build will use that.
  27. def boostPath = dependenciesPath ?: System.getenv("REACT_NATIVE_BOOST_PATH")
  28. task createNativeDepsDirectories {
  29. downloadsDir.mkdirs()
  30. thirdPartyNdkDir.mkdirs()
  31. }
  32. task downloadBoost(dependsOn: createNativeDepsDirectories, type: Download) {
  33. src("https://github.com/react-native-community/boost-for-react-native/releases/download/v${BOOST_VERSION.replace("_", ".")}-0/boost_${BOOST_VERSION}.tar.gz")
  34. onlyIfNewer(true)
  35. overwrite(false)
  36. dest(new File(downloadsDir, "boost_${BOOST_VERSION}.tar.gz"))
  37. }
  38. task prepareBoost(dependsOn: boostPath ? [] : [downloadBoost], type: Copy) {
  39. from(boostPath ?: tarTree(resources.gzip(downloadBoost.dest)))
  40. from("src/main/jni/third-party/boost/Android.mk")
  41. include("Android.mk", "boost_${BOOST_VERSION}/boost/**/*.hpp", "boost/boost/**/*.hpp")
  42. includeEmptyDirs = false
  43. into("$thirdPartyNdkDir/boost")
  44. doLast {
  45. file("$thirdPartyNdkDir/boost/boost").renameTo("$thirdPartyNdkDir/boost/boost_${BOOST_VERSION}")
  46. }
  47. }
  48. task downloadDoubleConversion(dependsOn: createNativeDepsDirectories, type: Download) {
  49. src("https://github.com/google/double-conversion/archive/v${DOUBLE_CONVERSION_VERSION}.tar.gz")
  50. onlyIfNewer(true)
  51. overwrite(false)
  52. dest(new File(downloadsDir, "double-conversion-${DOUBLE_CONVERSION_VERSION}.tar.gz"))
  53. }
  54. task prepareDoubleConversion(dependsOn: dependenciesPath ? [] : [downloadDoubleConversion], type: Copy) {
  55. from(dependenciesPath ?: tarTree(downloadDoubleConversion.dest))
  56. from("src/main/jni/third-party/double-conversion/Android.mk")
  57. include("double-conversion-${DOUBLE_CONVERSION_VERSION}/src/**/*", "Android.mk")
  58. filesMatching("*/src/**/*", { fname -> fname.path = "double-conversion/${fname.name}" })
  59. includeEmptyDirs = false
  60. into("$thirdPartyNdkDir/double-conversion")
  61. }
  62. task downloadFolly(dependsOn: createNativeDepsDirectories, type: Download) {
  63. src("https://github.com/facebook/folly/archive/v${FOLLY_VERSION}.tar.gz")
  64. onlyIfNewer(true)
  65. overwrite(false)
  66. dest(new File(downloadsDir, "folly-${FOLLY_VERSION}.tar.gz"))
  67. }
  68. task prepareFolly(dependsOn: dependenciesPath ? [] : [downloadFolly], type: Copy) {
  69. from(dependenciesPath ?: tarTree(downloadFolly.dest))
  70. from("src/main/jni/third-party/folly/Android.mk")
  71. include("folly-${FOLLY_VERSION}/folly/**/*", "Android.mk")
  72. eachFile { fname -> fname.path = (fname.path - "folly-${FOLLY_VERSION}/") }
  73. includeEmptyDirs = false
  74. into("$thirdPartyNdkDir/folly")
  75. }
  76. task downloadGlog(dependsOn: createNativeDepsDirectories, type: Download) {
  77. src("https://github.com/google/glog/archive/v${GLOG_VERSION}.tar.gz")
  78. onlyIfNewer(true)
  79. overwrite(false)
  80. dest(new File(downloadsDir, "glog-${GLOG_VERSION}.tar.gz"))
  81. }
  82. // Prepare glog sources to be compiled, this task will perform steps that normally should've been
  83. // executed by automake. This way we can avoid dependencies on make/automake
  84. task prepareGlog(dependsOn: dependenciesPath ? [] : [downloadGlog], type: Copy) {
  85. from(dependenciesPath ?: tarTree(downloadGlog.dest))
  86. from("src/main/jni/third-party/glog/")
  87. include("glog-${GLOG_VERSION}/src/**/*", "Android.mk", "config.h")
  88. includeEmptyDirs = false
  89. filesMatching("**/*.h.in") {
  90. filter(ReplaceTokens, tokens: [
  91. ac_cv_have_unistd_h : "1",
  92. ac_cv_have_stdint_h : "1",
  93. ac_cv_have_systypes_h : "1",
  94. ac_cv_have_inttypes_h : "1",
  95. ac_cv_have_libgflags : "0",
  96. ac_google_start_namespace : "namespace google {",
  97. ac_cv_have_uint16_t : "1",
  98. ac_cv_have_u_int16_t : "1",
  99. ac_cv_have___uint16 : "0",
  100. ac_google_end_namespace : "}",
  101. ac_cv_have___builtin_expect : "1",
  102. ac_google_namespace : "google",
  103. ac_cv___attribute___noinline : "__attribute__ ((noinline))",
  104. ac_cv___attribute___noreturn : "__attribute__ ((noreturn))",
  105. ac_cv___attribute___printf_4_5: "__attribute__((__format__ (__printf__, 4, 5)))"
  106. ])
  107. it.path = (it.name - ".in")
  108. }
  109. into("$thirdPartyNdkDir/glog")
  110. doLast {
  111. copy {
  112. from(fileTree(dir: "$thirdPartyNdkDir/glog", includes: ["stl_logging.h", "logging.h", "raw_logging.h", "vlog_is_on.h", "**/glog/log_severity.h"]).files)
  113. includeEmptyDirs = false
  114. into("$thirdPartyNdkDir/glog/exported/glog")
  115. }
  116. }
  117. }
  118. task downloadJSC(dependsOn: createNativeDepsDirectories, type: Download) {
  119. src("https://registry.npmjs.org/jsc-android/-/jsc-android-${JSC_VERSION}.tgz")
  120. onlyIfNewer(true)
  121. overwrite(false)
  122. dest(new File(downloadsDir, "jsc-${JSC_VERSION}.tar.gz"))
  123. }
  124. // Create Android.mk library module based on jsc from npm
  125. task prepareJSC(dependsOn: downloadJSC) {
  126. doLast {
  127. def jscTar = tarTree(downloadJSC.dest)
  128. def jscAAR = jscTar.matching({ it.include "**/android-jsc/**/*.aar" }).singleFile
  129. def soFiles = zipTree(jscAAR).matching({ it.include "**/*.so" })
  130. def headerFiles = jscTar.matching({ it.include "**/include/*.h" })
  131. copy {
  132. from(soFiles)
  133. from(headerFiles)
  134. from("src/main/jni/third-party/jsc/Android.mk")
  135. filesMatching("**/*.h", { it.path = "JavaScriptCore/${it.name}" })
  136. includeEmptyDirs(false)
  137. into("$thirdPartyNdkDir/jsc")
  138. }
  139. }
  140. }
  141. task downloadNdkBuildDependencies {
  142. if (!boostPath) {
  143. dependsOn(downloadBoost)
  144. }
  145. dependsOn(downloadDoubleConversion)
  146. dependsOn(downloadFolly)
  147. dependsOn(downloadGlog)
  148. dependsOn(downloadJSC)
  149. }
  150. def getNdkBuildName() {
  151. if (Os.isFamily(Os.FAMILY_WINDOWS)) {
  152. return "ndk-build.cmd"
  153. } else {
  154. return "ndk-build"
  155. }
  156. }
  157. def findNdkBuildFullPath() {
  158. // we allow to provide full path to ndk-build tool
  159. if (hasProperty("ndk.command")) {
  160. return property("ndk.command")
  161. }
  162. // or just a path to the containing directory
  163. if (hasProperty("ndk.path")) {
  164. def ndkDir = property("ndk.path")
  165. return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
  166. }
  167. if (System.getenv("ANDROID_NDK") != null) {
  168. def ndkDir = System.getenv("ANDROID_NDK")
  169. return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
  170. }
  171. def ndkDir = android.hasProperty("plugin") ? android.plugin.ndkFolder :
  172. plugins.getPlugin("com.android.library").hasProperty("sdkHandler") ?
  173. plugins.getPlugin("com.android.library").sdkHandler.getNdkFolder() :
  174. android.ndkDirectory.absolutePath
  175. if (ndkDir) {
  176. return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
  177. }
  178. return null
  179. }
  180. def getNdkBuildFullPath() {
  181. def ndkBuildFullPath = findNdkBuildFullPath()
  182. if (ndkBuildFullPath == null) {
  183. throw new GradleScriptException(
  184. "ndk-build binary cannot be found, check if you've set " +
  185. "\$ANDROID_NDK environment variable correctly or if ndk.dir is " +
  186. "setup in local.properties",
  187. null)
  188. }
  189. if (!new File(ndkBuildFullPath).canExecute()) {
  190. throw new GradleScriptException(
  191. "ndk-build binary " + ndkBuildFullPath + " doesn't exist or isn't executable.\n" +
  192. "Check that the \$ANDROID_NDK environment variable, or ndk.dir in local.properties, is set correctly.\n" +
  193. "(On Windows, make sure you escape backslashes in local.properties or use forward slashes, e.g. C:\\\\ndk or C:/ndk rather than C:\\ndk)",
  194. null)
  195. }
  196. return ndkBuildFullPath
  197. }
  198. task buildReactNdkLib(dependsOn: [prepareJSC, prepareBoost, prepareDoubleConversion, prepareFolly, prepareGlog], type: Exec) {
  199. inputs.dir("src/main/jni/react")
  200. outputs.dir("$buildDir/react-ndk/all")
  201. commandLine(getNdkBuildFullPath(),
  202. "NDK_PROJECT_PATH=null",
  203. "NDK_APPLICATION_MK=$projectDir/src/main/jni/Application.mk",
  204. "NDK_OUT=" + temporaryDir,
  205. "NDK_LIBS_OUT=$buildDir/react-ndk/all",
  206. "THIRD_PARTY_NDK_DIR=$buildDir/third-party-ndk",
  207. "REACT_COMMON_DIR=$projectDir/../ReactCommon",
  208. "REACT_SRC_DIR=$projectDir/src/main/java/com/facebook/react",
  209. "-C", file("src/main/jni/react/jni").absolutePath,
  210. "--jobs", project.findProperty("jobs") ?: Runtime.runtime.availableProcessors()
  211. )
  212. }
  213. task cleanReactNdkLib(type: Exec) {
  214. ignoreExitValue(true)
  215. errorOutput(new ByteArrayOutputStream())
  216. commandLine(getNdkBuildFullPath(),
  217. "NDK_APPLICATION_MK=$projectDir/src/main/jni/Application.mk",
  218. "THIRD_PARTY_NDK_DIR=$buildDir/third-party-ndk",
  219. "REACT_COMMON_DIR=$projectDir/../ReactCommon",
  220. "-C", file("src/main/jni/react/jni").absolutePath,
  221. "clean")
  222. doLast {
  223. file(AAR_OUTPUT_URL).delete()
  224. println("Deleted aar output dir at ${file(AAR_OUTPUT_URL)}")
  225. }
  226. }
  227. task packageReactNdkLibs(dependsOn: buildReactNdkLib, type: Copy) {
  228. from("$buildDir/react-ndk/all")
  229. from("$thirdPartyNdkDir/jsc/jni")
  230. into("$buildDir/react-ndk/exported")
  231. }
  232. task packageReactNdkLibsForBuck(dependsOn: packageReactNdkLibs, type: Copy) {
  233. from("$buildDir/react-ndk/exported")
  234. into("src/main/jni/prebuilt/lib")
  235. }
  236. android {
  237. compileSdkVersion 28
  238. compileOptions {
  239. sourceCompatibility(JavaVersion.VERSION_1_8)
  240. targetCompatibility(JavaVersion.VERSION_1_8)
  241. }
  242. defaultConfig {
  243. minSdkVersion(16)
  244. targetSdkVersion(28)
  245. versionCode(1)
  246. versionName("1.0")
  247. consumerProguardFiles("proguard-rules.pro")
  248. ndk {
  249. moduleName("reactnativejni")
  250. }
  251. buildConfigField("boolean", "IS_INTERNAL_BUILD", "false")
  252. buildConfigField("int", "EXOPACKAGE_FLAGS", "0")
  253. testApplicationId("com.facebook.react.tests.gradle")
  254. testInstrumentationRunner("android.support.test.runner.AndroidJUnitRunner")
  255. }
  256. sourceSets.main {
  257. jni.srcDirs = []
  258. jniLibs.srcDir("$buildDir/react-ndk/exported")
  259. res.srcDirs = ["src/main/res/devsupport", "src/main/res/shell", "src/main/res/views/modal", "src/main/res/views/uimanager"]
  260. java {
  261. srcDirs = ["src/main/java", "src/main/libraries/soloader/java", "src/main/jni/first-party/fb/jni/java"]
  262. exclude("com/facebook/react/processing")
  263. exclude("com/facebook/react/module/processing")
  264. }
  265. }
  266. tasks.withType(JavaCompile) {
  267. compileTask -> compileTask.dependsOn(packageReactNdkLibs)
  268. }
  269. clean.dependsOn(cleanReactNdkLib)
  270. lintOptions {
  271. abortOnError(false)
  272. }
  273. packagingOptions {
  274. exclude("META-INF/NOTICE")
  275. exclude("META-INF/LICENSE")
  276. }
  277. }
  278. dependencies {
  279. api("com.facebook.infer.annotation:infer-annotation:0.11.2")
  280. api("javax.inject:javax.inject:1")
  281. api("com.android.support:appcompat-v7:28.0.0")
  282. api("com.facebook.fresco:fresco:${FRESCO_VERSION}")
  283. api("com.facebook.fresco:imagepipeline-okhttp3:${FRESCO_VERSION}")
  284. api("com.facebook.soloader:soloader:${SO_LOADER_VERSION}")
  285. api("com.google.code.findbugs:jsr305:3.0.2")
  286. api("com.squareup.okhttp3:okhttp:${OKHTTP_VERSION}")
  287. api("com.squareup.okhttp3:okhttp-urlconnection:${OKHTTP_VERSION}")
  288. api("com.squareup.okio:okio:1.15.0")
  289. testImplementation("junit:junit:${JUNIT_VERSION}")
  290. testImplementation("org.powermock:powermock-api-mockito:${POWERMOCK_VERSION}")
  291. testImplementation("org.powermock:powermock-module-junit4-rule:${POWERMOCK_VERSION}")
  292. testImplementation("org.powermock:powermock-classloading-xstream:${POWERMOCK_VERSION}")
  293. testImplementation("org.mockito:mockito-core:${MOCKITO_CORE_VERSION}")
  294. testImplementation("org.easytesting:fest-assert-core:${FEST_ASSERT_CORE_VERSION}")
  295. testImplementation("org.robolectric:robolectric:${ROBOLECTRIC_VERSION}")
  296. androidTestImplementation(fileTree(dir: "src/main/third-party/java/buck-android-support/", include: ["*.jar"]))
  297. androidTestImplementation("com.android.support.test:runner:${ANDROID_SUPPORT_TEST_VERSION}")
  298. androidTestImplementation("com.android.support.test:rules:${ANDROID_SUPPORT_TEST_VERSION}")
  299. androidTestImplementation("org.mockito:mockito-core:${MOCKITO_CORE_VERSION}")
  300. }
  301. apply(from: "release.gradle")