Skip to content

Commit 3aac867

Browse files
authored
Fix ndk build. (#4311)
* 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+. * fix filename
1 parent d719e10 commit 3aac867

File tree

2 files changed

+68
-34
lines changed

2 files changed

+68
-34
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.firebase.gradle
15+
16+
import java.io.File
17+
import java.nio.file.Files
18+
import java.nio.file.StandardCopyOption
19+
import org.gradle.api.DefaultTask
20+
import org.gradle.api.file.RegularFileProperty
21+
import org.gradle.api.tasks.InputFile
22+
import org.gradle.api.tasks.Internal
23+
import org.gradle.api.tasks.OutputFile
24+
import org.gradle.api.tasks.TaskAction
25+
26+
abstract class NdkBinaryFixTask : DefaultTask() {
27+
@get:InputFile
28+
abstract val inputFile: RegularFileProperty
29+
30+
@get:OutputFile
31+
val outputFile: File
32+
get() = inputFile.get().asFile.let {
33+
File(it.parentFile, "lib${it.name}.so")
34+
}
35+
36+
@get:Internal
37+
val into: String
38+
get() = "jni/${outputFile.parentFile.name}"
39+
40+
@TaskAction
41+
fun run() {
42+
Files.copy(
43+
inputFile.get().asFile.toPath(),
44+
outputFile.toPath(),
45+
StandardCopyOption.REPLACE_EXISTING
46+
)
47+
}
48+
}

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)