Skip to content

Commit 107ffa6

Browse files
committed
feat: use stripped native so libs
1 parent ce0262e commit 107ffa6

1 file changed

Lines changed: 45 additions & 15 deletions

File tree

  • gradle-plugins/react/brownfield/src/main/kotlin/com/callstack/react/brownfield/processors

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

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,41 @@ class JNILibsProcessor : BaseProject() {
4848
listOf("arm64-v8a", "armeabi-v7a", "x86_64", "x86")
4949
.associateWith { mutableListOf<String>() }
5050
.toMutableMap()
51-
5251
for (archiveLibrary in aarLibraries) {
5352
val jniDir = archiveLibrary.getJniDir()
5453
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) }
58-
}
54+
copyStrippedSoLibs(variant, existingJNILibs)
5955
}
6056
}
6157
}
6258
}
6359

64-
private fun copySoLibsTask(variant: LibraryVariant): TaskProvider<Copy> {
60+
private fun getStrippedLibsPath(variant: LibraryVariant): Pair<File, File> {
61+
val projectExt = project.extensions.getByType(Extension::class.java)
62+
val appProject = project.rootProject.project(projectExt.appProjectName)
63+
val appBuildDir = appProject.layout.buildDirectory.get()
64+
6565
val variantName = variant.name
6666
val capitalizedVariant = variantName.replaceFirstChar(Char::titlecase)
6767

68+
val fromDir = appBuildDir
69+
.dir("intermediates/stripped_native_libs/$variantName/strip${capitalizedVariant}DebugSymbols/out/lib")
70+
.asFile
71+
72+
val intoDir = project.rootProject.file("${project.name}/libs$capitalizedVariant")
73+
74+
return Pair(fromDir, intoDir)
75+
}
76+
77+
private fun copySoLibsTask(variant: LibraryVariant): TaskProvider<Copy> {
78+
val capitalizedVariant = variant.name.replaceFirstChar(Char::titlecase)
6879
val projectExt = project.extensions.getByType(Extension::class.java)
6980
val appProject = project.rootProject.project(projectExt.appProjectName)
70-
val appBuildDir = appProject.layout.buildDirectory.get()
7181

7282
val stripTask = ":${appProject.name}:strip${capitalizedVariant}DebugSymbols"
7383
val codegenTask = ":${project.name}:generateCodegenSchemaFromJavaScript"
7484

75-
val fromDir =
76-
appBuildDir
77-
.dir("intermediates/stripped_native_libs/$variantName/strip${capitalizedVariant}DebugSymbols/out/lib")
78-
.asFile
79-
80-
val intoDir =
81-
project.rootProject
82-
.file("${project.name}/libs$capitalizedVariant")
85+
val (fromDir, intoDir) = getStrippedLibsPath(variant)
8386

8487
return project.tasks.register("copy${capitalizedVariant}LibSources", Copy::class.java) {
8588
it.dependsOn(stripTask, codegenTask)
@@ -91,6 +94,33 @@ class JNILibsProcessor : BaseProject() {
9194
}
9295
}
9396

97+
private fun copyStrippedSoLibs(
98+
variant: LibraryVariant,
99+
existingJNILibs: MutableMap<String, MutableList<String>>
100+
) {
101+
val (fromDir, intoDir) = getStrippedLibsPath(variant)
102+
103+
existingJNILibs.forEach { (arch, libNames) ->
104+
val sourceArchDir = File(fromDir, arch)
105+
if (!sourceArchDir.exists()) return@forEach
106+
107+
val destArchDir = File(intoDir, arch).apply { mkdirs() }
108+
109+
libNames.forEach { libName ->
110+
val sourceFile = File(sourceArchDir, libName)
111+
val destFile = File(destArchDir, libName)
112+
113+
if (sourceFile.exists()) {
114+
try {
115+
sourceFile.copyTo(destFile, overwrite = true)
116+
} catch (e: Exception) {
117+
Logging.log("Failed to copy $libName: ${e.message}")
118+
}
119+
}
120+
}
121+
}
122+
}
123+
94124
private fun processNestedLibs(
95125
files: Array<File>?,
96126
existingJNILibs: MutableMap<String, MutableList<String>>,

0 commit comments

Comments
 (0)