简体   繁体   中英

Run library build from app gradle as build.dependsOn libraryBuild task without including as dependency in app gradle file

In my case when I will run this below command

 ./gradlew -q projects

It will return output as below


Root project

Root project 'MyProject'
+--- Project ':app'
\--- Project ':mymodule'

now I want to build(Run/Green Play icon for the app) my ':app' so ':mymodule' should build automatically but without including the ':mymodule' as a dependency in ':app' level build.gradle

something like this in app level build.gradle file

task gradleBuild(type: GradleBuild ) {
    println "gradleBuild task start"
    doFirst{
        tasks=["mymodule:clean"]
        tasks = ["mymodule:build"]
    }
    doLast {
        tasks = ["mymodule:install"]
        println 'gradleBuild task end.'
    }

}

build.dependsOn gradleBuild

But it not working I am getting the error message as below

Task :app:gradleBuild FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Project 'mymodule' not found in project ':MyProject:app'.

In settings.gradle I have this

include ':app' , ':mymodule'

Your requirement of automatically building the library project("mymodule" in your case) without including it in the app module build.gradle file, when we press "Run/green Play " icon can be achieved with below approach.

Generally Run icon will be configured for single configuration. This is not at project scope, it is at module scope. Because of this reason, when you press icon, "mymodule" is not built, whereas if you invoke 1. gradlew build from command line 2. Sync Project 3. click on Make Project

both the modules(app & mymodule) are built.

To achieve your requirement, we have to edit the existing app configuration in Android studio to build the "mymodule" lib project.

  1. Add below task in the app build.gradle
task buildModule(type: GradleBuild) { buildFile = '../mymodule/build.gradle' tasks = ['build'] }
  1. Click on edit configuration in Toolbar

在此处输入图像描述

  1. Click on Plus symbol present under "Before launch section", and select the row with "android" icon.

在此处输入图像描述

  1. After selecting, a prompt appears and in the prompt, enter ":app:buildModule" and press "OK"

在此处输入图像描述

  1. Final UI will be

    在此处输入图像描述

  2. Apply the changes and press the "Run" icon, the way you are trying before. This will build "mymodule" automatically.

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