简体   繁体   中英

Android Gradle - how to run task after app installation

I need to copy some files to the Android App folder (data/data/...) after installing the app via Gradle. This has to be done at every debug build before the app gets started and thus I assumed I could find a gradle task that could be executed after the installation (but before the run command).

It works somewhat fine for other tasks when doing something like

tasks.whenTaskAdded { task ->
    if (task.name == 'build') {
         task.dependsOn taskXYZ
    }
}

They then get executed before my build task is executed. However, since I need that to happen after the install process (and before the running of the app) I was hoping for something like this:

tasks.whenTaskAdded { task ->
    if (task.name.startsWith('install')){
       println ">>>>>>>>> " + task.name
       task.finalizedBy taskXYZ
    }

But sadly, this does not work.

In the build output I can see that this executed pretty much at the start.

Executing tasks: [:app:assemblePlayStoreStagingDebug] in project /Users/...

> Configure project :app
###########################
TASK XYZ WAS EXECUTED HERE!
###########################
---> installPlayStoreStagingDebug

> Task :app:preBuild UP-TO-DATE
> Task :app:prePlayStoreStagingDebugBuild
> Task :app:compilePlayStoreStagingDebugAidl UP-TO-DATE
...
> Task :app:stripPlayStoreStagingDebugDebugSymbols UP-TO-DATE
> Task :app:packagePlayStoreStagingDebug
> Task :app:assemblePlayStoreStagingDebug

BUILD SUCCESSFUL in 6s
33 actionable tasks: 6 executed, 27 up-to-date

It looks like the task did not get executed at the expected time. What I need is something like this:

  1. Task Build
  2. Task Install
  3. Task XYZ
  4. Task run App

But how can I achieve this?

Solutions like this one Android run Gradle task after app install Don't seem to work for the install task.

----- EDIT -----

I tried it the other way around and - instead of using gradle tasks - I tried to change the run configuration of AS by adding an "external tool" call in the "before launch" settings.

https://developer.android.com/studio/run/rundebugconfig#definingbefore

However, when selecting for instance a shell script there, it won't get executed (for instance I was creating a file or directory in that script so I could see whether it was executed at all). It doesn't seem to be that error=13 permission problem. So if gradle tasks are not a solution, perhaps anyone knows a solution with this option!

In gradle you can use the mustRunAfter task, this does not create dependencies between the tasks but forces one task to be after a list of other tasks in the build sequence.

An example:

task taskY { 
    mustRunAfter "taskX" 
}

https://gradle.github.io/kotlin-dsl-docs/api/org.gradle.api/-task/must-run-after.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM