Using custom tasks to build android modules - android

My project on Android Studio has multiple modules(MyApp1,MyApp2 etc.) and i want to create a custom task so that instead of calling ./gradlew :MyApp1:assembleDebug: , i want to call something like ./gradlew releaseMyApp1.
I want to customize that task, so it will clean the projects first, and then edit the sub project's build informations (version name,version number etc.).
First i want to ask if i can i can put all those custom tasks inside the root projects build.gradle file? Secondly how can i call the subproject assembleDebug or assembleRelease tasks from another gradle script? I tried something like this(inside root project build.gradle but it doesnt do anything for me:
task releaseMyApp1{
finalizedBy ':MyApp1:assembleDebug'
// clean the projects, change version name, number etc
}
Thanks for all the help.

Something like this:
task releaseMyApp1(dependsOn: ':MyApp1:assembleDebug')

Related

How to Sync Project with Gradle Files?

change gradle.properties by a script ;
manual Sync Project so the resVal and BuildConfig will refresh.
so, what does the manual Sync Project button do?
how to manual write it in my cmd script
May bellow tow thing will do your work
If you want to sync just project without rebuild then use bellow command
./gradlew --recompile-scripts
And if you want to rebuild your full project
./gradlew build
And Also if you want to see your all task then use bellow commands
./gradlew tasks

How to add source files to a gradle project using an external gradle file

The java classes in my project have dependencies on some files. These files should automatically be imported when I try to sync all gradle files in Android Studio. Right now I have a task in my build.gradle file which runs an external tasks from another build file (compile.gradle). The compile.gradle file will then copy the necessary folders into my main project.
Everything works fine if I call the task manually at first and then syncing the whole project. But as soon as I try to sync the project, without manually calling the task, the external compile.gradle file doesn't get executed and the stack trace shows a "class not found" error.
task thrift(type: GradleBuild) {
buildFile = '../other_project/compile.gradle'
startParameter.projectProperties = ['example.bin.java': example_bin_java,
'example.src': example_src] //Should not be important
tasks = ['clean', 'compile', ...]
}
I already read somewhere that I should not import these files via external tasks because they only get executed in the "execution phase" (Lifecycle: Initialization, Configuration, Execution) and apparently this phase does not get called if I sync or execute the project. However I cannot come up with a solution for this problem. I probably have to use another way to execute the tasks from an external gradle file (without a GradleBuild task).
How do I force/declare the execution of external tasks while syncing all gradle files in Android Studio?
It will probably solve the problem if I copy the external tasks to the build.gradle file. But this is not a soution I am looking for (redundant code...).
You could do this in a couple of different ways. You could create a separate "module" for your external code to be built as a library and make your app have a compile dependency on that module (project in gradle - a lovely discrepancy between gradle and IntelliJ terms.) Or, if you wish to keep the task based structure you have now, you can modify the existing Android specific tasks to be dependent on your new task:
androidDependencies.dependsOn thrift
Now when you perform a sync (or gradlew assemble) the build should pick up the dependencies properly.
I added thrift.execute() to the grade file.
Maybe not the best way to force the task execution but this results
into the wanted behaviour.

Change gradle build directory in android studio?

I just installed Android Studio and I am just learning to build using Gradle. However, with the default project setup, my builds are located in the project directory and I would like to have them placed elsewhere (preferably outside of the project directory). Is it possible to achieve this? Where do I make a change and what change do I make?
in root build.gradle
allprojects {
buildDir = "/path/to/build/${rootProject.name}/${project.name}"
}
See also Gradle global build directory
and docs https://gradle.org/docs/current/userguide/writing_build_scripts.html
You can pass the "buildDir" property to the gradlew.bat (I'd assume you can do this in the Linux version as well but I haven't tested it)
Example:
gradlew.bat assembleRelease -PbuildDir="C:\BuildFolder"
The project iml file has a BUILD_FOLDER_PATH attribute. I haven't tried changing it myself yet, so not sure if it will work. The default value is $MODULE_DIR$/build.
Edit: I did a quick test and this did not work. Once changed, the project needs to reload because the iml file changed. Upon reload it reverts the build directory to default.

Difference between make and build in Android Studio

The Android Studio Build menu has options including
Make Project
Rebuild Project
When should I use each?
Most of the time you should use Make Project. Sometimes, after adding libraries and making big changes to the project you should use Rebuild Project.
If you look at the menu, you'll see that Make Project and Compile have keyboard shortcuts, that suggests that they are often used. Others are seldom used.
It is the same as IntelliJ Idea.
Compile All the source files in the specified scope are compiled. The scope in this case may be a file, a package, etc.
Make Project All the source files in the entire project that have been modified since the last compilation are compiled. Dependent source files, if appropriate, are also compiled. Additionally, the tasks tied to the compilation or make process on modified sources are performed. For example, EJB validation is performed if the corresponding option is enabled on the Validation page.
Make Module Compiled are all the source files that have been modified since the last compilation in the selected module as well as in all the modules it depends on recursively.
Rebuild Project All the source files in the project are recompiled. This may be necessary when the classpath entries have changed, for example, SDKs or libraries being used added, removed or altered
Copied from IntelliJ Idea 13 help.
The difference is that Rebuild executes gradle's clean task first. If you look in the Gradle Console 'Rebuild Project' will say something like
Executing tasks: [clean, :app:compileDebugSources, :app:compileDebugAndroidTestSources]
While 'Make Project' won't have clean
Executing tasks: [:app:compileDebugSources, :app:compileDebugAndroidTestSources]
Difference between make and rebuild is "clean" task.
When you do rebuild project it performs clean too.

In Android Studio, building one project of a multi-project setup with Gradle, can I make it only build one of the projects?

I have a multi-project setup in Gradle and work in Android-studio. The setup contains two apps (each one has its own project directory with its own build.gradle) and some libraries shared by those apps.
The directory structure looks like this:
/workspace/
/workspace/app1/
/workspace/app2/
/workspace/app3/
/workspace/library1/
/workspace/library1/
When I build from the command line I can limit the build to only one of the apps via
gradle assembleDebugApp1
When I build from within Android Studio, it seems to build all projects that have a build.gradle file and are inside of workspace.
In Android Studio, when I run Build->Make Project, I see on the bottom what gradle does:
Gradle build using tasks: [:app1:assembleDebug, :app2:assembleDebug, library1:bundleDebug, (...)].
I would like it to only run the assemble task for my "current project".
I'm new to AndroidStudio, so maybe the question is : How do I set the "current project'?
How do I tell Android Studio to only build what I need for app1?
Edit based on replies:
I do want to run gradle via make so it does give feedback back to the IDE, because I'd like to see the "Make Messages" window:
Open Gradle tasks tab and select task to run. You can select a task from subprojects.
On command line:
gradle :app1:assembleDebug
I'm not sure if there is an "official" way of handling this yet but I got around it by creating a new "Run Configuration" and replacing the default "Build" option in the "Before Launch" section with my own command that calls Gradle with the relevant command line options.

Categories

Resources