How to set gradle properties for all my android projects? - android

How to set gradle properties for all my android projects?
I have diffrent android projetcs but whenever i change my gradel.properties file in project folder it only affect that projects.
And i cannot find the gradle.properties in .gradle folder in root directory.
How do i set common properties for all projects so i dont need to change everytime.

Gradle supports system-wide properties in a gradle.properties file in the GRADLE_USER_HOME directory, which defaults to a folder called .gradle in your home directory, e.g. C:\Users\<user>\.gradle on Windows.
These system specific properties may override project specific properties, so they can be used to specify usernames and passwords only for your local machine.
This global gradle.properties may not exist by default, but even if not, you can create one and it will be used and the properties will be available in your build script.
If your global settings require more logic than just plain properties, you can use initialization scripts, which can be placed in the same location (or the init.d subdirectory).

For MacOS users:
Open terminal and enter cd ~/.gradle
Do a ls to check whether gradle.properties already exists or not
If it doesn't exist, create one: vi gradle.properties.
If it exists, just modify the configs you want to modify and save it.

Here is the answer
This global gradle.properties may not exist by default, but even if not, you can create one and it will be used and the properties will be available in your build script.
Create one in C:\Users\user-name.gradle and name it gradle.properties without any extensions.

Documentation :
Project properties are inherited from parent to child projects.
Root Project
ext{
supportLibVersion = '25.3.1'
//supportLib
supportLib = "com.android.support:support-v4:$supportLibVersion"
}
Child Projects:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//supportLib
compile rootProject.ext.supportLib
}

According to Gradle documentation you can set a project property through an environment variable. All you need to do is to prepend the environment variable with ORG_GRADLE_PROJECT and Android Studio loads it as a project property for all the Android (and Gradle) projects. Example:
echo 'export ORG_GRADLE_PROJECT_foo=bar' >> ~/.zshenv
And you can access this as project property in build.gradle files:
def fooProperty = project.foo
println("Foo property is $fooProperty") // prints bar
Note: Make sure you restart Android Studio after adding the environment variable so that the property is correctly loaded.

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

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.

How do I set an environment variable in Android Studio before it runs my gradle build

My Gradle build looks at an environment variable called BUILD_NUMBER to determine the version to allocate to my android application as follows:
def buildNumber = System.getenv("BUILD_NUMBER") ?: "local"
So as long as that environment variable is set, the build number is used in defaultConfig as follows:
versionName "1.4.0."+buildNumber
Usually, Jenkins will call this Gradle build and supply the BUILD_NUMBER environment variable.
If I run the Gradle build from my command prompt, I know I can set BUILD_NUMBER = x.
However, if I build using Android Studio, how can I set the BUILD_NUMBER environment variable through Android Studio itself?
One option is to make use of gradle properties that can be overriden by environment variables. You can read about it here.
If the environment variable name looks like ORG_GRADLE_PROJECT_prop=somevalue, then Gradle will set a prop property on your project object, with the value of somevalue.
What this means is that you can
set BUILD_NUMBER=42 in your .properties file (project, or global) as you would usually do,
and in your CI you would name the environment variable ORG_GRADLE_PROJECT_BUILD_NUMBER, overwriting or setting BUILD_NUMBER in your CI build.
Note: Use gradle.properties in your project root directory, and do not modify local.properties.
I ended up using the following in my build.gradle:
def buildNumber = System.getenv("BUILD_NUMBER")
if (buildNumber == null) {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
buildNumber = properties.getProperty('buildNumber')?:"NoBuildNumberFound"
}
Because the local.properties file is not supposed to be committed to your source code repository, each developer manages their own copy.
So if they want to set the buildNumber on their local Android Studio, they simply add the following to their local.properties:
buildNumber=7
So on a local developers machine, the build number will be set to what ever they put into their local.properties file, but on our Jenkins server, it will use the environment variable BUILD_NUMBER
Our Jenkins server sets the BUILD_NUMBER environment variable
These are just environment variables.
If under Windows, go to the OS control panel and type "environment" in the search box at the top right. Then click on "Edit environment variables for your account", highly user friendly and no admin rights required. Restart the Android Studio after the edit.
If under Linux, edit .bashrc in your home folder (vi ~/.bashrc or with some other editor). Environment variables can be set there. source ~/.bashrc (or logout/login) and restart the Android Studio after the edit.

SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable

I recently tried to import sample Android games I downloaded from Google's developer website. After importing them into Android Studio, I'm getting the following error:
Error: SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.
What is this? I want to run the sample programs from Android Studio.
Please follow the below steps:
Go to your react-native Project then go to the android directory
Create a file with the following name:
local.properties
Open the file and paste your Android SDK path like below:
For windows users:
sdk.dir=C:\\Users\\UserName\\AppData\\Local\\Android\\sdk
or (for newer versions of Android Studio / IntelliJ IDEA):
sdk.dir=C\:\\Users\\USERNAME\\AppData\\Local\\Android\\sdk
Where USERNAME your PC user name. Also, make sure the folder is sdk or Sdk.
Example:
sdk.dir=C:\\Users\\USERNAME\\AppData\\Local\\Android\\sdk
or:
sdk.dir=C\:\\Users\\USERNAME\\AppData\\Local\\Android\\Sdk
For Mac users:
sdk.dir = /Users/USERNAME/Library/Android/sdk
Where USERNAME is your OSX username.
For Linux (Ubuntu) users:
sdk.dir = /home/USERNAME/Android/Sdk
Where USERNAME is your linux username (Linux paths are case-sensitive: make sure the case of S in Sdk matches)
In case this doesn't work, add ANDROID_HOME variable in "Environment Variables" as C:\Users\USER\AppData\Local\Android\Sdk
The project might be missing a settings.gradle file. Make sure that file exists from the project you are importing. If not add the settings.gradle file with the following :
include ':app'
Save the file and put it at the top level folder in your project.
You have to just copy your local.properties file to the folder where project is stored and it will work like charm. But remember, it must be placed in the root folder where the project is stored.
Please follow bellow points it work's for me:
Go to your Project -> Android
Create a file local.properties
Open the file
Paste your Android SDK path depending on the operating system:
4.a Windows
sdk.dir = C:/Users/USERNAME/AppData/Local/Android/sdk
4.b Linux or MacOS
sdk.dir = /home/USERNAME/Android/sdk
Replace USERNAME with your user name
Check out in your local.properties file
sdk.dir=C\:\\Users\\USERNAME\\AppData\\Local\\Android\\sdk
properly write this format, and also check / slas using for path
This problem is encountered when you try to import an Android Studio project from the ../app/build.gradle file.
Import the project by selecting the ../build.gradle file located in the root directory of your project.
Here is a work around for the problem when you click "Run App" (green arrow) and get the following in the Edit Configuration dialog:
Error: Please select Android SDK
In Android Studio, do:
From the menu, choose File > Settings.
In the settings dialog, go to Appearance & Behavior > System Settings > Android SDK.
Look at the top for Android SDK Location, and click the Edit button
Hit Next, Next, Finish to accept the defaults
This seems to save away the SDK location - even though nothing has changed - into some internal location. I inspected the .idea and .gradle folders but didn't see what Studio did to change a config file - but now I can run the app.
And to summarize the previous fixes - these are normally OK for a repo without build problems:
local.properties file is copied into the root folder by Studio.
The path in the local.properties file has the correct path to the android SDK - in my case it is sdk.dir=C:\\android\\sdk
(note that this path has a different format - Studio should write this file for you based on the Text Entry field in the Android SDK Settings dialog)
settings.gradle file is present in the repo - and references the application folder (typically :app)
I came across the same issue but a little bit different error message is
SDK location not found. Define location with an ANDROID_SDK_ROOT
environment variable or by setting the sdk.dir path in your project's
local properties file at "xxx"
MAC & ReactNative
Add local.properties
Find your Android SDK location
/Users/yourMacUserName/Library/Android/sdk
Create local.properties under rootProject/android/local.properties.
Add sdk path into it
sdk.dir = /Users/yourMacUserName/Library/Android/sdk
This normally works, but if you are working in a team with other team members, then yourMacUserName is different.
OR
Set ANDROID_SDK_ROOT variable
Edit your ~/.zshrc or ~/.bashrc or ...
Add SDK path:
export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
Open a new terminal tab or source ~/.zshrc
echo $ANDROID_SDK_ROOT to test the print correct SDK path.
Alternatively, you also can add your path
export PATH=${PATH}:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools
to use some useful commands.
Go to your React-native Project -> Android
Create a file local.properties
Open the file
paste your Android SDK path like below
in Windows sdk.dir = C:\\Users\\USERNAME\\AppData\\Local\\Android\\sdk
in macOS sdk.dir = /Users/USERNAME/Library/Android/sdk
in linux sdk.dir = /home/USERNAME/Android/Sdk
Replace USERNAME with your user name
Now, Run the react-native run-android in your terminal
or
Sometimes project might be missing a settings.gradle file.
Make sure that file exists from the project
you are importing.
If not add the settings.gradle file with the following :
include ':app'
Save the file and put it at the top level folder in your project.
If you have this problem when you pull a react-native project, you just need to open the android project with Android Studio. Everything you need will be automatically created.
Open Android Studio
File -> Open
Choose the android folder under your react-native project folder
Wait for AndroidStudio to complete setup
You can now close Android Studio
OR
If you have installed the AndroidStudio command line launcher:
Run this in your react-native root folder
studio android/
Wait for AndroidStudio to complete setup
You can now close Android Studio
In Linux:
If you have already downloaded the android SDK but its not being found.
The problem might be that the file local.properties needs to be inside the same directory as gradle stuff for gradle to find it when building and running adb.
For my react-native project using gradle I needed to put the local.properties file to Myprojectname/android/ folder.
As I had unzipped the SDK to Downloads so I just use that path in the file with a row like this:
sdk.dir=/home/USER/Downloads/android-sdk-linux
I resolved this issue by creating ANDROID_HOME environment variable as follows in windows.
ANDROID_HOME=C:\Users\<user_name>\AppData\Local\Android\sdk
Restart Android Studio it should build project!
If you are trying to run Google android sample code, try to import the entire repository instead of an individual sample.
Here is instructions.html, included with the Google Calendar API sample code.
Import calendar-android-sample project
Select "Import Project..." or File > Import Project...
Select [someDirectory]/google-api-java-client-samples/build.gradle and
click OK.
Note: it will not work if you try to import [someDirectory]/google-api-java-client-samples/calendar-android-sample/build.gradle
Select "Use local gradle distribution" with "Gradle home" of [someDirectory]/gradle-2.2.1 and click OK.
Just Remove .idea folder and import the project again. It's worked for me.
There is not a single reason for this error.
settings.gradle may be missing or the content in it may be wrong.
local.properties may be missing or the sdk path may be wrongly written.
Go to the android folder and create local.properties files and paste your SDK path
In my case I was using linux and putting double quote around the path inside local.properties like
sdk.dir = "/root/Android/Sdk/"
export ANDROID_SDK_ROOT = "/root/Android/Sdk/"
So it should be
sdk.dir = /root/Android/Sdk/
export ANDROID_SDK_ROOT = /root/Android/Sdk/
and .bash_profile
I had this error in flutter so i fixed it by going to android->app->build.gradle
and changing targetSdkVersion from 28 to 29 and compileSdkVersion to 29 from 28 and it worked for me
create a local.properties file in your root directory of your project with the following content
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Tue Oct 24 17:40:53 CEST 2017
sdk.dir=/Users/****/Library/Android/sdk
put these two lines in your .bashrc file and run source ~/.bashrc
export ANDROID_HOME=/Users/$USER/Library/Android/sdk
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
For linux I did this.(For the first line)
export ANDROID_HOME=/home/$USER/Android/Sdk
In my case, I had to close Project and open again. It worked fine.
Like This
Close Project
And again Open Project Again
Follow followings steps :
Create a file under 'android' folder with name 'local.properties'
Add this line in file 'local.properties' as
sdk.dir=/Users/bijendrasingh/Library/Android/sdk
Add here your android sdk path.
There can be two different possibilities :
1). Either you SDK location is incorrect in local.properites file.
2). Or the file is missing, this can happen if you have cloned a project, so just create a local.properites file under Gradle Scripts foler, and then set up the sdk path.
Set up the correct sdk path like this :
sdk.dir=YOUR_PATH_TO_THE_SDK
For mac users the path should be:
/Users/USER_NAME/Library/Android/sdk
For windows users the path should be:
c:\Users\USER_NAME\AppData\Local\Android\Sdk
For Mac/Linux users
You need to add ANDROID_HOME to your path, add the following to your .bashrc || .zshrc || .profile file
# change $HOME to the path where you installed android Sdk
export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/tools
Then run
$ source ~/.bashrc || .zshrc || .profile
Environment variables in bash_profile or bashrc?
Difference between .bashrc and .bash_profile
This solution actually works for me..
go to this pc -> properties -> advanced system settings -> environment variables ->
then in system variable create new variable with name ANDROID_SDK_ROOT and value C:\Users{USERNAME(Replace it with your username}\AppData\Local\Android\Sdk
and make sure that if real android mobile using usb debugging is enabled. (very important)
then close cmd and restart it should work.
the best and the easiest way is to create new Android project move "app" folder from non working project to that newly made one and add the needed dependencies in the gradle of your new project and everything will work perfectly )
I got this error after freshly cloning a repository. I expected local.properties to be generated automatically, but it wasn't. I was able to generate it by re-importing the Gradle project.
File > Re-import Gradle Project
If all else fails, copy the local.properties file to the root of the project directory. Simply.
Anyone using Jenkins, might get it useful
You need to define a global variable name ANDROID_HOME with the value of the path to android sdk.
For mac, it is /Users/YOUR_USER_NAME/Library/Android/sdk
I had a situation where I already had the local.properties file set up but I was still getting this error. Turns out, if your project has a submodule, you have to copy the local.properties into the submodule folder as well.

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.

Categories

Resources