Skip to content

Commit 6d8b4d3

Browse files
committed
Fix ndk build.
Due to AGP upgrade the crashlytics-trampoline.so file stopped being included in the resulting AAR, this causes issues on devices with api 29+.
1 parent d719e10 commit 6d8b4d3

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.google.firebase.gradle
2+
3+
import java.io.File
4+
import java.nio.file.Files
5+
import java.nio.file.StandardCopyOption
6+
import org.gradle.api.DefaultTask
7+
import org.gradle.api.file.RegularFileProperty
8+
import org.gradle.api.tasks.InputFiles
9+
import org.gradle.api.tasks.Internal
10+
import org.gradle.api.tasks.OutputFiles
11+
import org.gradle.api.tasks.TaskAction
12+
13+
abstract class NdkBinaryFixTask : DefaultTask() {
14+
@get:InputFiles
15+
abstract val inputFile: RegularFileProperty
16+
17+
@get:OutputFiles
18+
val outputFile: File
19+
get() = File("${inputFile.get().asFile.absolutePath}.so")
20+
21+
@get:Internal
22+
val into: String
23+
get() = "jni/${outputFile.parentFile.name}"
24+
25+
@TaskAction
26+
fun run() {
27+
Files.copy(
28+
inputFile.get().asFile.toPath(),
29+
File("${inputFile.get().asFile.absolutePath}.so").toPath(),
30+
StandardCopyOption.REPLACE_EXISTING
31+
)
32+
}
33+
}

firebase-crashlytics-ndk/firebase-crashlytics-ndk.gradle

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,45 +65,31 @@ android {
6565
}
6666
}
6767

68+
// There is not any normal way to package native executables in an Android APK.
69+
// It is normal to package native code as a loadable module but Android's APK
70+
// installer will ignore files not named like a shared object, so give the
71+
// handler executable an acceptable name
6872
libraryVariants.all { variant ->
69-
variant.outputs.each { output ->
70-
def func = fixTrampolineFilenames(variant.baseName)
71-
72-
tasks.findAll {
73-
it.name.startsWith("bundleReleaseAar")
74-
}.each {
75-
it.dependsOn func
73+
def fixTasks = ["x86", "x86_64", "armeabi-v7a", "arm64-v8a"].collect { arch ->
74+
tasks.register("fixTrampolineFilenames${variant.baseName}${arch}", com.google.firebase.gradle.NdkBinaryFixTask) {
75+
it.inputFile =
76+
file("${buildDir}/intermediates/ndkBuild/${variant.baseName}/obj/local/${arch}/crashlytics-trampoline")
7677
}
78+
}
7779

78-
tasks.findAll {
79-
it.name.startsWith("externalNativeBuild") && !it.name.contains("Clean")
80-
}.each {
81-
func.dependsOn it
80+
tasks.withType(com.android.build.gradle.tasks.BundleAar) {
81+
if (it.variantName != variant.baseName) return
82+
fixTasks.each { fix ->
83+
it.dependsOn fix
84+
it.from(fix.map { it.outputFile }) {
85+
into fix.map { it.into }
86+
}
8287
}
8388
}
84-
}
85-
}
86-
87-
88-
import java.nio.file.Files
89-
import java.nio.file.StandardCopyOption
90-
91-
// There is not any normal way to package native executables in an Android APK.
92-
// It is normal to package native code as a loadable module but Android's APK
93-
// installer will ignore files not named like a shared object, so give the
94-
// handler executable an acceptable name
95-
def fixTrampolineFilenames(variantBaseName) {
96-
project.task("fixTrampolineFilenames${variantBaseName}").configure({
97-
}).doFirst {
98-
["x86", "x86_64", "armeabi-v7a", "arm64-v8a"].each { arch ->
99-
def initial = new File(
100-
"${buildDir}/intermediates/ndkBuild/${variantBaseName}/obj/local/${arch}/crashlytics-trampoline")
101-
def renamed = new File(
102-
"${buildDir}/intermediates/ndkBuild/${variantBaseName}/obj/local/${arch}/libcrashlytics-trampoline.so")
103-
104-
// There is no need to delete the original file, it will not be
105-
// packaged into the APK
106-
Files.copy(initial.toPath(), renamed.toPath(), StandardCopyOption.REPLACE_EXISTING)
89+
tasks.findAll {
90+
it.name.startsWith("externalNativeBuild") && !it.name.contains("Clean")
91+
}.each { task ->
92+
fixTasks.each { fix -> fix.configure { it.dependsOn task } }
10793
}
10894
}
10995
}

0 commit comments

Comments
 (0)