2

So I have a custom_rules.xml file Im trying to rebuild in gradle. so far I have no problem figuring everything else out but the last part Im trying to accomplish is running a special apk signing tool for widevine drm on the unsigned apk that I generate with my gradle build process. Ive been looking online and in the gradle documentation all day but I have yet to find how to run an executable jar file from gradle. I know I have to create a task but thats about it. Any help would be much appreciated.

PS. Ive read a lot of the gradle documentation so please spare me any read the docs responses

EDIT:

this is what my task looks like at the moment and it compiles but Im not sure its actually doing anything

task (runApkSigTool , dependsOn: 'android', type: JavaExec) {

classpath files('apksigtool.jar')
main 'com.widevine.tools.android.apksigtool.ApkSigTool'
args[0] = apkLocation
args[1] = 'private_key.der'
args[2] = 'my.crt'

}

Im not sure if I have to use it like a method in the android section of my build.gradle file or what.

James andresakis
  • 5,335
  • 9
  • 53
  • 88
  • My final solution for my problem is here http://stackoverflow.com/questions/26048396/best-way-to-sign-an-apk-using-ant-in-a-build-gradle-file – James andresakis Sep 29 '14 at 18:33

1 Answers1

5

You could probably use the JavaExec task. Something like this:

task signApk(type: JavaExec) {
    classpath files('path/to/executable.jar')
    main 'com.foo.MainClass'
    args ['-foo', 'bar']
}
Mark Vieira
  • 13,198
  • 4
  • 46
  • 39
  • Thanks for providing an answer. Since my project is an android application what would I use for my main declaration? – James andresakis Sep 23 '14 at 03:34
  • 2
    Main would be the main class in your executable jar. Typically Java would figure this out for you when doing something like `java -jar` by looking at the `Main-Class` header in the jar's manifest. You could simply unpack the jar and look at the manifest to find this information. – Mark Vieira Sep 23 '14 at 03:37
  • Cool thanks :) if I cant make this work maybe Ill try using the command line functions in a task. – James andresakis Sep 23 '14 at 03:37
  • That would be the other option, to simply create an Exec task that called `java -jar`. The benefit of using the JavaExec task is that by default it will use the same JRE being used by Gradle and you don't have to implement any code to find the JRE should it not happen to exist on the path. – Mark Vieira Sep 23 '14 at 03:40
  • I dont think Im using the args[] properly. Its giving me an error saying it cant cast an object '' to an int – James andresakis Sep 23 '14 at 04:03
  • Ive updated my question with my current task. How can I run this task after the buildTypes portion of the build process runs. – James andresakis Sep 23 '14 at 04:26
  • so after a while I still cant run the task correctly. No in the command line I get an error message just the same as I would if I ran the jar from the command line and I left out some parameters. – James andresakis Sep 23 '14 at 05:53
  • Try setting your args using an array literal `args = [apkLocation, 'private_key.der', 'my.crt']`. Also, you can run this task after your artifacts are built by adding `dependsOn assemble` to your task. – Mark Vieira Sep 23 '14 at 14:55
  • When I try using an array literal like you say I get an error when i run the script saying that it cant cast an object to an int. What Im trying to pass in the args are file paths to the files that the executable jar file uses – James andresakis Sep 23 '14 at 16:09
  • Perhaps the file path in `apkLocation` needs to be escaped. Try wrapping it in quotes `'"' + apkLocation + '"'`. Also, you can confirm whether or not the command is being constructed correctly by adding `doFirst { println commandLine }` to your task. – Mark Vieira Sep 23 '14 at 16:26