Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit be3633a

Browse files
authored
Android Gradle Plugin 3.x integration (#1219)
* AAPT2-compatible Android build process integration * enhancement * enhancement * fix assembleDebug
1 parent cf93997 commit be3633a

File tree

2 files changed

+63
-43
lines changed

2 files changed

+63
-43
lines changed

android/codepush.gradle

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Adapted from https://raw.githubusercontent.com/facebook/react-native/master/local-cli/generator-android/templates/src/app/react.gradle
1+
// Adapted from https://raw.githubusercontent.com/facebook/react-native/d16ff3bd8b92fa84a9007bf5ebedd8153e4c089d/react.gradle
22

33
import java.nio.file.Paths;
44

55
def config = project.hasProperty("react") ? project.react : [];
66
def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
77

8+
// because elvis operator
89
def elvisFile(thing) {
910
return thing ? file(thing) : null;
1011
}
@@ -17,37 +18,58 @@ void runBefore(String dependentTaskName, Task task) {
1718
}
1819

1920
gradle.projectsEvaluated {
20-
def buildTypes = android.buildTypes.collect { type -> type.name }
2121
android.buildTypes.each {
2222
// to prevent incorrect long value restoration from strings.xml we need to wrap it with double quotes
2323
// https://github.com/Microsoft/cordova-plugin-code-push/issues/264
2424
it.resValue 'string', "CODE_PUSH_APK_BUILD_TIME", String.format("\"%d\"", System.currentTimeMillis())
2525
}
26-
def productFlavors = android.productFlavors.collect { flavor -> flavor.name }
27-
if (!productFlavors) productFlavors.add('')
28-
def nodeModulesPath;
29-
if (config.root) {
30-
nodeModulesPath = Paths.get(config.root, "/node_modules");
31-
}
32-
else if (project.hasProperty('nodeModulesPath')) {
33-
nodeModulesPath = project.nodeModulesPath
34-
} else {
35-
nodeModulesPath = "../../node_modules";
36-
}
26+
27+
android.applicationVariants.all { variant ->
28+
def nodeModulesPath;
29+
if (config.root) {
30+
nodeModulesPath = Paths.get(config.root, "/node_modules");
31+
} else if (project.hasProperty('nodeModulesPath')) {
32+
nodeModulesPath = project.nodeModulesPath
33+
} else {
34+
nodeModulesPath = "../../node_modules";
35+
}
36+
37+
def targetName = variant.name.capitalize()
38+
def targetPath = variant.dirName
39+
40+
def jsBundleDir;
41+
def resourcesDir;
42+
def jsBundleFile;
3743

38-
productFlavors.each { productFlavorName ->
39-
buildTypes.each { buildTypeName ->
40-
def targetName = "${productFlavorName.capitalize()}${buildTypeName.capitalize()}"
41-
def targetPath = productFlavorName ?
42-
"${productFlavorName}/${buildTypeName}" :
43-
"${buildTypeName}"
44+
// Additional node commandline arguments
45+
def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
46+
def extraPackagerArgs = config.extraPackagerArgs ?: []
47+
48+
// Make this task run right after the bundle task
49+
def generateBundledResourcesHash;
50+
51+
if (variant.hasProperty("bundleJsAndAssets")) {
52+
def reactBundleTask = variant.bundleJsAndAssets
53+
jsBundleDir = reactBundleTask.generatedAssetsFolders[0].absolutePath
54+
resourcesDir = reactBundleTask.generatedResFolders[0].absolutePath
55+
jsBundleFile = file("$jsBundleDir/$bundleAssetName")
56+
57+
generateBundledResourcesHash = tasks.create(
58+
name: "generateBundledResourcesHash${targetName}",
59+
type: Exec) {
60+
commandLine (*nodeExecutableAndArgs, "${nodeModulesPath}/react-native-code-push/scripts/generateBundledResourcesHash.js", resourcesDir, jsBundleFile, jsBundleDir)
4461

62+
enabled config."bundleIn${targetName}" ||
63+
config."bundleIn${variant.buildType.name.capitalize()}" ?:
64+
targetName.toLowerCase().contains("release")
65+
}
66+
} else {
4567
def jsBundleDirConfigName = "jsBundleDir${targetName}"
46-
def jsBundleDir = elvisFile(config."$jsBundleDirConfigName") ?:
68+
jsBundleDir = elvisFile(config."$jsBundleDirConfigName") ?:
4769
file("$buildDir/intermediates/assets/${targetPath}")
4870

4971
def resourcesDirConfigName = "resourcesDir${targetName}"
50-
def resourcesDir = elvisFile(config."${resourcesDirConfigName}") ?:
72+
resourcesDir = elvisFile(config."${resourcesDirConfigName}") ?:
5173
file("$buildDir/intermediates/res/merged/${targetPath}")
5274

5375
// In case version of 'Android Plugin for Gradle'' is lower than 1.3.0
@@ -56,13 +78,16 @@ gradle.projectsEvaluated {
5678
if (!resourcesDir.exists() && file("$buildDir/intermediates/res/${targetPath}").exists()) {
5779
resourcesDir = file("$buildDir/intermediates/res/${targetPath}")
5880
}
59-
60-
def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
81+
82+
jsBundleFile = file("$jsBundleDir/$bundleAssetName")
6183

6284
def resourcesMapTempFileName = "CodePushResourcesMap-" + java.util.UUID.randomUUID().toString().substring(0,8) + ".json"
6385

64-
// Additional node commandline arguments
65-
def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
86+
generateBundledResourcesHash = tasks.create(
87+
name: "generateBundledResourcesHash${targetName}",
88+
type: Exec) {
89+
commandLine (*nodeExecutableAndArgs, "${nodeModulesPath}/react-native-code-push/scripts/generateBundledResourcesHash.js", resourcesDir, jsBundleFile, jsBundleDir, resourcesMapTempFileName)
90+
}
6691

6792
// Make this task run right before the bundle task
6893
def recordFilesBeforeBundleCommand = tasks.create(
@@ -75,21 +100,14 @@ gradle.projectsEvaluated {
75100
recordFilesBeforeBundleCommand.dependsOn("merge${targetName}Assets")
76101
runBefore("bundle${targetName}JsAndAssets", recordFilesBeforeBundleCommand)
77102

78-
// Make this task run right after the bundle task
79-
def generateBundledResourcesHash = tasks.create(
80-
name: "generateBundledResourcesHash${targetName}",
81-
type: Exec) {
82-
commandLine (*nodeExecutableAndArgs, "${nodeModulesPath}/react-native-code-push/scripts/generateBundledResourcesHash.js", resourcesDir, "$jsBundleDir/$bundleAssetName", jsBundleDir, resourcesMapTempFileName)
83-
}
84-
85103
// We need to generate and record the resources map, but we use it to generate the bundle hash
86104
generateBundledResourcesHash.dependsOn("recordFilesBeforeBundleCommand${targetName}")
87-
88-
generateBundledResourcesHash.dependsOn("bundle${targetName}JsAndAssets")
89-
runBefore("processArmeabi-v7a${targetName}Resources", generateBundledResourcesHash)
90-
runBefore("processX86${targetName}Resources", generateBundledResourcesHash)
91-
runBefore("processUniversal${targetName}Resources", generateBundledResourcesHash)
92-
runBefore("process${targetName}Resources", generateBundledResourcesHash)
93105
}
106+
107+
generateBundledResourcesHash.dependsOn("bundle${targetName}JsAndAssets")
108+
runBefore("processArmeabi-v7a${targetName}Resources", generateBundledResourcesHash)
109+
runBefore("processX86${targetName}Resources", generateBundledResourcesHash)
110+
runBefore("processUniversal${targetName}Resources", generateBundledResourcesHash)
111+
runBefore("process${targetName}Resources", generateBundledResourcesHash)
94112
}
95113
}

scripts/generateBundledResourcesHash.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ var jsBundleFilePath = process.argv[3];
2727
var assetsDir = process.argv[4];
2828
var tempFileName = process.argv[5];
2929

30-
var tempFileLocalPath = path.join(require("os").tmpdir(), tempFileName);
30+
var oldFileToModifiedTimeMap = {};
31+
if (tempFileName) {
32+
var tempFileLocalPath = path.join(require("os").tmpdir(), tempFileName);
33+
oldFileToModifiedTimeMap = require(tempFileLocalPath);
34+
fs.unlinkSync(tempFileLocalPath);
35+
}
3136
var resourceFiles = [];
3237

3338
getFilesInFolder(resourcesDir, resourceFiles);
3439

35-
var oldFileToModifiedTimeMap = require(tempFileLocalPath);
3640
var newFileToModifiedTimeMap = {};
3741

3842
resourceFiles.forEach(function(resourceFile) {
@@ -114,6 +118,4 @@ function addFileToManifest(folder, assetFile, manifest, done) {
114118
function fileExists(file) {
115119
try { return fs.statSync(file).isFile(); }
116120
catch (e) { return false; }
117-
}
118-
119-
fs.unlinkSync(tempFileLocalPath);
121+
}

0 commit comments

Comments
 (0)