Build and install several modules from an Android Studio gradle Task - android

I have a grade project in Android Studio that contains several modules; each module containing an Android application or service. The applications/modules have no dependencies on each other.
Project
- module1
- module2
- etc...
I would like to build and install all of the apps with the push of a button. Is there a way to create a gradle task, for the project, that will compile and install each of modules/apps? If not, what would be the best approach to build and deploy the applications together?

I was able to get all modules/projects to work with the following gradle task in the root project:
task _installEverything{
subprojects { sp ->
println("Building and Installing: " + sp.name)
dependsOn(':' + sp.name + ':installDebug')
}
}
The task 'installDebug' can be used to compile and install each application; this task needs to be called in a Lazy dependsOn because the task does not exist when the root projects build script is compiled. This way, the following tasks will be called:
:app1:installDebug
:app2:installDebug
etc...

Related

Run Ktlint for all modules by executing single task command

I have multiple modules in my android project. I have one Gradle file which having code for ktlint. I applied that Gradle file to every module by writing this in the project Gradle file.
subprojects { subProject ->
apply from: "$project.rootDir/commonFiles/gradleScript/kotlin-code-quality.gradle"
}
now the problem is I can run ktlint on each module by giving their name in command like this.
Running ktlint for cache module.
./gradlew :cache:ktlint
but I need to run all modules ktlint at once.
Here is my open-source repository and pull request on which I need to perform ktlint action.
Sorry, it's my bad. ./gradlew ktlint will always run on multimodule. I had some lint checks fail in the app module. I thought it is running only on the app module.
but when I fixed the app module's lint and ran the ./gradlew ktlint. it jumped to the cache module.
I hope it helps you Thanks.

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.
Add below task in the app build.gradle
task buildModule(type: GradleBuild) {
buildFile = '../mymodule/build.gradle'
tasks = ['build']
}
Click on edit configuration in Toolbar
Click on Plus symbol present under "Before launch section", and select the row with "android" icon.
After selecting, a prompt appears and in the prompt, enter ":app:buildModule" and press "OK"
Final UI will be
Apply the changes and press the "Run" icon, the way you are trying before. This will build "mymodule" automatically.

Android build gradle dependencies

I have an app with 2 libraries as dependencies. I'm trying to build a script that clones those libraries into an empty folder and build the project.
I created a task within the app's build.gradle that clones the repos:
exec { commandLine "git", "clone", repo, repoFolderName }
The script starts and clones the repos, all fine but when it starts building the app if fails with the following error:
"Could not determine the dependencies of task ..."
If I run the script again it will successfully build the project.
From my initial investigation it seems that Gradle creates the dependencies as the 1st thing, before the libraries are locally cloned so the libraries are not there hence the build fails. If I run it again as the libraries are already there it will successfully build the project.
Any help is much appreciated.

Android: Skip Gradle "testClasses" task for a dependency project

I have followed this guide to create a JUnit test file for my main Android module (let's call it "module-a"), in Android Studio v1.4.
My "module-a" has a dependency on an external library that is provided as a .aar file and for which I had to create a dedicated module.
This dependency causes an error:
When right clicking the test Java file and hitting "Run MyTestName" , it fails with this error
Error:Gradle:
FAILURE: Build failed with an exception.
* What went wrong:
Task 'testClasses' not found in project ':module-b'.
Removing the dependency on module-b solves the problem.
Excerpt of module-a build.gradle:
compile project(':module-b')
module-b build.gradle:
configurations.create("default")
artifacts.add("default", file('library-b.aar'))
How should I configure Gradle so that it does not try to run the testClasses task on "module-b" ? (this should solve my issue)
I did not find a way to skip the testClasses task for module-b: it seems that actions started from Android Studio (like running a JUnit test) run Gradle commands that cannot be modified. In my case:
Information:Gradle: Executing tasks:
[:module-a:prepareFree_flavorDebugUnitTestDependencies,
:module-a:generateFree_flavorDebugSources,
:module-a:mockableAndroidJar,
:module-a:assembleFree_flavorDebug,
:module-a:assembleFree_flavorDebugUnitTest,
:module-b:testClasses]
I found a workaround for my problem, though:
Add the following code to module-b build.gradle:
task testClasses {
doLast {
println 'This is a dummy testClasses task'
}
}

Gradle - Which task creates the 'build' directory?

I'm using Android Studio 1.3.2 on Mac.
Gradle version is listed as 2.2.1, Android Plugin version 1.3.1.
I have applied the FindBugs Gradle plugin, and I've created a task that successfully runs the analysis on the directory 'build/intermediates/classes'.
To trigger this task on Gradle Sync, I've added it as a dependency to the preBuild task, like this:
preBuild.dependsOn findBugs
The problem with this dependency is that, at time of preBuild, the generated class files are either non-existent (first sync) or stale (remaining from previous sync). Basically, I want my task to run immediately after the 'build/intermediates/classes' directory is created, or when the files there are refreshed as part of the 'Sync' operation.
Looking at the tasks available, I can see the 'clean' task has the following description:
clean - Deletes the build directory.
However, none of the other tasks I see describe the directory's creation. My first thought was "Well, it's got to be the build task, right?". Unfortunately, as usual, it's not that simple (pressing gradle 'sync' button, does not trigger my task when I've added it as a dependency to the 'build' task). Is no such task available? If so, which task would best suit what I'm trying to achieve?
You can see all tasks using ./gradlew tasks
The build task is the main one. It builds everything, generates APKs and runs all checks.
I recommend you adding the following to the build.gradle file:
check.dependsOn 'findbugs'
So when you run the check task, it will execute findbugs as well.
Then also set a dependency on findBugs for the compilation task:
task findbugs(type: FindBugs, dependsOn: 'compileDebugSources') {...}
This will compile the source in case you run findbugs without previously compiling it.

Categories

Resources