Browse Source

fix(android): cleanup `build.gradle` & fix #218 (#238)

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-e60f3ee744.png)
![image](https://user-images.githubusercontent.com/5347038/50959955-22b1de00-14bc-11e9-8afa-d1abbba46a.png)

cc @terrytianliang
Michael Diarmid 5 years ago
parent
commit
24ec4f752c
1 changed files with 97 additions and 67 deletions
  1. 97
    67
      android/build.gradle

+ 97
- 67
android/build.gradle View File

@@ -1,92 +1,122 @@
1 1
 buildscript {
2
-    ext.kotlin_version = '1.2.71'
3
-    repositories {
4
-        google()
5
-        jcenter()
6
-        maven {
7
-            url 'https://maven.fabric.io/public'
8
-        }
9
-    }
10
-    dependencies {
11
-        classpath 'com.android.tools.build:gradle:3.2.1'
12
-        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13
-    }
2
+  ext.kotlin_version = '1.2.71'
3
+  repositories {
4
+    google()
5
+    jcenter()
6
+  }
7
+  dependencies {
8
+    classpath 'com.android.tools.build:gradle:3.2.1'
9
+    //noinspection DifferentKotlinGradleVersion
10
+    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11
+  }
14 12
 }
15 13
 
16 14
 apply plugin: 'com.android.library'
17 15
 apply plugin: 'kotlin-android'
18 16
 
19
-
17
+def DEFAULT_TARGET_SDK_VERSION = 27
20 18
 def DEFAULT_COMPILE_SDK_VERSION = 27
21 19
 def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3"
22
-def DEFAULT_TARGET_SDK_VERSION = 27
20
+
21
+def getExtOrDefault(name, defaultValue) {
22
+  return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue
23
+}
23 24
 
24 25
 android {
25
-    compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
26
-    buildToolsVersion rootProject.hasProperty('buildToolsVersion') ? rootProject.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION
27
-    defaultConfig {
28
-        minSdkVersion 16
29
-        targetSdkVersion rootProject.hasProperty('targetSdkVersion') ? rootProject.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
30
-        versionCode 1
31
-        versionName "1.0"
32
-    }
33
-    buildTypes {
34
-        release {
35
-            minifyEnabled false
36
-        }
37
-    }
38
-    productFlavors {
39
-    }
40
-    lintOptions {
41
-        disable 'GradleCompatible'
42
-    }
43
-    compileOptions {
44
-        sourceCompatibility JavaVersion.VERSION_1_8
45
-        targetCompatibility JavaVersion.VERSION_1_8
26
+  compileSdkVersion getExtOrDefault('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
27
+  buildToolsVersion getExtOrDefault('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
28
+  defaultConfig {
29
+    minSdkVersion 16
30
+    targetSdkVersion getExtOrDefault('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
31
+    versionCode 1
32
+    versionName "1.0"
33
+  }
34
+  buildTypes {
35
+    release {
36
+      minifyEnabled false
46 37
     }
38
+  }
39
+  lintOptions {
40
+    disable 'GradleCompatible'
41
+  }
42
+  compileOptions {
43
+    sourceCompatibility JavaVersion.VERSION_1_8
44
+    targetCompatibility JavaVersion.VERSION_1_8
45
+  }
47 46
 }
48 47
 
49 48
 repositories {
50
-    mavenCentral()
51
-    jcenter()
49
+  mavenCentral()
50
+  jcenter()
51
+
52
+  def found = false
53
+  def defaultDir = null
54
+  def androidSourcesName = 'React Native sources'
55
+
56
+  if (rootProject.ext.has('reactNativeAndroidRoot')) {
57
+    defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
58
+  } else {
59
+    defaultDir = new File(
60
+      projectDir,
61
+      '/../../../node_modules/react-native/android'
62
+    )
63
+  }
64
+
65
+  if (defaultDir.exists()) {
52 66
     maven {
53
-        url 'https://maven.google.com/'
54
-        name 'Google'
67
+      url defaultDir.toString()
68
+      name androidSourcesName
55 69
     }
56 70
 
57
-    // Stolen from react-native-firebase, thanks dudes!
58
-    def found = false
71
+    logger.quiet(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
72
+    found = true
73
+  } else {
59 74
     def parentDir = rootProject.projectDir
60
-    def reactNativeAndroidName = 'React Native (Node Modules)'
61
-
62
-    1.upto(4, {
63
-        if (found) return true
64
-        parentDir = parentDir.parentFile
65
-        def reactNativeAndroid = new File(
66
-                parentDir,
67
-                'node_modules/react-native/android'
68
-        )
69
-
70
-        if (reactNativeAndroid.exists()) {
71
-            maven {
72
-                url reactNativeAndroid.toString()
73
-                name reactNativeAndroidName
74
-            }
75
-
76
-            println "${project.name}: using React Native sources from ${reactNativeAndroid.toString()}"
77
-            found = true
75
+
76
+    1.upto(5, {
77
+      if (found) return true
78
+      parentDir = parentDir.parentFile
79
+
80
+      def androidSourcesDir = new File(
81
+        parentDir,
82
+        'node_modules/react-native'
83
+      )
84
+
85
+      def androidPrebuiltBinaryDir = new File(
86
+        parentDir,
87
+        'node_modules/react-native/android'
88
+      )
89
+
90
+      if (androidPrebuiltBinaryDir.exists()) {
91
+        maven {
92
+          url androidPrebuiltBinaryDir.toString()
93
+          name androidSourcesName
78 94
         }
95
+
96
+        logger.quiet(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
97
+        found = true
98
+      } else if (androidSourcesDir.exists()) {
99
+        maven {
100
+          url androidSourcesDir.toString()
101
+          name androidSourcesName
102
+        }
103
+
104
+        logger.quiet(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
105
+        found = true
106
+      }
79 107
     })
108
+  }
80 109
 
81
-    if (!found) {
82
-        throw new GradleException(
83
-                "${project.name}: unable to locate React Native Android sources, " +
84
-                        "ensure you have you installed React Native as a dependency and try again."
85
-        )
86
-    }
110
+  if (!found) {
111
+    throw new GradleException(
112
+      "${project.name}: unable to locate React Native android sources. " +
113
+        "Ensure you have you installed React Native as a dependency in your project and try again."
114
+    )
115
+  }
87 116
 }
88 117
 
89 118
 dependencies {
90
-    implementation 'com.facebook.react:react-native:+'
91
-    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
119
+  //noinspection GradleDynamicVersion
120
+  api 'com.facebook.react:react-native:+'
121
+  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
92 122
 }