373 строки
14 KiB
Groovy
373 строки
14 KiB
Groovy
// Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
// LICENSE file in the root directory of this source tree.
|
|
|
|
plugins {
|
|
id("com.android.library")
|
|
id("maven")
|
|
id("de.undercouch.download")
|
|
}
|
|
|
|
import de.undercouch.gradle.tasks.download.Download
|
|
import org.apache.tools.ant.taskdefs.condition.Os
|
|
import org.apache.tools.ant.filters.ReplaceTokens
|
|
|
|
// In FB react-native, we are downloading all third dependencies source code during build time and then build and consume it.
|
|
// We got rid of this due to anti-compliance and other requirements like building on other environment like clang.
|
|
// Now we have all third party dependencies as separate git repo which are then sub modules of react-native.
|
|
// For boost, we are getting source though nuget. After this we are following the same approach.
|
|
// We then copy both the downloaded code and our custom makefiles and headers into third-party-ndk.
|
|
// After that we build native code from src/main/jni with module path pointing at third-party-ndk.
|
|
|
|
|
|
def customDownloadsDir = System.getenv("REACT_NATIVE_DOWNLOADS_DIR")
|
|
def downloadsDir = customDownloadsDir ? new File(customDownloadsDir) : new File("$buildDir/downloads")
|
|
def thirdPartyNdkDir = new File("$buildDir/third-party-ndk")
|
|
|
|
// You need to have following folders in this directory:
|
|
// - boost_1_63_0
|
|
// - double-conversion-1.1.6
|
|
// - folly-deprecate-dynamic-initializer
|
|
// - glog-0.3.5
|
|
|
|
def dependenciesPath = System.getenv("REACT_NATIVE_DEPENDENCIES")
|
|
|
|
// The Boost library is a very large download (>100MB).
|
|
// If Boost is already present on your system, define the REACT_NATIVE_BOOST_PATH env variable
|
|
// and the build will use that.
|
|
def boostPath = dependenciesPath ?: System.getenv("REACT_NATIVE_BOOST_PATH") ?: 'packages/boost.1.68.0.0/lib/native/include'
|
|
|
|
def BoostNugetPath = 'packages/boost.1.68.0.0/lib/native/include'
|
|
def DoubleConversionPath = '../double-conversion'
|
|
def FollyPath = '..'
|
|
def GlogPath = '..'
|
|
def JSCPath = '../jsc'
|
|
def V8Path = 'packages/Office.Google_V8.Android.7.0.276.32-v1'
|
|
|
|
def hasBoostNuget = file(BoostNugetPath).exists()
|
|
|
|
task createNativeDepsDirectories {
|
|
downloadsDir.mkdirs()
|
|
thirdPartyNdkDir.mkdirs()
|
|
}
|
|
|
|
// On Native side, V8_ENABLED flag is used
|
|
def isV8Enabled() {
|
|
return true;
|
|
}
|
|
|
|
// For GitHub CI - we use this task
|
|
task downloadBoost(dependsOn: createNativeDepsDirectories, type: Download) {
|
|
src("https://github.com/react-native-community/boost-for-react-native/releases/download/v${BOOST_VERSION.replace("_", ".")}-0/boost_${BOOST_VERSION}.tar.gz")
|
|
onlyIfNewer(true)
|
|
overwrite(false)
|
|
dest(new File(downloadsDir, "boost_${BOOST_VERSION}.tar.gz"))
|
|
}
|
|
|
|
task prepareBoost(dependsOn: boostPath ? [] : [downloadBoost], type: Copy) {
|
|
logger.error(BoostNugetPath.toString())
|
|
from hasBoostNuget ? BoostNugetPath : tarTree(resources.gzip(downloadBoost.dest))
|
|
logger.error("hasBoostNuget: " + hasBoostNuget)
|
|
from("src/main/jni/third-party/boost/Android.mk")
|
|
include 'Android.mk', hasBoostNuget ? 'boost/**/*.hpp' : 'boost_1_63_0/boost/**/*.hpp'
|
|
includeEmptyDirs = false
|
|
into("$thirdPartyNdkDir/boost_tmp")
|
|
doLast {
|
|
if (hasBoostNuget) {
|
|
file("$thirdPartyNdkDir/boost_tmp").renameTo("$thirdPartyNdkDir/boost")
|
|
} else {
|
|
file("$thirdPartyNdkDir/boost_tmp/boost_1_63_0").renameTo("$thirdPartyNdkDir/boost")
|
|
}
|
|
file("$thirdPartyNdkDir/boost_tmp/Android.mk").renameTo("$thirdPartyNdkDir/boost/Android.mk")
|
|
}
|
|
}
|
|
|
|
task prepareDoubleConversion(dependsOn: createNativeDepsDirectories, type: Copy) {
|
|
from(DoubleConversionPath)
|
|
from("src/main/jni/third-party/double-conversion/Android.mk")
|
|
include 'double-conversion/**/*', 'Android.mk'
|
|
includeEmptyDirs = false
|
|
into("$thirdPartyNdkDir/double-conversion")
|
|
}
|
|
|
|
task prepareFolly(dependsOn: createNativeDepsDirectories, type: Copy) {
|
|
from(FollyPath)
|
|
from("src/main/jni/third-party/folly/Android.mk")
|
|
include("Folly/folly/**/*", "Android.mk")
|
|
eachFile {fname -> fname.path = (fname.path - "Folly/")}
|
|
includeEmptyDirs = false
|
|
into("$thirdPartyNdkDir/folly")
|
|
}
|
|
|
|
// Prepare glog sources to be compiled, this task will perform steps that normally should've been
|
|
// executed by automake. This way we can avoid dependencies on make/automake
|
|
task prepareGlog(dependsOn: createNativeDepsDirectories, type: Copy) {
|
|
from(GlogPath)
|
|
from("src/main/jni/third-party/glog/")
|
|
include("glog/src/**/*", "Android.mk", "config.h")
|
|
includeEmptyDirs = false
|
|
filesMatching("**/*.h.in") {
|
|
filter(ReplaceTokens, tokens: [
|
|
ac_cv_have_unistd_h : "1",
|
|
ac_cv_have_stdint_h : "1",
|
|
ac_cv_have_systypes_h : "1",
|
|
ac_cv_have_inttypes_h : "1",
|
|
ac_cv_have_libgflags : "0",
|
|
ac_google_start_namespace : "namespace google {",
|
|
ac_cv_have_uint16_t : "1",
|
|
ac_cv_have_u_int16_t : "1",
|
|
ac_cv_have___uint16 : "0",
|
|
ac_google_end_namespace : "}",
|
|
ac_cv_have___builtin_expect : "1",
|
|
ac_google_namespace : "google",
|
|
ac_cv___attribute___noinline : "__attribute__ ((noinline))",
|
|
ac_cv___attribute___noreturn : "__attribute__ ((noreturn))",
|
|
ac_cv___attribute___printf_4_5: "__attribute__((__format__ (__printf__, 4, 5)))"
|
|
])
|
|
it.path = (it.name - ".in")
|
|
}
|
|
into("$thirdPartyNdkDir/glog")
|
|
|
|
doLast {
|
|
copy {
|
|
from fileTree(dir: "$thirdPartyNdkDir/glog", includes: ["stl_logging.h", "logging.h", "raw_logging.h", "vlog_is_on.h", "**/glog/log_severity.h"]).files
|
|
includeEmptyDirs = false
|
|
into("$thirdPartyNdkDir/glog/exported/glog")
|
|
}
|
|
}
|
|
}
|
|
|
|
task downloadJSC(dependsOn: createNativeDepsDirectories, type: Download) {
|
|
src("https://registry.npmjs.org/jsc-android/-/jsc-android-${JSC_VERSION}.tgz")
|
|
onlyIfNewer(true)
|
|
overwrite(false)
|
|
dest(new File(downloadsDir, "jsc-${JSC_VERSION}.tar.gz"))
|
|
}
|
|
|
|
// Create Android.mk library module based on jsc from npm
|
|
task prepareJSC(dependsOn: isV8Enabled() ? [] : [downloadJSC]) {
|
|
if (isV8Enabled()) {
|
|
return;
|
|
}
|
|
doLast {
|
|
def jscTar = tarTree(downloadJSC.dest)
|
|
def jscAAR = jscTar.matching({ it.include "**/android-jsc/**/*.aar" }).singleFile
|
|
def soFiles = zipTree(jscAAR).matching({ it.include "**/*.so" })
|
|
|
|
def headerFiles = jscTar.matching({ it.include "**/include/*.h" })
|
|
|
|
copy {
|
|
from(soFiles)
|
|
from(headerFiles)
|
|
from("src/main/jni/third-party/jsc/Android.mk")
|
|
|
|
filesMatching("**/*.h", { it.path = "JavaScriptCore/${it.name}" })
|
|
|
|
includeEmptyDirs(false)
|
|
into("$thirdPartyNdkDir/jsc")
|
|
}
|
|
}
|
|
}
|
|
|
|
task downloadNdkBuildDependencies {
|
|
if (!boostPath) {
|
|
dependsOn(downloadBoost)
|
|
}
|
|
dependsOn(downloadJSC)
|
|
}
|
|
|
|
def getNdkBuildName() {
|
|
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
|
|
return "ndk-build.cmd"
|
|
} else {
|
|
return "ndk-build"
|
|
}
|
|
}
|
|
|
|
def findNdkBuildFullPath() {
|
|
// we allow to provide full path to ndk-build tool
|
|
if (hasProperty("ndk.command")) {
|
|
return property("ndk.command")
|
|
}
|
|
// or just a path to the containing directory
|
|
if (hasProperty("ndk.path")) {
|
|
def ndkDir = property("ndk.path")
|
|
return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
|
|
}
|
|
if (System.getenv("ANDROID_NDK") != null) {
|
|
def ndkDir = System.getenv("ANDROID_NDK")
|
|
return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
|
|
}
|
|
def ndkDir = android.hasProperty("plugin") ? android.plugin.ndkFolder :
|
|
plugins.getPlugin("com.android.library").hasProperty("sdkHandler") ?
|
|
plugins.getPlugin("com.android.library").sdkHandler.getNdkFolder() :
|
|
android.ndkDirectory.absolutePath
|
|
if (ndkDir) {
|
|
return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
|
|
}
|
|
return null
|
|
}
|
|
|
|
def getNdkBuildFullPath() {
|
|
def ndkBuildFullPath = findNdkBuildFullPath()
|
|
if (ndkBuildFullPath == null) {
|
|
throw new GradleScriptException(
|
|
"ndk-build binary cannot be found, check if you've set " +
|
|
"\$ANDROID_NDK environment variable correctly or if ndk.dir is " +
|
|
"setup in local.properties",
|
|
null)
|
|
}
|
|
if (!new File(ndkBuildFullPath).canExecute()) {
|
|
throw new GradleScriptException(
|
|
"ndk-build binary " + ndkBuildFullPath + " doesn't exist or isn't executable.\n" +
|
|
"Check that the \$ANDROID_NDK environment variable, or ndk.dir in local.properties, is set correctly.\n" +
|
|
"(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)",
|
|
null)
|
|
}
|
|
return ndkBuildFullPath
|
|
}
|
|
|
|
task buildReactNdkLib(dependsOn: [prepareJSC, prepareBoost, prepareDoubleConversion, prepareFolly, prepareGlog], type: Exec) {
|
|
inputs.dir("src/main/jni/react")
|
|
outputs.dir("$buildDir/react-ndk/all")
|
|
commandLine(getNdkBuildFullPath(),
|
|
"NDK_PROJECT_PATH=null",
|
|
"NDK_APPLICATION_MK=$projectDir/src/main/jni/Application.mk",
|
|
"NDK_OUT=" + temporaryDir,
|
|
"NDK_LIBS_OUT=$buildDir/react-ndk/all",
|
|
"THIRD_PARTY_NDK_DIR=$buildDir/third-party-ndk",
|
|
"THIRD_PARTY_NDK_SRC_DIR=$projectDir/src/main/jni/third-party",
|
|
"V8_NUGET_DIR=$projectDir/$V8Path",
|
|
"REACT_COMMON_DIR=$projectDir/../ReactCommon",
|
|
"REACT_SRC_DIR=$projectDir/src/main/java/com/facebook/react",
|
|
"-C", file("src/main/jni/react/jni").absolutePath,
|
|
"--jobs", project.findProperty("jobs") ?: Runtime.runtime.availableProcessors()
|
|
)
|
|
}
|
|
|
|
task cleanReactNdkLib(type: Exec) {
|
|
ignoreExitValue(true)
|
|
errorOutput(new ByteArrayOutputStream())
|
|
commandLine(getNdkBuildFullPath(),
|
|
"NDK_APPLICATION_MK=$projectDir/src/main/jni/Application.mk",
|
|
"THIRD_PARTY_NDK_DIR=$buildDir/third-party-ndk",
|
|
"THIRD_PARTY_NDK_SRC_DIR=$projectDir/src/main/jni/third-party",
|
|
"V8_NUGET_DIR=$projectDir/$V8Path",
|
|
"REACT_COMMON_DIR=$projectDir/../ReactCommon",
|
|
"REACT_SRC_DIR=$projectDir/src/main/java/com/facebook/react",
|
|
"-C", file("src/main/jni/react/jni").absolutePath,
|
|
"clean")
|
|
doLast {
|
|
file(AAR_OUTPUT_URL).delete()
|
|
println("Deleted aar output dir at ${file(AAR_OUTPUT_URL)}")
|
|
}
|
|
}
|
|
|
|
// usage : gradlew -Pparam="excludeLibs" <taskname>
|
|
task packageReactNdkLibs(dependsOn: buildReactNdkLib, type: Copy) {
|
|
def arg = project.hasProperty('param') ? project.property('param') : 'noArg'
|
|
from "$buildDir/react-ndk/all"
|
|
from("$thirdPartyNdkDir/jsc/jni")
|
|
if(arg.equals("excludeLibs")) {
|
|
exclude '**/*.so'
|
|
} else {
|
|
exclude '**/libjsc.so'
|
|
}
|
|
into "$buildDir/react-ndk/exported"
|
|
}
|
|
|
|
task packageReactNdkLibsForBuck(dependsOn: packageReactNdkLibs, type: Copy) {
|
|
from("$buildDir/react-ndk/exported")
|
|
into("src/main/jni/prebuilt/lib")
|
|
}
|
|
|
|
android {
|
|
compileSdkVersion 28
|
|
|
|
compileOptions {
|
|
sourceCompatibility(JavaVersion.VERSION_1_8)
|
|
targetCompatibility(JavaVersion.VERSION_1_8)
|
|
}
|
|
|
|
defaultConfig {
|
|
minSdkVersion(16)
|
|
targetSdkVersion(28)
|
|
versionCode(1)
|
|
versionName("1.0")
|
|
|
|
consumerProguardFiles("proguard-rules.pro")
|
|
|
|
ndk {
|
|
moduleName("reactnativejni")
|
|
}
|
|
|
|
buildConfigField("boolean", "IS_INTERNAL_BUILD", "false")
|
|
buildConfigField("int", "EXOPACKAGE_FLAGS", "0")
|
|
testApplicationId("com.facebook.react.tests.gradle")
|
|
testInstrumentationRunner("android.support.test.runner.AndroidJUnitRunner")
|
|
}
|
|
|
|
sourceSets.main {
|
|
jni.srcDirs = []
|
|
jniLibs.srcDir("$buildDir/react-ndk/exported")
|
|
res.srcDirs = ["src/main/res/devsupport", "src/main/res/shell", "src/main/res/views/modal", "src/main/res/views/uimanager"]
|
|
java {
|
|
srcDirs = ["src/main/java", "src/main/libraries/soloader/java", "src/main/jni/first-party/fb/jni/java"]
|
|
exclude("com/facebook/react/processing")
|
|
exclude("com/facebook/react/module/processing")
|
|
}
|
|
}
|
|
|
|
tasks.withType(JavaCompile) {
|
|
compileTask -> compileTask.dependsOn(packageReactNdkLibs)
|
|
}
|
|
|
|
clean.dependsOn(cleanReactNdkLib)
|
|
|
|
lintOptions {
|
|
abortOnError(false)
|
|
}
|
|
packagingOptions {
|
|
exclude("META-INF/NOTICE")
|
|
exclude("META-INF/LICENSE")
|
|
}
|
|
}
|
|
|
|
dependencies {
|
|
if (System.getenv("BUCK_BUILD") != "1")
|
|
{
|
|
if (isSDXPlatform()) {
|
|
annotationProcessor project(':react-native:processor');
|
|
}
|
|
else {
|
|
annotationProcessor project(':processor');
|
|
}
|
|
}
|
|
|
|
api("com.facebook.infer.annotation:infer-annotation:0.11.2")
|
|
api("javax.inject:javax.inject:1")
|
|
api("com.android.support:appcompat-v7:28.0.0")
|
|
api("com.facebook.fresco:fresco:${FRESCO_VERSION}")
|
|
api("com.facebook.fresco:imagepipeline-okhttp3:${FRESCO_VERSION}")
|
|
api("com.facebook.soloader:soloader:${SO_LOADER_VERSION}")
|
|
api("com.google.code.findbugs:jsr305:3.0.2")
|
|
api("com.squareup.okhttp3:okhttp:${OKHTTP_VERSION}")
|
|
api("com.squareup.okhttp3:okhttp-urlconnection:${OKHTTP_VERSION}")
|
|
api("com.squareup.okio:okio:1.15.0")
|
|
|
|
testImplementation("junit:junit:${JUNIT_VERSION}")
|
|
testImplementation("org.powermock:powermock-api-mockito:${POWERMOCK_VERSION}")
|
|
testImplementation("org.powermock:powermock-module-junit4-rule:${POWERMOCK_VERSION}")
|
|
testImplementation("org.powermock:powermock-classloading-xstream:${POWERMOCK_VERSION}")
|
|
testImplementation("org.mockito:mockito-core:${MOCKITO_CORE_VERSION}")
|
|
testImplementation("org.easytesting:fest-assert-core:${FEST_ASSERT_CORE_VERSION}")
|
|
testImplementation("org.robolectric:robolectric:${ROBOLECTRIC_VERSION}")
|
|
|
|
androidTestImplementation(fileTree(dir: "src/main/third-party/java/buck-android-support/", include: ["*.jar"]))
|
|
androidTestImplementation("com.android.support.test:runner:${ANDROID_SUPPORT_TEST_VERSION}")
|
|
androidTestImplementation("com.android.support.test:rules:${ANDROID_SUPPORT_TEST_VERSION}")
|
|
androidTestImplementation("org.mockito:mockito-core:${MOCKITO_CORE_VERSION}")
|
|
}
|
|
|
|
apply(from: "release.gradle") |