Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Android - Override standard tasks

Tags:

android

gradle

I'm trying to customize the behavior of my Gradle build to be Android-Wear friendly.

I am bundling manually my wear apk in my handled apk (because i didnt managed to do it automagically).

This means that if I want to build a new version of the handled apk, i have to manually build my wear apk, copy/past the generated wear-apk insinde my res/raw of the handled project then build the new handled apk.

I want all this to be automatized.

So, what I need to do is :

  1. Launch app:assembleRelease from cmd line
  2. Gradle first do a wear:assembleRelease
  3. At the end, Gradle take the apk from wear/output/apk/wear-apk.apk and copy it in app/src/main/res/raw
  4. Then Gradle can procede to do app:assembleRelease

I dont find how to launch a task (wear:assembleRelease) from another task.

Any help is welcome !

like image 336
pdegand59 Avatar asked Sep 25 '14 13:09

pdegand59


People also ask

How do I set default Gradle tasks?

Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.

How do I enable tasks in Gradle?

Run a Gradle task in the Run Anything windowIn the Run Anything window, start typing a name of the task you want to execute. To execute several tasks, enter task names using space to separate each new task. Alternatively, scroll down to the Gradle tasks section and select the task you need.

How do I add a clean task in Gradle?

Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This task is able to remove any output files or directories we have defined for our task. For example we can assign an output file or directory to our task with the outputs property.


1 Answers

I found a solution that may not be optimal but it is working for what I need.

In my handled app, i first have to say that the assembleRelease depends on my wear:assembleRelease:

app/build.gradle

project.afterEvaluate {
    preReleaseBuild.dependsOn(':wear:assembleRelease')
}

preReleaseBuildis one of the very first task of the build but this task is created dynamically, that's why you have to wrap it after the project is evaluated.

Then, in my wear build.gradle, I have to specify the copy at the end of the build:

wear/build.gradle

assembleRelease << {
    println "Copying the Wear APK"
    copy {
        from 'build/outputs/apk'
        into '../app/src/main/assets'
        include '**/wear-release.apk'
    }
}

With only theses modifications, i managed to have the workflow explained in the question.

This could be enhanced because it is only working for the release build but it's a good first step.

Feel free to comment this solution.

like image 173
pdegand59 Avatar answered Sep 30 '22 23:09

pdegand59