Skip to content

Commit 73e0556

Browse files
authored
feat: use stripped native so libs (#167)
* feat: use stripped native so libs * fix: detekt * fix: detekt * chore: hide behind flag
1 parent d47173d commit 73e0556

2 files changed

Lines changed: 76 additions & 13 deletions

File tree

gradle-plugins/react/brownfield/src/main/kotlin/com/callstack/react/brownfield/processors/JNILibsProcessor.kt

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,54 @@ class JNILibsProcessor : BaseProject() {
4444
it.dependsOn(copyTask)
4545

4646
it.doFirst {
47+
val projectExt = project.extensions.getByType(Extension::class.java)
4748
val existingJNILibs =
4849
listOf("arm64-v8a", "armeabi-v7a", "x86_64", "x86")
4950
.associateWith { mutableListOf<String>() }
5051
.toMutableMap()
51-
5252
for (archiveLibrary in aarLibraries) {
5353
val jniDir = archiveLibrary.getJniDir()
5454
processNestedLibs(jniDir.listFiles(), existingJNILibs)
55-
if (jniDir.exists()) {
56-
val filteredSourceSets = androidExtension.sourceSets.filter { sourceSet -> sourceSet.name == variant.name }
57-
filteredSourceSets.forEach { sourceSet -> sourceSet.jniLibs.srcDir(jniDir) }
55+
if (projectExt.experimentalUseStrippedSoFiles) {
56+
copyStrippedSoLibs(variant, existingJNILibs)
57+
} else {
58+
if (jniDir.exists()) {
59+
val filteredSourceSets = androidExtension.sourceSets.filter { sourceSet -> sourceSet.name == variant.name }
60+
filteredSourceSets.forEach { sourceSet -> sourceSet.jniLibs.srcDir(jniDir) }
61+
}
5862
}
5963
}
6064
}
6165
}
6266
}
6367

64-
private fun copySoLibsTask(variant: LibraryVariant): TaskProvider<Copy> {
65-
val variantName = variant.name
66-
val capitalizedVariant = variantName.replaceFirstChar(Char::titlecase)
67-
68+
private fun getStrippedLibsPath(variant: LibraryVariant): Pair<File, File> {
6869
val projectExt = project.extensions.getByType(Extension::class.java)
6970
val appProject = project.rootProject.project(projectExt.appProjectName)
7071
val appBuildDir = appProject.layout.buildDirectory.get()
7172

72-
val stripTask = ":${appProject.name}:strip${capitalizedVariant}DebugSymbols"
73-
val codegenTask = ":${project.name}:generateCodegenSchemaFromJavaScript"
73+
val variantName = variant.name
74+
val capitalizedVariant = variantName.replaceFirstChar(Char::titlecase)
7475

7576
val fromDir =
7677
appBuildDir
7778
.dir("intermediates/stripped_native_libs/$variantName/strip${capitalizedVariant}DebugSymbols/out/lib")
7879
.asFile
7980

80-
val intoDir =
81-
project.rootProject
82-
.file("${project.name}/libs$capitalizedVariant")
81+
val intoDir = project.rootProject.file("${project.name}/libs$capitalizedVariant")
82+
83+
return Pair(fromDir, intoDir)
84+
}
85+
86+
private fun copySoLibsTask(variant: LibraryVariant): TaskProvider<Copy> {
87+
val capitalizedVariant = variant.name.replaceFirstChar(Char::titlecase)
88+
val projectExt = project.extensions.getByType(Extension::class.java)
89+
val appProject = project.rootProject.project(projectExt.appProjectName)
90+
91+
val stripTask = ":${appProject.name}:strip${capitalizedVariant}DebugSymbols"
92+
val codegenTask = ":${project.name}:generateCodegenSchemaFromJavaScript"
93+
94+
val (fromDir, intoDir) = getStrippedLibsPath(variant)
8395

8496
return project.tasks.register("copy${capitalizedVariant}LibSources", Copy::class.java) {
8597
it.dependsOn(stripTask, codegenTask)
@@ -91,6 +103,50 @@ class JNILibsProcessor : BaseProject() {
91103
}
92104
}
93105

106+
private fun copyStrippedSoLibs(
107+
variant: LibraryVariant,
108+
existingJNILibs: MutableMap<String, MutableList<String>>,
109+
) {
110+
val (fromDir, intoDir) = getStrippedLibsPath(variant)
111+
112+
existingJNILibs.forEach { (arch, libNames) ->
113+
copyLibsForArchitecture(fromDir, intoDir, arch, libNames)
114+
}
115+
}
116+
117+
private fun copyLibsForArchitecture(
118+
fromDir: File,
119+
intoDir: File,
120+
arch: String,
121+
libNames: List<String>,
122+
) {
123+
val sourceArchDir = File(fromDir, arch)
124+
if (!sourceArchDir.exists()) return
125+
126+
val destArchDir = File(intoDir, arch).apply { mkdirs() }
127+
128+
libNames.forEach { libName ->
129+
copyLibFile(sourceArchDir, destArchDir, libName)
130+
}
131+
}
132+
133+
private fun copyLibFile(
134+
sourceArchDir: File,
135+
destArchDir: File,
136+
libName: String,
137+
) {
138+
val sourceFile = File(sourceArchDir, libName)
139+
val destFile = File(destArchDir, libName)
140+
141+
if (sourceFile.exists()) {
142+
try {
143+
sourceFile.copyTo(destFile, overwrite = true)
144+
} catch (e: java.io.IOException) {
145+
Logging.log("Failed to copy $libName: ${e.message}")
146+
}
147+
}
148+
}
149+
94150
private fun processNestedLibs(
95151
files: Array<File>?,
96152
existingJNILibs: MutableMap<String, MutableList<String>>,

gradle-plugins/react/brownfield/src/main/kotlin/com/callstack/react/brownfield/utils/Extension.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,11 @@ open class Extension {
4545
* bundled.
4646
*/
4747
var dynamicLibs = listOf<String>()
48+
49+
/**
50+
* Whether to use stripped .so files.
51+
*
52+
* Default value is `false`
53+
*/
54+
var experimentalUseStrippedSoFiles = false
4855
}

0 commit comments

Comments
 (0)