Skip to content

Fix ndk build. #4311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.firebase.gradle

import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

abstract class NdkBinaryFixTask : DefaultTask() {
@get:InputFile
abstract val inputFile: RegularFileProperty

@get:OutputFile
val outputFile: File
get() = inputFile.get().asFile.let {
File(it.parentFile, "lib${it.name}.so")
}

@get:Internal
val into: String
get() = "jni/${outputFile.parentFile.name}"

@TaskAction
fun run() {
Files.copy(
inputFile.get().asFile.toPath(),
outputFile.toPath(),
StandardCopyOption.REPLACE_EXISTING
)
}
}
54 changes: 20 additions & 34 deletions firebase-crashlytics-ndk/firebase-crashlytics-ndk.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,45 +65,31 @@ android {
}
}

// There is not any normal way to package native executables in an Android APK.
// It is normal to package native code as a loadable module but Android's APK
// installer will ignore files not named like a shared object, so give the
// handler executable an acceptable name
libraryVariants.all { variant ->
variant.outputs.each { output ->
def func = fixTrampolineFilenames(variant.baseName)

tasks.findAll {
it.name.startsWith("bundleReleaseAar")
}.each {
it.dependsOn func
def fixTasks = ["x86", "x86_64", "armeabi-v7a", "arm64-v8a"].collect { arch ->
tasks.register("fixTrampolineFilenames${variant.baseName}${arch}", com.google.firebase.gradle.NdkBinaryFixTask) {
it.inputFile =
file("${buildDir}/intermediates/ndkBuild/${variant.baseName}/obj/local/${arch}/crashlytics-trampoline")
}
}

tasks.findAll {
it.name.startsWith("externalNativeBuild") && !it.name.contains("Clean")
}.each {
func.dependsOn it
tasks.withType(com.android.build.gradle.tasks.BundleAar) {
if (it.variantName != variant.baseName) return
fixTasks.each { fix ->
it.dependsOn fix
it.from(fix.map { it.outputFile }) {
into fix.map { it.into }
}
}
}
}
}


import java.nio.file.Files
import java.nio.file.StandardCopyOption

// There is not any normal way to package native executables in an Android APK.
// It is normal to package native code as a loadable module but Android's APK
// installer will ignore files not named like a shared object, so give the
// handler executable an acceptable name
def fixTrampolineFilenames(variantBaseName) {
project.task("fixTrampolineFilenames${variantBaseName}").configure({
}).doFirst {
["x86", "x86_64", "armeabi-v7a", "arm64-v8a"].each { arch ->
def initial = new File(
"${buildDir}/intermediates/ndkBuild/${variantBaseName}/obj/local/${arch}/crashlytics-trampoline")
def renamed = new File(
"${buildDir}/intermediates/ndkBuild/${variantBaseName}/obj/local/${arch}/libcrashlytics-trampoline.so")

// There is no need to delete the original file, it will not be
// packaged into the APK
Files.copy(initial.toPath(), renamed.toPath(), StandardCopyOption.REPLACE_EXISTING)
tasks.findAll {
it.name.startsWith("externalNativeBuild") && !it.name.contains("Clean")
}.each { task ->
fixTasks.each { fix -> fix.configure { it.dependsOn task } }
}
}
}
Expand Down