Change gradle build directory in android studio? - android

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.

Related

How to prevent to delete the build directory when cleaning a build?

I am now using Android Studio 3.1.3 version.
When clicked Build/Clean menu, it delete the build directory.(for example: app/build directory)
By the way, I want to retain the build directory only.
On the other hand, I want to delete the sub-directories and files in build directory.
How to modify the "clean" feature in gradle build?
(Because I am using "soft link" of build directory and the real build directory is located in RAMDISK.)
Have you considered changing the $buildDir on each project rather than using a soft link? See Project.setBuildDir(File)
Eg
ext {
ramDiskRoot = 'z:/foo'
}
allprojects {
buildDir = "$ramDiskRoot/$name/build"
}
If you wanted to keep this logic outside of your buildscript you could use an init script

Android with visual studio 2017 - build.gradle vs build.gradle.template

So I'm trying to build an Android app using Visual Studio. I started with a project created from the 'New Project' wizard ('Basic Application (Android, Gradle)'). I'm having trouble understanding how the build.gradle and build.gradle.template files relate to each other. The build.gradle file doesn't even show in the VS solution explorer until I enable 'show all files'. And when I make changes to build.gradle, they seem to be overwritten (sometimes?) upon build. Should I only edit build.gradle.template? What build step or tool processes this file to create the final build.gradle? Does the same apply to AndroidManifest.xml.template?
And what about gradle-wrapper - is gradle-wrapper.properties the same as gradle.properties? (I had to add the location of my JDK in there to get builds from the command line, outside VS, to work - so I just created my own gradle.properties but now I'm not sure if there are multiple parts doing the same thing, potentially stepping on each other).
The build.grade is generated by Visual Studio, from the build.gradle.template before it actually runs Gradle. This is true for the AndroidManifest.xml.template as well.
You should not edit the .gradle or .xml file itself and preferably not include it in your source control.
As for the wrapper, I can't really answer.

Where to put local settings for Android gradle build

I am not sure where to properly put local properties for a project (a file path in this case) for an Android Studio project built with gradle:
local.properties: That would be what I need, however it is autogenerated and just used for the SDK-location
gradle.properties (Project level): This file is checked into version control for us to guarantee the same JVM settings for all project members for example
gradle.properties (Global): This would be possible, however I don't feel it is the correct place to put project specific settings
So either I overlooked a possibility or I must go the way of ignoring the project level gradle.properties
local.properties: You can put informations about the SDK location but also, your sensitive data like username and password of your repo access and the location of your Key to sign the Apks.
gradle.properties (Project level): It's only specified for the current project (or module) it contains dependencies, plugins, tasks, repositories for that project.
gradle.properties (Global): Can contains same data as gradle.properties (Project level) except adding dependencies. Configuration in this file are taken for all subProjects (modules in that project). this file can be located also in your gradle home directory (userDir/.gradle). If you are working with a Proxy, the best place to configure your proxy is that file.
If you want to put something specific in the local.properties then just over write once its being generated depending on the OS you have. It generates the SDK location by default but you can override like this from command line:
echo 'sdk.dir=/home/jenkins/Android/Sdk' > ./local.properties
Then you can run your gradle to build the project.

Android studio: this project is not gradle based project

I have one project which was initially in eclipse, then i managed somehow to convert it to gradle and everything was working fine in Android Studio.
But then i didn't work some time with the project and after few months when i returned to project i am surprised that Android Studio has problems with this project.
When i selected "Android" from top left dropdown in AS, where you should see the project as Android, i saw no files, then i did something and now i see there one empty folder Java.
If i hit button "sync project with gradle files" AS says message as posted in name of question, but it is gradle based project.. there are gradle files etc.
Do you someone know why is this problem? Why it should stop work like this?
Thank you
Make sure you open the project from its root directory. i.e. the directory which contains the top level build.gradle file.
The missing top level gradle file is what causes this error.
For native Flutter Projects:
Note that this error will also appear if you "sync your project with Gradle files " in the root directory. Gradle works only in your android directory (flutter_name/android).
Configure Gradle version
I got this error because i wanted to change some build values like the Gradle version and the Gradle plugin version (and other build values ex. kotlin):
In your current project: File>Open
Select directory: user/StudioProjects/flutter_name/android
Open directory in a new window
After it is opened it will load some packages (see right bottom), which will take some time.
Under File>Project Structure Project you can set the gradle version and under Modules variables of your build
Now you can sync your project with Gradle files or at least read out the error messages, that come with your configuration
After configuration, you might need to restart your project with File>Invalid Caches/Restart....
That worked for me:
copy the file build.gradle
drop the copied file build.gradle in
your app/project folder .
My project stopped showing all of sudden and already had build.gradle file.
I just reopened it via File > Open.

Android Studio: Android Manifest doesn't exists or has incorrect root tag

I got this error on Android Studio (AS):
How I got it:
I created an AS Project from scratch and everything was fine.
Then a popup showed up, and (if I recall correctly) was saying something like: "Android Framework detected". I pressed "Yes' and some changes happened to the project/module. And after that I got the above error
Edit:
In my case the problem was not Gradle related. It had to do with IntelliJ/Android Studio configuration, an .iml file in particular.
I realized that after reading this informative post here.
On Android Studio v0.8.2 clicking on Sync project with Gradle files button solved my problem.
update
Thanks to the comment of jaumard. If the Sync project with Gradle files it's not visible you have to open the Gradle panel and click sync icon on top the toolbar.
Just call (in any case) File -> Invalidate Caches and Restart....
In my case:
settings.gradle file was empty. I added defualt code:
include ':app'
Then I clicked
Then project start works.
Double check that under project structure / module / Manifest file it points to the correct manifest file in your code and not the one in generated sources.
This is true for both Android Studio and Intelli J. Sometime when you import project from existing sources it prefer the manifest file inside the generates sources directory.
What helped for me was:
delete .gradle/ folder
delete .idea/ folder
delete ****.idea*** file
reopen Android Studio
import from gradle as Android Studio then suggests
I ran into a similar problem. Looks like my .../src directory for whatever reason moved under my .../lib directory. I moved it out of the /lib directory. Now both /lib and /src are at the same level. After a couple of clean rebuilds and restarts of Android studio everything is back to normal. My emulator started up fine.
You might want to check your directory structure. Compare the directory structure with a working project. You might be able to see the difference.
Moving my AndroidManifest.xml to PROJECT_NAME/src/main fixed the issue.
I had the same errormessage and noticed I had 2 MYPROJECTNAME.iml files, but with a different casing of MYPROJECTNAME. I created this situation after checking out from SVN the project into a directory with the casing error.
Just throw away the IML file with the wrong casing, after saving the content of both and use the content that works
I had this problem with a multi-module project when I renamed the app module. In my case to solve the problem I had to manually update the app module name in the project's settings.gradle file (Android Studio didn't update that value)
Happened to me. Found that i had wrongly opened the parent folder of the actual project in Android Studio.
In my case, it was my AndroidManifest.xml file, it was all messed up due to a new library that I added in my build.gradle.
So I took the AndroidManifest.xml from my latest commit on Git and I replaced it the current one, and also my settings.gradle was empty so I added include ':app'.
Hope it helps, and happy coding !
Facing same issue, resolve after adding the missing build.gradle file in root project.

Categories

Resources