2

I use bugsnag to monitor crashes in my Android app. I also use ProGuard when I compile a release version. I need to upload the mapping.txt file that is generated in build/outputs/mapping/prod/release folder of my project.

When I run this command in (Windows) command line:

curl https://upload.bugsnag.com -F proguard=@C:/mapping.txt -F apiKey=1111111111111111111111 -F versionCode=11111111 -F appId=com.my.package -F versionNumber=1.0.0

The file is uploaded and everything works.

All I need is to add a task to gradle so it uploads the file once its ready. So once I compile the project for release, the mapping.txt file gets generated and once its ready, upload the file using this curl command (which is taken from bugsnag's web site by the way).

I tried many variations. The current one compiles but I do not think the file is being uploaded... I can't see any indication that it actually happened. This is the code I currently use:

task uploadPro(type: Exec) {
    logger.error("inside upload task")
    commandLine 'cmd', '/c', 'curl', 'https://upload.bugsnag.com', '-F','proguard=@build/outputs/mapping/prod/release/mapping.txt', '-F', 'apiKey=1111111111111111111111', '-F', 'versionCode=111111', '-F', 'appId=com.my.package', '-F', 'versionNumber=1.0.0'
    standardOutput = new ByteArrayOutputStream()
    doLast {
        String output = standardOutput.toString()
        logger.info(output);
    }
}

I also tried using this:

def p = ['cmd', '/c', 'curl', 'https://upload.bugsnag.com', '-F', 'proguard=@build/outputs/mapping/prod/release/mapping.txt', '-F', 'apiKey=11111111111111111111111', '-F', 'versionCode=1111111', '-F', 'appId=com.my.package', '-F', 'versionNumber=1.0.0'].execute()

The way I call this task is by using this command:

tasks.getByName("assembleRelease").finalizedBy(uploadPro)

Im really not sure how to do this. Any help is appreciated!! Thank you!

Alon Minski
  • 1,571
  • 2
  • 19
  • 32
  • Maybe just this might be enough: `assembleRelease.finalizedBy uploadPro` – Daniel Zolnai Jan 28 '16 at 16:26
  • Well the uploadPro task does work. I can see the "inside upload task" getting printed in Gradle Console. So Im sure the code is reached. But Im not sure if the way I run the curl command is correct... In Windows cmd, when I run this command and it is successful I see a result text: OK. If I fail I see the error text. How can I get those in gradle? And how can I make sure this is called AFTER the mapping.txt file is generated and not before? – Alon Minski Jan 28 '16 at 16:31

1 Answers1

5

I found a solution to my own question... mostly. Here is the code I use to run the curl command at the end of the build process:

task uploadPro << {
    logger.error("Uploading mapping.txt file to bugsnag")
    def myCommand = "cmd /c curl https://upload.bugsnag.com -F proguard=@path\\to\mappingFile\\mapping.txt -F " +
        "apiKey=111111111111111111111111 -F versionCode=" + versionCodeId + " -F " +
        "appId=com.xxxx.yyy.zzz -F versionNumber=" + versionId
    ProcessBuilder builder = new ProcessBuilder(myCommand.split(' '));
    Process process = builder.start();
    process.waitFor()
    println process.err.text
    println process.text
}

Then I use this to run it at the end of the build process:

gradle.buildFinished {
    tasks.uploadPro.execute()
}

Cant be sure this is best practice but it works.

I would have liked to run the uploadPro task ONLY during a release buildType...but this proves to be difficult as well.

Alon Minski
  • 1,571
  • 2
  • 19
  • 32
  • What if argument have spaces like `Authorization:Bearer ehfh...` ? https://stackoverflow.com/questions/75789954/auth-token-error-in-curl-command-in-gradle – Shabar Mar 22 '23 at 04:16