@@ -2,10 +2,31 @@ var fs = require("fs");
2
2
var glob = require ( "glob" ) ;
3
3
var path = require ( "path" ) ;
4
4
5
- var ignoreNodeModules = { ignore : "node_modules/**" } ;
6
- var mainApplicationPath = glob . sync ( "**/MainApplication.java" , ignoreNodeModules ) [ 0 ] ;
7
- var mainActivityPath = glob . sync ( "**/MainActivity.java" , ignoreNodeModules ) [ 0 ] ;
5
+ var ignoreFolders = { ignore : [ "node_modules/**" , "**/build/**" ] } ;
8
6
var buildGradlePath = path . join ( "android" , "app" , "build.gradle" ) ;
7
+ var manifestPath = glob . sync ( "**/AndroidManifest.xml" , ignoreFolders ) [ 0 ] ;
8
+
9
+ function findMainApplication ( ) {
10
+ if ( ! manifestPath ) {
11
+ return null ;
12
+ }
13
+
14
+ var manifest = fs . readFileSync ( manifestPath , "utf8" ) ;
15
+
16
+ // Android manifest must include single 'application' element
17
+ var matchResult = manifest . match ( / a p p l i c a t i o n \s + a n d r o i d : n a m e \s * = \s * " ( .* ?) " / ) ;
18
+ if ( matchResult ) {
19
+ var appName = matchResult [ 1 ] ;
20
+ } else {
21
+ return null ;
22
+ }
23
+
24
+ var nameParts = appName . split ( '.' ) ;
25
+ var searchPath = glob . sync ( "**/" + nameParts [ nameParts . length - 1 ] + ".java" , ignoreFolders ) [ 0 ] ;
26
+ return searchPath ;
27
+ }
28
+
29
+ var mainApplicationPath = findMainApplication ( ) || glob . sync ( "**/MainApplication.java" , ignoreFolders ) [ 0 ] ;
9
30
10
31
// 1. Add the getJSBundleFile override
11
32
var getJSBundleFileOverride = `
@@ -30,20 +51,34 @@ if (mainApplicationPath) {
30
51
fs . writeFileSync ( mainApplicationPath , mainApplicationContents ) ;
31
52
}
32
53
} else {
33
- var mainActivityContents = fs . readFileSync ( mainActivityPath , "utf8" ) ;
34
- if ( isAlreadyOverridden ( mainActivityContents ) ) {
35
- console . log ( `"getJSBundleFile" is already overridden` ) ;
54
+ var mainActivityPath = glob . sync ( "**/MainActivity.java" , ignoreFolders ) [ 0 ] ;
55
+ if ( mainActivityPath ) {
56
+ var mainActivityContents = fs . readFileSync ( mainActivityPath , "utf8" ) ;
57
+ if ( isAlreadyOverridden ( mainActivityContents ) ) {
58
+ console . log ( `"getJSBundleFile" is already overridden` ) ;
59
+ } else {
60
+ var mainActivityClassDeclaration = "public class MainActivity extends ReactActivity {" ;
61
+ mainActivityContents = mainActivityContents . replace ( mainActivityClassDeclaration ,
62
+ `${ mainActivityClassDeclaration } \n${ getJSBundleFileOverride } ` ) ;
63
+ fs . writeFileSync ( mainActivityPath , mainActivityContents ) ;
64
+ }
36
65
} else {
37
- var mainActivityClassDeclaration = "public class MainActivity extends ReactActivity {" ;
38
- mainActivityContents = mainActivityContents . replace ( mainActivityClassDeclaration ,
39
- `${ mainActivityClassDeclaration } \n${ getJSBundleFileOverride } ` ) ;
40
- fs . writeFileSync ( mainActivityPath , mainActivityContents ) ;
66
+ console . error ( `Couldn't find Android application entry point. You might need to update it manually. \
67
+ Please refer to plugin configuration section for Android at \
68
+ https://github.com/microsoft/react-native-code-push#plugin-configuration-android for more details` ) ;
41
69
}
42
70
}
43
71
72
+ if ( ! fs . existsSync ( buildGradlePath ) ) {
73
+ console . error ( `Couldn't find build.gradle file. You might need to update it manually. \
74
+ Please refer to plugin installation section for Android at \
75
+ https://github.com/microsoft/react-native-code-push#plugin-installation-android---manual` ) ;
76
+ return ;
77
+ }
78
+
44
79
// 2. Add the codepush.gradle build task definitions
45
80
var buildGradleContents = fs . readFileSync ( buildGradlePath , "utf8" ) ;
46
- var reactGradleLink = buildGradleContents . match ( / \n a p p l y f r o m : " .* ?r e a c t \. g r a d l e " / ) [ 0 ] ;
81
+ var reactGradleLink = buildGradleContents . match ( / \n a p p l y f r o m : [ " ' ] .* ?r e a c t \. g r a d l e [ " ' ] / ) [ 0 ] ;
47
82
var codePushGradleLink = `apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"` ;
48
83
if ( ~ buildGradleContents . indexOf ( codePushGradleLink ) ) {
49
84
console . log ( `"codepush.gradle" is already linked in the build definition` ) ;
0 commit comments