-
Notifications
You must be signed in to change notification settings - Fork 10
Add Gradle task to upload symbols and mapping #457
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ | |
| .externalNativeBuild | ||
| .cxx | ||
| local.properties | ||
| .kotlin | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| cmake_minimum_required(VERSION 3.6.0) | ||
| project(native-lib) | ||
|
|
||
| add_library( | ||
| native-lib | ||
| SHARED | ||
| native.cpp) | ||
|
|
||
| find_library( | ||
| log-lib | ||
| log) | ||
|
|
||
| target_link_libraries( | ||
| native-lib | ||
| ${log-lib}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include <jni.h> | ||
| #include <string.h> | ||
|
|
||
| // Leave this public so that the compiler can't elide the null ptr access. | ||
| char *invalid_ptr = nullptr; | ||
|
|
||
| static void trigger_sefgault() { | ||
| *invalid_ptr = 0; | ||
| } | ||
|
|
||
| extern "C" | ||
| JNIEXPORT jstring JNICALL | ||
| Java_io_bitdrift_gradleexample_MainActivity_stringFromJNI(JNIEnv *env, jobject thiz) { | ||
| return env->NewStringUTF("This is a string coming via JNI"); | ||
| } | ||
|
|
||
| extern "C" | ||
| JNIEXPORT void JNICALL | ||
| Java_io_bitdrift_gradleexample_FirstFragment_triggerSegfault(JNIEnv *env, jobject thiz) { | ||
| trigger_sefgault(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
platform/jvm/capture-plugin/src/main/kotlin/io/bitdrift/capture/task/CLITask.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // capture-sdk - bitdrift's client SDK | ||
| // Copyright Bitdrift, Inc. All rights reserved. | ||
| // | ||
| // Use of this source code is governed by a source available license that can be found in the | ||
| // LICENSE file or at: | ||
| // https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt | ||
|
|
||
| package io.bitdrift.capture.task | ||
|
|
||
| import org.gradle.api.DefaultTask | ||
| import org.gradle.api.tasks.Internal | ||
| import org.gradle.api.tasks.TaskAction | ||
| import org.w3c.dom.Document | ||
| import java.io.File | ||
| import java.io.IOException | ||
| import java.net.URI | ||
| import javax.xml.parsers.DocumentBuilderFactory | ||
| import org.gradle.api.file.Directory | ||
|
|
||
| abstract class CLIUploadMappingTask : CLITask() { | ||
| @TaskAction | ||
| fun action() { | ||
| // e.g. build/intermediates/packaged_manifests/release/processReleaseManifestForPackage/AndroidManifest.xml | ||
| val androidManifestXmlFile = buildDir.asFile.mostRecentSubfileNamed("AndroidManifest.xml") | ||
| // e.g. build/outputs/mapping/release/mapping.txt | ||
| val mappingTxtFile = buildDir.asFile.mostRecentSubfileNamed("mapping.txt") | ||
|
|
||
| val manifest = androidManifestXmlFile.asXmlDocument().documentElement | ||
| val appId = manifest.getAttribute("package") | ||
| val versionCode = manifest.getAttribute("android:versionCode") | ||
| val versionName = manifest.getAttribute("android:versionName") | ||
|
|
||
| runBDCLI(listOf( | ||
| "debug-files", "upload-proguard", | ||
| "--app-id", appId, | ||
| "--app-version", versionName, | ||
| "--version-code", versionCode, | ||
| mappingTxtFile.absolutePath)) | ||
| } | ||
| } | ||
|
|
||
| abstract class CLIUploadSymbolsTask : CLITask() { | ||
| @TaskAction | ||
| fun action() { | ||
| // e.g. build/intermediates/merged_native_libs/release/mergeReleaseNativeLibs | ||
| val nativeLibsDir = buildDir.asFile.mostRecentSubfileMatching(".*merge.*NativeLibs".toRegex()) | ||
| runBDCLI(listOf("debug-files", "upload", nativeLibsDir.absolutePath)) | ||
| } | ||
| } | ||
|
|
||
| abstract class CLITask : DefaultTask() { | ||
| @Internal | ||
| val buildDir: Directory = project.layout.buildDirectory.get() | ||
| @Internal | ||
| val bdcliFile: File = buildDir.dir("bin").file("bd").asFile | ||
| @Internal | ||
| val downloader = BDCLIDownloader(bdcliFile) | ||
|
|
||
| fun runBDCLI(args: List<String>) { | ||
| checkEnvironment() | ||
| downloader.downloadIfNeeded() | ||
| runCommand(listOf(bdcliFile.absolutePath) + args) | ||
| } | ||
|
|
||
| fun runCommand(command: List<String>) { | ||
| val process = ProcessBuilder(command) | ||
| .redirectErrorStream(true) | ||
| .start() | ||
| process.inputStream.transferTo(System.out) | ||
| if (process.waitFor() != 0) { | ||
| throw RuntimeException("Command $command failed") | ||
| } | ||
| } | ||
|
|
||
| private fun checkEnvironment() { | ||
| val apiKeyEnvName = "API_KEY" | ||
| if(System.getenv(apiKeyEnvName) == null) { | ||
| throw IllegalStateException("Environment variable $apiKeyEnvName must be set to your Bitdrift API key before running this task") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class BDCLIDownloader(val executableFilePath: File) { | ||
| val bdcliVersion = "0.1.33-rc.1" | ||
| val bdcliDownloadLoc: URI = URI.create("https://dl.bitdrift.io/bd-cli/${bdcliVersion}/${downloadFilename()}/bd") | ||
|
|
||
| private enum class OSType { | ||
| MacIntel, | ||
| MacArm, | ||
| LinuxIntel, | ||
| } | ||
|
|
||
| private fun osType(): OSType { | ||
| val osName = System.getProperty("os.name") | ||
| val arch = System.getProperty("os.arch") | ||
| return when(osName) { | ||
| "Mac OS X" -> when(arch) { | ||
| "aarch64" -> OSType.MacArm | ||
| else -> OSType.MacIntel | ||
| } | ||
| "Linux" -> OSType.LinuxIntel | ||
| else -> throw IllegalStateException("Could not determine running system (got $osName, $arch). Only Mac (Intel, Arm) and linux (Intel) are currently supported") | ||
| } | ||
| } | ||
|
|
||
| private fun downloadFilename(): String { | ||
| return when(osType()) { | ||
| OSType.MacArm -> "bd-cli-mac-arm64.tar.gz" | ||
| OSType.MacIntel -> "bd-cli-mac-x86_64.tar.gz" | ||
| OSType.LinuxIntel -> "bd-cli-linux-x86_64.tar.gz" | ||
| } | ||
| } | ||
|
|
||
| fun downloadIfNeeded() { | ||
| if(executableFilePath.exists()) { | ||
| return | ||
| } | ||
| val parentDir = executableFilePath.parentFile | ||
| if(!parentDir.exists() && !parentDir.mkdirs()) { | ||
| throw IOException("Could not create path '${parentDir.absolutePath}' to contain the downloaded binary") | ||
| } | ||
| try { | ||
| executableFilePath.writeBytes(bdcliDownloadLoc.toURL().readBytes()) | ||
| } catch(e: Exception) { | ||
| throw IOException("Failed to download bd cli tool from $bdcliDownloadLoc", e) | ||
| } | ||
| if(!executableFilePath.setExecutable(true)) { | ||
| throw IOException("Could not mark ${executableFilePath.absolutePath} as executable") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun File.asXmlDocument(): Document { | ||
| try { | ||
| return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(this) | ||
| } catch(e: Exception) { | ||
| throw IOException("Could not parse XML file $this", e) | ||
| } | ||
| } | ||
|
|
||
| fun File.mostRecentSubfileNamed(name: String): File { | ||
| try { | ||
| return this.subfilesNamed(name).mostRecent() | ||
| } catch(e: Exception) { | ||
| throw IOException("Could not find any file named '${name}' in path or subpath of '${this}", e) | ||
| } | ||
| } | ||
|
|
||
| fun File.subfilesNamed(name: String): Sequence<File> { | ||
| return this.walkTopDown().filter { it.name == name } | ||
| } | ||
|
|
||
| fun File.mostRecentSubfileMatching(regex: Regex): File { | ||
| try { | ||
| return this.subfilesMatching(regex).mostRecent() | ||
| } catch(e: Exception) { | ||
| throw IOException("Could not find any file matching regex '${regex}' in path or subpath of '${this}", e) | ||
| } | ||
| } | ||
|
|
||
| fun File.subfilesMatching(regex: Regex): Sequence<File> { | ||
| return this.walkTopDown().filter { regex.matches(it.name) } | ||
| } | ||
|
|
||
| fun Sequence<File>.mostRecent(): File { | ||
| return this.sortedBy { it.lastModified() }.last() | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.