Skip to content

Commit

Permalink
fix(android): refactor font copy / support android gradle plugin 4.1+
Browse files Browse the repository at this point in the history
- react-native JS bundle doesn't copy per-release target
- react-native JS bundle adds the copy task as dependent very carefully now!
- I added some metadata for the task, a "group" and "description"
- Fixed formatting, 4-space indents was predominant, I went with that everywhere

this is based heavily off the react-native JS bundle copy task
this was developed in collaboration with @gulshan183

Fixes oblador#1265
  • Loading branch information
mikehardy committed Feb 12, 2021
1 parent 7c3dbab commit 5abc083
Showing 1 changed file with 38 additions and 40 deletions.
78 changes: 38 additions & 40 deletions fonts.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,55 @@
* Task to copy icon font files
*/

def config = project.hasProperty("vectoricons") ? project.vectoricons : [];

def iconFontsDir = config.iconFontsDir ?: "../../node_modules/react-native-vector-icons/Fonts";
def iconFontNames = config.iconFontNames ?: [ "*.ttf" ];
afterEvaluate {
def config = project.hasProperty("vectoricons") ? project.vectoricons : [];
def iconFontsDir = config.iconFontsDir ?: "../../node_modules/react-native-vector-icons/Fonts";
def iconFontNames = config.iconFontNames ?: [ "*.ttf" ];


gradle.projectsEvaluated {
android.applicationVariants.all { def variant ->
def targetName = variant.name.capitalize()
def targetPath = variant.dirName

// Create task for copying fonts
def currentFontTask = tasks.create(
name: "copy${targetName}IconFonts",
type: Copy) {
into("${buildDir}/intermediates")
def currentFontCopyTask = tasks.create(
name: "copy${targetName}ReactNativeVectorIconFonts",
type: Copy) {
group = "react"
description = "copy fonts into ${targetName}."

iconFontNames.each { fontName ->
into("$buildDir/intermediates")
// println("[react-native-vector-icons] Copying font resources in release build")

from(iconFontsDir) {
include(fontName)
into("assets/${targetPath}/fonts/")
}

// Workaround for Android Gradle Plugin 3.2+ new asset directory
from(iconFontsDir) {
include(fontName)
into("merged_assets/${variant.name}/merge${targetName}Assets/out/fonts/")
}

// Workaround for Android Gradle Plugin 3.4+ new asset directory
from(iconFontsDir) {
include(fontName)
into("merged_assets/${variant.name}/out/fonts/")
}
iconFontNames.each { fontName ->
// println(" ...copying ${fontName}")

from(iconFontsDir) {
include(fontName)
into("assets/${targetPath}/fonts/")
}

// Workaround for Android Gradle Plugin 3.2+ new asset directory
from(iconFontsDir) {
include(fontName)
into("merged_assets/${variant.name}/merge${targetName}Assets/out/fonts/")
}

// Workaround for Android Gradle Plugin 3.4+ new asset directory
from(iconFontsDir) {
include(fontName)
into("merged_assets/${variant.name}/out/fonts/")
}
}
}

currentFontTask.dependsOn("merge${targetName}Resources")
currentFontTask.dependsOn("merge${targetName}Assets")

[
"processArmeabi-v7a${targetName}Resources",
"processX86${targetName}Resources",
"processUniversal${targetName}Resources",
"process${targetName}Resources"
].each { name ->
Task dependentTask = tasks.findByPath(name);

if (dependentTask != null) {
dependentTask.dependsOn(currentFontTask)
}
// mergeAssets must run first, as it clears the intermediates directory
dependsOn(variant.mergeAssetsProvider.get())
}

// mergeResources task runs before the bundle file is copied to the intermediate asset directory from Android plugin 4.1+.
// This ensures to copy the bundle file before mergeResources task starts
def mergeResourcesTask = tasks.findByName("merge${targetName}Resources")
mergeResourcesTask.dependsOn(currentFontCopyTask)
}
}

0 comments on commit 5abc083

Please sign in to comment.