From 24ec4f752cdfd29d49b0a13a0035aaea736eb641 Mon Sep 17 00:00:00 2001 From: Michael Diarmid Date: Thu, 10 Jan 2019 10:23:46 +0000 Subject: [PATCH] fix(android): cleanup `build.gradle` & fix #218 (#238) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This changes the following: - Fixes #218 - Removed unused Fabric Maven config - Versions now correctly checking `rootProject.ext.X` instead of `rootProject.X` to match behaviour from all other libs (RNFB needs changing as well 🙈) - Swapped `implementation` for `api` for react native sources - only used to build. ![image](https://user-images.githubusercontent.com/5347038/50959941-1af23980-14bc-11e9-8677-e60f3ee744e7.png) ![image](https://user-images.githubusercontent.com/5347038/50959955-22b1de00-14bc-11e9-8afa-d1abbba46a64.png) cc @terrytianliang --- android/build.gradle | 160 +++++++++++++++++++++++++------------------ 1 file changed, 95 insertions(+), 65 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index cb174a3..7bcb7b4 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,92 +1,122 @@ buildscript { - ext.kotlin_version = '1.2.71' - repositories { - google() - jcenter() - maven { - url 'https://maven.fabric.io/public' - } - } - dependencies { - classpath 'com.android.tools.build:gradle:3.2.1' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } + ext.kotlin_version = '1.2.71' + repositories { + google() + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.2.1' + //noinspection DifferentKotlinGradleVersion + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } } apply plugin: 'com.android.library' apply plugin: 'kotlin-android' - +def DEFAULT_TARGET_SDK_VERSION = 27 def DEFAULT_COMPILE_SDK_VERSION = 27 def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3" -def DEFAULT_TARGET_SDK_VERSION = 27 + +def getExtOrDefault(name, defaultValue) { + return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue +} android { - compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION - buildToolsVersion rootProject.hasProperty('buildToolsVersion') ? rootProject.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION - defaultConfig { - minSdkVersion 16 - targetSdkVersion rootProject.hasProperty('targetSdkVersion') ? rootProject.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION - versionCode 1 - versionName "1.0" - } - buildTypes { - release { - minifyEnabled false - } - } - productFlavors { - } - lintOptions { - disable 'GradleCompatible' - } - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 + compileSdkVersion getExtOrDefault('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION) + buildToolsVersion getExtOrDefault('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION) + defaultConfig { + minSdkVersion 16 + targetSdkVersion getExtOrDefault('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION) + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false } + } + lintOptions { + disable 'GradleCompatible' + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } } repositories { - mavenCentral() - jcenter() + mavenCentral() + jcenter() + + def found = false + def defaultDir = null + def androidSourcesName = 'React Native sources' + + if (rootProject.ext.has('reactNativeAndroidRoot')) { + defaultDir = rootProject.ext.get('reactNativeAndroidRoot') + } else { + defaultDir = new File( + projectDir, + '/../../../node_modules/react-native/android' + ) + } + + if (defaultDir.exists()) { maven { - url 'https://maven.google.com/' - name 'Google' + url defaultDir.toString() + name androidSourcesName } - // Stolen from react-native-firebase, thanks dudes! - def found = false + logger.quiet(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}") + found = true + } else { def parentDir = rootProject.projectDir - def reactNativeAndroidName = 'React Native (Node Modules)' - 1.upto(4, { - if (found) return true - parentDir = parentDir.parentFile - def reactNativeAndroid = new File( - parentDir, - 'node_modules/react-native/android' - ) + 1.upto(5, { + if (found) return true + parentDir = parentDir.parentFile - if (reactNativeAndroid.exists()) { - maven { - url reactNativeAndroid.toString() - name reactNativeAndroidName - } + def androidSourcesDir = new File( + parentDir, + 'node_modules/react-native' + ) - println "${project.name}: using React Native sources from ${reactNativeAndroid.toString()}" - found = true + def androidPrebuiltBinaryDir = new File( + parentDir, + 'node_modules/react-native/android' + ) + + if (androidPrebuiltBinaryDir.exists()) { + maven { + url androidPrebuiltBinaryDir.toString() + name androidSourcesName } - }) - if (!found) { - throw new GradleException( - "${project.name}: unable to locate React Native Android sources, " + - "ensure you have you installed React Native as a dependency and try again." - ) - } + logger.quiet(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}") + found = true + } else if (androidSourcesDir.exists()) { + maven { + url androidSourcesDir.toString() + name androidSourcesName + } + + logger.quiet(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}") + found = true + } + }) + } + + if (!found) { + throw new GradleException( + "${project.name}: unable to locate React Native android sources. " + + "Ensure you have you installed React Native as a dependency in your project and try again." + ) + } } dependencies { - implementation 'com.facebook.react:react-native:+' - implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + //noinspection GradleDynamicVersion + api 'com.facebook.react:react-native:+' + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" }