Please follow below steps to get it working ( I have tested it up to Android Studio 2.2)
Lets say you have kept aar file in libs folder. ( assume file name is cards.aar
)
then in app build.gradle
specify following and click sync project with Gradle files.
Open Project level build.gradle and add flatDir{dirs 'libs'}
like did below
allprojects {
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
}
and now open app level build.grdle file and add .aar
file
dependencies {
implementation(name:'cards', ext:'aar')
}
If everything goes well you will see library entry is made in build -> exploded-aar
Also note that if you are importing a .aar file from another project that has dependencies you'll need to include these in your build.gradle, too.
Short Answer
Gradle is a build system.
Long Answer
Before Android Studio you were using Eclipse for your development purposes, and, chances are, you didn't know how to build your Android APK without Eclipse.
You can do this on the command line, but you have to learn what each tool (dx and AAPT) does in the SDK.
Eclipse saved us all from these low-level, but important, fundamental details by giving us their own build system.
Now, have you ever wondered why the res
folder is in the same directory as your src
folder?
This is where the build system enters the picture. The build system automatically takes all the source files (.java
or .xml
), then applies the appropriate tool (e.g., takes .java
class files and converts them to .dex
files), and groups all of them into one compressed file - our beloved APK.
This build system uses some conventions: an example of one is to specify the directory containing the source files (in Eclipse it is \src
folder) or resources files (in Eclipse it is \res
folder).
Now, in order to automate all these tasks, there has to be a script; you can write your own build system using shell scripting in Linux or batch files syntax in Windows. Got it?
Gradle is another build system that takes the best features from other build systems and combines them into one. It is improved based off of their shortcomings. It is a JVM-based build system. That means you can write your own script in Java, which Android Studio makes use of.
One cool thing about Gradle is that it is a plugin-based system. This means if you have your own programming language and you want to automate the task of building some package (output like a JAR file for Java) from sources, then you can write a complete plugin in Java or Groovy (or Kotlin, see here), and distribute it to the rest of the world.
Why did Google use it?
Google saw one of the most advanced build systems on the market and realized that you could write scripts of your own with little-to-no learning curve, and without learning Groovy or any other new language. So they wrote the Android plugin for Gradle.
You must have seen build.gradle
file(s) in your project. That is where you can write scripts to automate your tasks. The code you saw in these files is Groovy code. If you write System.out.println("Hello Gradle!");
then it will print on your console.
What can you do in a build script?
A simple example is that you have to copy some files from one directory to another before the actual build process happens. A Gradle build script can do this.
Best Answer
You can use properties by defining them in a
gradle.properties
file, see gradle documentation. But you'll have to be really careful which names you use for your properties: if you use versionName that property will have the correct value in gradle (you canprintln
it) but it won't end up in your AndroidManifest.xml! So I chose to use 'versName'. (I don't know enough about gradle to understand why this is so...)So in your project's
gradle.properties
you can define the following properties:versCode=3 versName=1.2
And then change the
build.gradle
file into:Now you can override them on the command line like this:
./gradlew -PversCode=4 -PversName=2.1.3 clean build