Skip to content

Commit 8f4d6ba

Browse files
authored
Merge pull request #1475 from NativeScript/trifonov/copy-metadata
Add copyMetadata task to copy the result metadata if needed
2 parents b10b6d6 + 20a458e commit 8f4d6ba

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

test-app/app/build.gradle

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,29 @@ android {
295295
applyBeforePluginGradleConfiguration()
296296
applyPluginGradleConfigurations()
297297
applyAppGradleConfiguration()
298+
299+
def initializeMergedAssetsOutputPath = { ->
300+
android.applicationVariants.all { variant ->
301+
if (variant.buildType.name == project.selectedBuildType) {
302+
def task
303+
if (variant.metaClass.respondsTo(variant, "getMergeAssetsProvider")) {
304+
def provider = variant.getMergeAssetsProvider()
305+
task = provider.get();
306+
} else {
307+
// fallback for older android gradle plugin versions
308+
task = variant.getMergeAssets()
309+
}
310+
for (File file : task.getOutputs().getFiles()) {
311+
if (!file.getPath().contains("/incremental/")) {
312+
project.ext.mergedAssetsOutputPath = file.getPath()
313+
break;
314+
}
315+
}
316+
}
317+
}
318+
}
319+
320+
initializeMergedAssetsOutputPath()
298321
}
299322

300323
def externalRuntimeExists = !findProject(':runtime').is(null)
@@ -454,8 +477,7 @@ tasks.whenTaskAdded({ org.gradle.api.DefaultTask currentTask ->
454477
}
455478
if (currentTask =~ /compile.+JavaWithJavac/) {
456479
currentTask.dependsOn(runSbg)
457-
currentTask.finalizedBy(ensureMetadataOutDir)
458-
ensureMetadataOutDir.finalizedBy(buildMetadata)
480+
currentTask.finalizedBy(buildMetadata)
459481
}
460482
if (currentTask =~ /merge.*Assets/) {
461483
currentTask.shouldRunAfter(buildMetadata)
@@ -498,13 +520,6 @@ task runSbg(type: BuildToolTask) {
498520
}
499521
}
500522

501-
task ensureMetadataOutDir {
502-
doLast {
503-
def outputDir = file("$METADATA_OUT_PATH")
504-
outputDir.mkdirs()
505-
}
506-
}
507-
508523
def failOnCompilationWarningsEnabled() {
509524
return project.hasProperty("failOnCompilationWarnings") && (failOnCompilationWarnings || failOnCompilationWarnings.toBoolean())
510525
}
@@ -554,6 +569,14 @@ class EmptyRunnable implements Runnable {
554569
}
555570
}
556571

572+
def getMergedAssetsOutputPath() {
573+
if (!project.hasProperty("mergedAssetsOutputPath")) {
574+
// mergedAssetsOutputPath not found fallback to the default value for android gradle plugin 3.5.0
575+
project.ext.mergedAssetsOutputPath = "$projectDir/build/intermediates/merged_assets/" + project.selectedBuildType + "/out"
576+
}
577+
return project.ext.mergedAssetsOutputPath
578+
}
579+
557580
// Discover all jars and dynamically create tasks for the extraction of each of them
558581
project.ext.allJars = []
559582
afterEvaluate { project ->
@@ -702,11 +725,28 @@ task collectAllJars {
702725
}
703726
}
704727

728+
task copyMetadata {
729+
doLast {
730+
copy {
731+
from "$projectDir/src/main/assets/metadata"
732+
into getMergedAssetsOutputPath() + "/metadata"
733+
}
734+
}
735+
}
736+
705737
task buildMetadata(type: BuildToolTask) {
706738
if (!findProject(':android-metadata-generator').is(null)) {
707739
dependsOn ':android-metadata-generator:jar'
708740
}
709741

742+
// As some external gradle plugins can reorder the execution order of the tasks it may happen that buildMetadata is executed after merge{Debug/Release}Assets
743+
// in that case the metadata won't be included in the result apk and it will crash, so to avoid this we are adding the copyMetadata task which will manually copy
744+
// the metadata files in the merge assets folder and they will be added to the result apk
745+
746+
// The next line is added to avoid adding another copyData implementation from the firebase plugin - https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/3943bb9147f43c41599e801d026378eba93d3f3a/publish/scripts/installer.js#L1105
747+
//buildMetadata.finalizedBy(copyMetadata)
748+
finalizedBy copyMetadata
749+
710750
description "builds metadata with provided jar dependencies"
711751

712752
inputs.files("$MDG_JAVA_DEPENDENCIES")
@@ -726,6 +766,8 @@ task buildMetadata(type: BuildToolTask) {
726766
// these need to be called after the classes have compiled
727767
assert file(classesDir).exists()
728768

769+
new File(getMergedAssetsOutputPath() + "/metadata").deleteDir()
770+
729771
def classesSubDirs = new File(classesDir).listFiles()
730772
def selectedBuildType = project.ext.selectedBuildType
731773

0 commit comments

Comments
 (0)