I would like to add a gradle task to my Android build under Android Studio. Various on-line documentation shows clearly how to create a task, but not where to place it in a build.gradle file and which one of them.
A simple, complete example would be perfect.
BTW, I'd like to add a pre-build task to prepare some data, and maybe a post-build task to do some verification/validation.
By trial-and-error and input from Martin Zeitler, I successfuly added pre- and post-build tasks to my build.gradle (app module) as shown below:
This works perfectly.
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "MyPackage"
minSdkVersion 11
targetSdkVersion 26
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dependencies {
// My libraries here
}
// MY PREBUILD TASK
task PreBuild {
println "MY PRE-BUILD TASK"
}
// MY POSTBUILD TASK
gradle.buildFinished {
println "MY POST_BUILD TASK"
}
}
I am fairly new to android studio, and I am trying to export a jar out of an Android Studio project for another Unity project.
I followed the instruction given on the link Create An Android Plugin For Unity Using Android Studio
But whenever I'm trying to export its showing "Build Successful" and "External task execution finished 'exportJar'" but I am not able to find any jar in specified folder of gradle file.
I looked into the folders "app/release" its empty and "app/build/intermediates/bundles/release/" contains only another folder named "instant-run".
So I am bit confused whether I am skipping any step or looking on the wrong path.
I m using Android Studio v2.1.2
App.build file
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.example.merchantapp"
minSdkVersion 8
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:22.2.1'
compile files('libs/PGSDK_v1.0.jar')
compile files('libs/classes.jar')
}
task deleteOldJar(type: Delete) {
delete 'release/AndroidPlugin.jar'
}
//task to export contents as jar
task exportJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
///Rename the jar
rename('classes.jar', 'AndroidPlugin.jar')
}
exportJar.dependsOn(deleteOldJar, build)
android {
lintOptions {
abortOnError true
}
}
you should change this line
apply plugin: 'com.android.application'
to
apply plugin: 'com.android.library'
it is mandatory. and then rebuild your project.
cause we are generating library and it should required for build jar file.
reference How to export library to Jar in Android Studio?
How to create JAR file from Android Studio also referencing res/ folder? Below is my graddle. As of now it contains "include('classes.jar')" inside "task exportJar". Is it possible to also add resource folder?
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
useLibrary 'org.apache.http.legacy'
defaultConfig {
minSdkVersion 8
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
//task to delete the old jar
task deleteOldJar(type: Delete){
delete 'release/AndroidPlugin.jar'
}
//task to export contents as jar
task exportJar(type: Copy){
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
//Give whatever name you to give
rename('classes.jar', 'Executable.jar')
}
exportJar.dependsOn(deleteOldJar, build)
To properly include resources, manifest entries, assets, in addition to compiled code, you need to create an android library which exports an AAR file (not a JAR).
You do this with an Android Library. You can create one using Android Studio with File -> New -> Module and select Android Library. You'll notice that the build.gradle it creates has apply plugin: 'com.android.library' which tells gradle that it's going to build an aar instead of an app. Other app projects can then include this aar to merge all the library resources into its own.
I'd like to create an aar file for my library in Android Studio, i would've gone with a jar option but my library has resources.
Any idea how to create an aar file from a library?
If your library is set up as an Android library (i.e. it uses the apply plugin: 'com.android.library' statement in its build.gradle file), it will output an .aar when it's built. It will show up in the build/outputs/aar/ directory in your module's directory.
You can choose the "Android Library" type in File > New Module to create a new Android Library.
If you are still not seeing your aar file, select Build > Rebuild Project.
Retrieve exported .aar file from local builds
If you have a module defined as an android library project you'll get .aar files for all build flavors (debug and release by default) in the build/outputs/aar/ directory of that project.
your-library-project
|- build
|- outputs
|- aar
|- appframework-debug.aar
- appframework-release.aar
If these files don't exist start a build with
gradlew assemble
for macOS users
./gradlew assemble
Library project details
A library project has a build.gradle file containing apply plugin: com.android.library. For reference of this library packaged as an .aar file you'll have to define some properties like package and version.
Example build.gradle file for library (this example includes obfuscation in release):
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "0.1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Reference .aar file in your project
In your app project you can drop this .aar file in the libs folder and update the build.gradle file to reference this library using the below example:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
}
android {
compileSdkVersion 21
buildToolsVersion "21.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 20
versionCode 4
versionName "0.4.0"
applicationId "yourdomain.yourpackage"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
}
}
}
dependencies {
compile 'be.hcpl.android.appframework:appframework:0.1.0#aar'
}
Alternative options for referencing local dependency files in gradle can be found at: http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project
Sharing dependencies using maven
If you need to share these .aar files within your organization check out maven. A nice write up on this topic can be found at: https://web.archive.org/web/20141002122437/http://blog.glassdiary.com/post/67134169807/how-to-share-android-archive-library-aar-across
About the .aar file format
An aar file is just a .zip with an alternative extension and specific content. For details check this link about the aar format.
just like user hcpl said but if you want to not worry about the version of the library you can do this:
dependencies {
compile(name:'mylibrary', ext:'aar')
}
as its kind of annoying to have to update the version everytime. Also it makes the not worrying about the name space easier this way.
To create AAR
while creating follow below steps.
File->New->New Module->Android Library and create.
To generate AAR
Go to gradle at top right pane in android studio follow below steps.
Gradle->Drop down library name -> tasks-> build-> assemble or assemble release
AAR will be generated in build/outputs/aar/
But if we want AAR to get generated in specific folder in project directory with name you want, modify your app level build.gradle like below
defaultConfig {
minSdkVersion 26
targetSdkVersion 28
versionCode System.getenv("BUILD_NUMBER") as Integer ?: 1
versionName "0.0.${versionCode}"
libraryVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "/../../../../release/" + ("your_recommended_name.aar")
}
}
}
Now it will create folder with name "release" in project directory which will be having AAR.
Updated Answer
In Latest releases specific path is not supported.Please add below code in library's build.gradle and rebuild project.After Rebuilding "aar",change project structure from Android to Project->navigate to your library->build->outputs->aar
android {
defaultConfig {
minSdkVersion ..
targetSdkVersion ..
versionCode ...
versionName "1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
libraryVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "${archivesBaseName}_${variant.name}_${defaultConfig.versionName}.aar"
}
}}
To import "aar" into project,check below link.
How to manually include external aar package using Gradle for Android
After following the first and second steps mentioned in the hcpl's answer in the same thread, we added , '*.aar'], dir: 'libs' in the our-android-app-project-based-on-gradle/app/build.gradle file as shown below:
...
dependencies {
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
...
Our gradle version is com.android.tools.build:gradle:3.2.1
btw #aar doesn't have transitive dependency. you need a parameter to turn it on:
Transitive dependencies not resolved for aar library using gradle
Finally got the solution here - https://stackoverflow.com/a/49663101/9640177
implementation files('libs/aar-file.aar')
Edit
I had one more complication - I had set minifyEnabled true for the library module.
[JAR vs AAR]
.jar is generated by apply plugin: 'java-library'
.aar is generated by apply plugin: 'com.android.library'
File -> New -> New Module... -> Android Library
If you have correctly set up for publishing, then you can just run this command to generate aar files.
./gradlew publishReleasePublicationToMavenLocal
This will generate a aar file inside <module-dir>/build/output/aar directory.
Now you can use this library in other local projects also. Add this in the project gradle in which you want to use this aar module
implementation fileTree(dir: '<location-to-library>/build/outputs/aar/', include: ['*.aar', '*.jar'], exclude: [])
Build ---> Build bundle/apk
.aar file will be generated in build/outputs/aar folder.
I'm trying to use the project android-wheel from https://code.google.com/p/android-wheel/
with Android Studio.
I imported them as modules and put the dependency for 'wheel' into the build.gradle of 'wheeldemo'.
when building it with gradle it shows errors, that packages do not exist, even though the imports in the java file don't show any errors:
I'm not sure, what I'm missing here. Might be something obvious, since I'm not to familiar with android programming, yet.
edit: here is the build.gradle of the wheeldemo module:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
applicationId 'kankan.wheel.demo'
minSdkVersion 19
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
}
}
dependencies {
compile project(':wheel')
}
Make sure the build.gradle of "wheel" has correct plugin type. It should be 'android-library' instead of 'com.android.application'.