Setting android version code / name in gradle subproject from root project - android

In my sub-project that builds an android app, i'm attempting to set the version code / name based on variables in my root build.gradle.
sub-project build.gradle:
apply plugin: 'com.android.application'
repositories {
google()
mavenCentral()
jcenter()
}
android {
compileSdkVersion 24
// current atak build tools version"
buildToolsVersion "28.0.3"
defaultConfig {
multiDexEnabled true
versionCode project.commit_head_count
versionName project.full_version
}
lintOptions {
abortOnError false
}
}
// set the build info prior to building
build.dependsOn rootProject.setBuildInfo
root build.gradle:
// gathers git / build information and dumps it to VERSION files to be read by web app and data analysis program
task setBuildInfo() {
doLast {
// read the core version file and store in variable
def coreVersionFile = new File("$projectDir/VERSION.txt")
def coreVersion = coreVersionFile.readLines().get(0)
// get the git hash value (short)
def getShortGitHashCmd = "git rev-parse --short HEAD"
def getShortGitHashProcess = getShortGitHashCmd.execute()
// get the count of commits on this branch at HEAD
def getCommitCountCmd = "git rev-list HEAD --count"
def getCommitCountProcess = getCommitCountCmd.execute()
ext.commit_head_count = getCommitCountProcess.text.trim()
ext.git_hash = getShortGitHashProcess.text.trim()
ext.full_version = "$coreVersion.$ext.commit_head_count"
ext.build_date = new Date().format('yyyy-MM-dd HH:mm:ss')
// assigns the full_version for global use in other task
// https://stackoverflow.com/a/29597784/680268
project.ext.$full_version = ext.full_version
project.ext.$commit_head_count = ext.commit_head_count
def fileContent =
"Short Version:$coreVersion\n" +
"Long Version:$ext.full_version\n" +
"Git hash:$ext.git_hash\n" +
"Commit count:$ext.commit_head_count\n" +
"Build date:$ext.build_date\n"
print fileContent
}
}
// set the build info prior to building
compileJava.dependsOn setBuildInfo
When this executes, the android app sub-project says that it doesn't know what the commit_head_count variable is referring to. I feel like if I could actually get setBuildInfo to run first, it would work, but can't get that to work

Use below code snippet to execute your custom task when some other task executed:
tasks.matching { it.name == 'name of dependent task (i.e build)' }.all { Task task ->
task.dependsOn setBuildInfo
}
This would make all other matching tasks force to be dependent of your custom task.
Or other solution would be making your build info when project gets evaluated.
Put this code below in your root level build.gradle and check the result:
project.afterEvaluate {
print("This line prints every single time")
}

Related

How to change the generated filename for App Bundles with Gradle?

So to change the generated APK filename inside gradle android I could do something like:
applicationVariants.output.all {
outputFileName = "the_file_name_that_i_want.apk"
}
Is there a similar thing for the generated App Bundle file? How can I change the generated App Bundle filename?
You could use something like this:
defaultConfig {
applicationId "com.test.app"
versionCode 1
versionName "1.0"
setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")")
}
As a more generic way to Martin Zeitlers answer the following will listen for added tasks, then insert rename tasks for any bundle* task that gets added.
Just add it to the bottom of your build.gradle file.
Note: It will add more tasks than necessary, but those tasks will be skipped since they don't match any folder. e.g. > Task :app:renameBundleDevelopmentDebugResourcesAab NO-SOURCE
tasks.whenTaskAdded { task ->
if (task.name.startsWith("bundle")) {
def renameTaskName = "rename${task.name.capitalize()}Aab"
def flavor = task.name.substring("bundle".length()).uncapitalize()
tasks.create(renameTaskName, Copy) {
def path = "${buildDir}/outputs/bundle/${flavor}/"
from(path)
include "app.aab"
destinationDir file("${buildDir}/outputs/renamedBundle/")
rename "app.aab", "${flavor}.aab"
}
task.finalizedBy(renameTaskName)
}
}
Solution from #SaXXuM works great! Task is not necessary for renaming artifact. You can call setProperty() directly in the android {} block. I prefer to have in the file name:
app id
module name
version name
version code
date
build type
This is how I use it in my projects:
build.gradle:
apply from: "../utils.gradle"
android {
...
setProperty("archivesBaseName", getArtifactName(defaultConfig))
}
utils.gradle:
ext.getArtifactName = {
defaultConfig ->
def date = new Date().format("yyyyMMdd")
return defaultConfig.applicationId + "-" + project.name + "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + "-" + date
}
The result is:
com.example-app-1.2.0-10200000-20191206-release.aab
It works for both - APK and AAB.
Now I've wrote kind of a Exec template for cross-platform CLI execution, no matter what the commandLine is. My RenameTask can detect Linux & Windows, as well as release & debug.
Property archivesBaseName needs to be defined in defaultConfig:
android {
defaultConfig {
setProperty("archivesBaseName", "SomeApp_" + "1.0.0")
}
}
RenameTask extends Exec performs the renaming (not to be confused with type: Rename):
import javax.inject.Inject
/**
* App Bundle RenameTask
* #author Martin Zeitler
**/
class RenameTask extends Exec {
private String buildType
#Inject RenameTask(String value) {this.setBuildType(value)}
#Input String getBuildType() {return this.buildType}
void setBuildType(String value) {this.buildType = value}
#Override
#TaskAction
void exec() {
def baseName = getProject().getProperty('archivesBaseName')
def basePath = getProject().getProjectDir().getAbsolutePath()
def bundlePath = "${basePath}/build/outputs/bundle/${this.getBuildType()}"
def srcFile = "${bundlePath}/${baseName}-${this.getBuildType()}.aab"
def dstFile = "${bundlePath}/${baseName}.aab"
def os = org.gradle.internal.os.OperatingSystem.current()
if (os.isUnix() || os.isLinux() || os.isMacOsX()) {
commandLine "mv -v ${srcFile} ${dstFile}".split(" ")
} else if (os.isWindows()) {
commandLine "ren ${srcFile} ${dstFile}".split(" ")
} else {
throw new GradleException("Cannot move AAB with ${os.getName()}.")
}
super.exec()
}
}
And it finalizes two other tasks:
// it defines tasks :renameBundleRelease & :renameBundleDebug
task renameBundleRelease(type: RenameTask, constructorArgs: ['release'])
task renameBundleDebug(type: RenameTask, constructorArgs: ['debug'])
// it sets finalizedBy for :bundleRelease & :bundleDebug
tasks.whenTaskAdded { task ->
switch (task.name) {
case 'bundleRelease': task.finalizedBy renameBundleRelease; break
case 'bundleDebug': task.finalizedBy renameBundleDebug; break
}
}
The advance is, that it leaves nothing behind and one can move the files wherever one wants.
Why no one is using existing gradle tasks for this?
There is a gradle task with the type FinalizeBundleTask and it is called as the last step of bundle generation and it is doing two things:
Signing generated AAB package
Move and rename AAB package where was requested
All You need to do is just to change the "output" of this task to any that You want. This task contains a property finalBundleFile - full path to the final AAB package.
I'm using it something like that:
applicationVariants.all {
outputs.all {
// AAB file name that You want. Falvor name also can be accessed here.
val aabPackageName = "$App-v$versionName($versionCode).aab"
// Get final bundle task name for this variant
val bundleFinalizeTaskName = StringBuilder("sign").run {
// Add each flavor dimension for this variant here
productFlavors.forEach {
append(it.name.capitalizeAsciiOnly())
}
// Add build type of this variant
append(buildType.name.capitalizeAsciiOnly())
append("Bundle")
toString()
}
tasks.named(bundleFinalizeTaskName, FinalizeBundleTask::class.java) {
val file = finalBundleFile.asFile.get()
val finalFile = File(file.parentFile, aabPackageName)
finalBundleFile.set(finalFile)
}
}
}
It works perfectly with any flavors, dimensions, and buildTypes. No any additional tasks, works with any path set for output in Toolbar -> Generate signed Bundle, a unique name can be set for any flavor.
I've found a much better option to auto increment your app versioning and auto renaming when you generate an apk / aab. Solution as below (do remember to create "version.properties" file on your root folder:
android {
...
...
Properties versionProps = new Properties()
def versionPropsFile = file("${project.rootDir}/version.properties")
versionProps.load(new FileInputStream(versionPropsFile))
def value = 0
def runTasks = gradle.startParameter.taskNames
if ('assemble' in runTasks || 'assembleRelease' in runTasks) {
value = 1
}
def versionMajor = 1
def versionPatch = versionProps['VERSION_PATCH'].toInteger() + value
def versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
def versionNumber = versionProps['VERSION_NUMBER'].toInteger() + value
versionProps['VERSION_PATCH'] = versionPatch.toString()
versionProps['VERSION_BUILD'] = versionBuild.toString()
versionProps['VERSION_NUMBER'] = versionNumber.toString()
versionProps.store(versionPropsFile.newWriter(), null)
defaultConfig {
applicationId "com.your.applicationname"
versionCode versionNumber
versionName "${versionMajor}.${versionPatch}.${versionBuild}(${versionNumber})"
archivesBaseName = versionName
minSdkVersion 26
targetSdkVersion 29
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.release
setProperty("archivesBaseName","${applicationId}-v${versionName}")
...
}
Credits to this website and this post
Based on Martin Zeitler's answer I did this on Windows:
Please note that on my setup, .aab files are created in release folder and it deletes everything else in that folder as per this bug report.
In my app's module gradle:
apply from: "../utils.gradle"
...
tasks.whenTaskAdded { task ->
switch (task.name) {
case 'bundleRelease':
task.finalizedBy renameBundle
break
}
}
And in utils.gradle:
task renameBundle (type: Exec) {
def baseName = getProperty('archivesBaseName')
def stdout = new ByteArrayOutputStream()
def stderr = new ByteArrayOutputStream()
commandLine "copy.bat", rootProject.getProjectDir().getAbsolutePath() + "\\release\\${baseName}-release.aab", "<MY_AAB_PATH>\\${baseName}.aab", "D:\\Android\\studio\\release"
workingDir = rootProject.getProjectDir().getAbsolutePath()
ignoreExitValue true
standardOutput stdout
errorOutput stderr
doLast {
if (execResult.getExitValue() == 0) {
println ":${project.name}:${name} > ${stdout.toString()}"
} else {
println ":${project.name}:${name} > ${stderr.toString()}"
}
}
}
The copy.bat is created in project's folder and contains this:
COPY %1 %2
RMDIR /Q/S %3
Be careful with 3rd argument to make sure you don't use a folder that's important to you.
EDIT: Why a .BAT for 2 commands you might ask. If you try commandLine "copy", ... on Windows it results in "system does not recognize the command copy". Put anything, like COPY, REN, RENAME, etc, won't work.

Publishing Android Library (aar) to Bintray with chosen flavors

I've juste added some flavors (or productFlavors if you want) to my project.
The fact is that when I publish the library to bintray, all flavors are uploaded (which is great), but I'm unable to use them. The plugin used is the official one here.
The uploaded aar:
androidsdk-0.0.4-fullRelease.aar
androidsdk-0.0.4-fullDebug.aar
androidsdk-0.0.4-lightRelease.aar
androidsdk-0.0.4-lightDebug.aar
As you noted, the fullRelease is named as the classifier, see doc chapter 23.4.1.3.
I am searching for a solution to choose which flavors that I want to upload.
I've already looked at bintray examples (here and here) and this, with also other examples but I'm still stuck.
Here is my current script:
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
buildscript {
repositories {
jcenter()
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 64
versionName "0.0.4"
}
publishNonDefault true
productFlavors {
full {
}
light {
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
fullCompile 'com.squareup.picasso:picasso:2.5.0'
}
version = android.defaultConfig.versionName
uploadArchives {
repositories.mavenDeployer {
pom.project {
packaging 'aar'
}
}
}
////////////////////////////////
// Bintray Upload configuration
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = "MyRepo" // repo name
userOrg = 'hugo'
name = "AndroidSDK" // Package name
websiteUrl = siteUrl
vcsUrl = gitUrl
publish = true
}
}
To import the library I'm currently using this:
compile ('com.example.lib:sdk:0.0.8:fullRelease#aar') {
transitive = true;
}
I faced the same challenge, and here's the best I could make yet:
Using mavenPublications and the gradle maven-publish plugin along the bintray plugin, you can publish any variant to mavenLocal and bintray.
Here's the publish.gradle file I apply at the end of all my project's library modules I want to publish:
def pomConfig = {
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'louiscad'
name 'Louis CAD'
email 'louis.cognault#gmail.com'
}
}
scm {
connection 'https://github.com/LouisCAD/Splitties.git'
developerConnection 'https://github.com/LouisCAD/Splitties.git'
url siteUrl
}
}
def publicationNames = []
publishing.publications {
android.libraryVariants.all { variant ->
if (variant.buildType.name == "debug") return // Prevents publishing debug library
def flavored = !variant.flavorName.isEmpty()
/**
* Translates "_" in flavor names to "-" for artifactIds, because "-" in flavor name is an
* illegal character, but is well used in artifactId names.
*/
def variantArtifactId = flavored ? variant.flavorName.replace('_', '-') : project.name
/**
* If the javadoc destinationDir wasn't changed per flavor, the libraryVariants would
* overwrite the javaDoc as all variants would write in the same directory
* before the last javadoc jar would have been built, which would cause the last javadoc
* jar to include classes from other flavors that it doesn't include.
*
* Yes, tricky.
*
* Note that "${buildDir}/docs/javadoc" is the default javadoc destinationDir.
*/
def javaDocDestDir = file("${buildDir}/docs/javadoc ${flavored ? variantArtifactId : ""}")
/**
* Includes
*/
def sourceDirs = variant.sourceSets.collect {
it.javaDirectories // Also includes kotlin sources if any.
}
def javadoc = task("${variant.name}Javadoc", type: Javadoc) {
description "Generates Javadoc for ${variant.name}."
source = variant.javaCompile.source // Yes, javaCompile is deprecated,
// but I didn't find any working alternative. Please, tweet #Louis_CAD if you find one.
destinationDir = javaDocDestDir
classpath += files(android.getBootClasspath().join(File.pathSeparator))
classpath += files(configurations.compile)
options.links("http://docs.oracle.com/javase/7/docs/api/");
options.links("http://d.android.com/reference/");
exclude '**/BuildConfig.java'
exclude '**/R.java'
failOnError false
}
def javadocJar = task("${variant.name}JavadocJar", type: Jar, dependsOn: javadoc) {
description "Puts Javadoc for ${variant.name} in a jar."
classifier = 'javadoc'
from javadoc.destinationDir
}
def sourcesJar = task("${variant.name}SourcesJar", type: Jar) {
description "Puts sources for ${variant.name} in a jar."
from sourceDirs
classifier = 'sources'
}
def publicationName = "splitties${variant.name.capitalize()}Library"
publicationNames.add(publicationName)
"$publicationName"(MavenPublication) {
artifactId variantArtifactId
group groupId
version libraryVersion
artifact variant.outputs[0].packageLibrary // This is the aar library
artifact sourcesJar
artifact javadocJar
pom {
packaging 'aar'
withXml {
def root = asNode()
root.appendNode("name", 'Splitties')
root.appendNode("url", siteUrl)
root.children().last() + pomConfig
def depsNode = root["dependencies"][0] ?: root.appendNode("dependencies")
def addDep = {
if (it.group == null) return // Avoid empty dependency nodes
def dependencyNode = depsNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.hasProperty('optional') && it.optional) {
dependencyNode.appendNode('optional', 'true')
}
}
// Add deps that everyone has
configurations.compile.allDependencies.each addDep
// Add flavor specific deps
if (flavored) {
configurations["${variant.flavorName}Compile"].allDependencies.each addDep
}
// NOTE: This library doesn't use builtTypes specific dependencies, so no need to add them.
}
}
}
}
}
group = groupId
version = libraryVersion
afterEvaluate {
bintray {
user = bintray_user
key = bintray_api_key
publications = publicationNames
override = true
pkg {
repo = 'splitties'
name = project.name
desc = libraryDesc
websiteUrl = siteUrl
issueTrackerUrl = 'https://github.com/LouisCAD/Splitties/issues'
vcsUrl = gitUrl
licenses = ['Apache-2.0']
labels = ['aar', 'android']
publicDownloadNumbers = true
githubRepo = 'LouisCAD/Splitties'
}
}
}
In order for this to work, I need to have the bintray_user and bintray_api_key properties defined. I personally just have them in my ~/.gradle/gradle.properties file like this:
bintray_user=my_bintray_user_name
bintray_api_key=my_private_bintray_api_key
I also need to define the following ext properties I used in the publish.gradle file in my root project's build.gradle file:
allprojects {
...
ext {
...
// Libraries
groupId = "xyz.louiscad.splitties"
libraryVersion = "1.2.1"
siteUrl = 'https://github.com/LouisCAD/Splitties'
gitUrl = 'https://github.com/LouisCAD/Splitties.git'
}
}
And now, I can finally use it in my android library module, where I have multiple productFlavors. Here's a snippet from a publishable library module's build.gradle file:
plugins {
id "com.jfrog.bintray" version "1.7.3" // Enables publishing to bintray
id "com.github.dcendents.android-maven" version "1.5" // Allows aar in mavenPublications
}
apply plugin: 'com.android.library'
apply plugin: 'maven-publish' // Used for mavenPublications
android {
...
defaultPublishConfig "myLibraryDebug" // Allows using this library in another
// module in this project without publishing to mavenLocal or Bintray.
// Useful for debug purposes, or for your library's sample app.
defaultConfig {
...
versionName libraryVersion
...
}
...
productFlavors {
myLibrary
myLibrary_logged // Here, the "_" will be replaced "-" in artifactId when publishing.
myOtherLibraryFlavor
}
...
}
dependencies {
...
// Timber, a log utility.
myLibrary_loggedCompile "com.jakewharton.timber:timber:${timberVersion}"; // Just an example
}
...
ext {
libraryDesc = "Delegates for kotlin on android that check UI thread"
}
apply from: '../publish.gradle' // Makes this library publishable
When you have all of this setup properly, with the name of your library instead of mine's (which you can use as an example), you can try publishing a version of your flavored library by trying to first publishing to mavenLocal.
To do so, run this command:
myLibrary $ ../gradlew publishToMavenLocal
You can then try adding mavenLocal in your app's repositories (example here) and try adding your library as a dependency (artifactId should be the flavor name, with "_" replaced with "-") and building it.
You can also check with your file explorer (use cmd+shift+G on Mac in Finder to access hidden folder) the directory ~/.m2 and look for your library.
When it's time to publish to bintray/jcenter, you just have to run this command:
myLibrary $ ../gradlew bintrayUpload
Important:
Before you publish your library to mavenLocal, Bintray or another maven repository, you'll usually want to try your library against a sample app which uses the library. This sample app, which should be another module in the same project just need to have the project dependency, which should look like this: compile project(':myLibrary'). However, since your library has multiple productFlavors, you'll want to test all of them. Unfortunately, it's currently impossible to specify which configuration you want to use from your sample app's build.gradle file (unless, you use publishNonDefault true in your library's build.gradle file, which breaks maven and bintray publications), but you can specify the default configuration (i.e. buildVariant) in your library's module as such: defaultPublishConfig "myLibraryDebug" in the android closure. You can see the available build variants for your library in the "Build Variants" tool Windows in Android Studio.
Feel free to explore my library "Splitties" here if you need an example. The flavored module is named concurrency, but I use my script for unflavored library modules too, and I tested it throughly on all the library modules in my project.
You can reach me out if you need help setting it up for you.
The setup:
buildTypes {
debug {
}
release {
}
}
publishNonDefault true
The fix:
defaultPublishConfig 'release'
// Fix for defaultPublishConfig not working as expected
// ref: https://github.com/dcendents/android-maven-gradle-plugin/issues/11
libraryVariants.all { variant ->
if( publishNonDefault && variant.name == defaultPublishConfig ) {
def bundleTask = tasks["bundle${variant.name.capitalize()}"]
artifacts {
archives(bundleTask.archivePath) {
classifier null //necessary to get rid of the suffix in the artifact
builtBy bundleTask
name name.replace('-' + variant.name, '')//necessary to get rid of the suffix from the folder name
}
}
}
}
This fix will still publish all the artifacts, but it will publish a default artifact without the flavour suffix, which is enough to make it all work.
The fix to upload only the default artifact would be this (if the bintray plugin knew what POM filters are):
install {
repositories.mavenInstaller {
/*
POM filters can be used to block artifacts from certain build variants.
However, Bintray does not respect POM filters, therefore this only works for maven deploy plugin.
Also, bintray crashes with named filters, since it always expects a /build/pom/pom-default.xml,
which does not happen with named filters.
*/
filter { artifact, file ->
// this how the default classifier is identified in case the defaultPublishConfig fix is applied
artifact.attributes.classifier == null
}
}
}
I didn't try it so I will delete the answer if it doesn't resolve the issue.
You should post a different artifact for each flavor (or build variant if you prefer).
In this way you will have in jcenter x artifacts, each of them with a pom file.
Something like:
groupId
|--library-full
|----.pom
|----.aar
|--library-light
|----.pom
|----.aar
In your top level file you can define
allprojects {
repositories {
jcenter()
}
project.ext {
groupId="xxx"
libraryName = ""
......
}
}
Then in your library module:
productFlavors {
full {
project.ext.set("libraryName", "library-full");
}
light {
project.ext.set("libraryName", "library-light");
}
}
bintray {
//...
pkg {
//...Do the same for other variables
name = project.ext.libraryName
}
}
Finally make sure to publish only the release build type (why also the debug version?)
If someone is still stuck with this problem here's what worked for me -
Let's say you want to publish the release build for your flavour1 add this to your build.gradle
android {
...
defaultPublishConfig "flavour1Release"
}
Remove publishNonDefault true if it is present in your gradle file.
Add this inside the bintray block like this
bintray {
...
archivesBaseName = 'YOUR_ARTIFACT_ID'
...
}
Then just run the bintrayUpload task as you would.
The defaultPublishConfig will have to be changed everytime you need to publish a new flavour.
It sounds like you don't want the classifier in the filename. It looks like the classifier is the same as the generated library file name. Have you tried giving them the same filename but outputting them to separate directories?
E.g. in the android scope:
libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.aar')) {
def fileName = "same_name-${version}.aar"
output.outputFile = new File(outputFile.parent+"/${archivesBaseName}", fileName)
}
}
}

Gradle use different tasks / arguments for each build type

I'm in the early stages of migrating an android project to gradle, using the experimental plugin 0.4.0
As part of the build process I have a number of scripts that should run prior to compiling my code / building the apk. The arguments or tasks themselves will be different for a debug or release build.
I'm struggling to find a straight forward way to achieve this.
I've stripped everything back to a simple hello world project while I figure this out.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.4.0'
}
}
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.2"
}
android.buildTypes {
debug {
//
}
release {
//runProguard, sign etc.
}
}
android.productFlavors {
create("free")
{
applicationId = "com.example.app.free"
versionName = "1.0-free"
}
create("full")
{
applicationId = "com.example.app.full"
versionName = "1.0-full"
}
}
}
repositories {
jcenter()
}
dependencies {
compile 'joda-time:joda-time:2.7'
}
task runScript(type: Exec) {
executable "sh"
args "-c", "echo SomeScriptHere"
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn runScript
}
task wrapper(type: Wrapper) {
gradleVersion = '2.5'
}
Ideally I want to differentiate by buildType ( not productFlavor ) as the tasks required as based on the type of build not the variety of app produced.
Is it possible to specify that a task should only run on a release or debug build?
Alternatively is it possible to define different arguments to be used for my runScript task based on being a release or debug build?
Apologies if I'm missing something obvious, I'm pretty new to using gradle.
using onlyIf() would be one option gradle docs here but having to define properties seemed awkward especially as a project gets larger and more complicated.
ZXStudio has a good blog post / example here where instead of using properties or rules will iterate over the existing tasks and create a new task based on the buildType / flavor.
So for my original question the answer mean removing the runScript task above and replacing tasks.withType(JavaCompile) as follows;
Could also be extended to match build flavors and create tasks appropriately.
tasks.withType(JavaCompile) {
def newTaskName = "runScript_" + name;
def isDebug = false;
if( name.contains("Debug") )
{
isDebug = true;
}
//Create a new task
tasks.create( newTaskName, Exec ) {
if( isDebug )
{
executable "sh"
args "-c", "echo this is a DEBUG task"
}
else
{
executable "sh"
args "-c", "echo this is a RELEASE task"
}
}
dependsOn newTaskName
}

Set Android versionCode after Gradle task completes

I have a task that updates my app's version code, called changeVersionCode. This task runs before my Android build tasks (assembleRelease), but obviously it happens after the android { } closure. This seems to mean that versionCode is set and cannot be changed even when changeVersionCode runs.
Here's a simplified build script that demonstrates how I have tried to approach this problem:
// .version is a file that starts at "1" the first time I call this
def loadVersionCode() {
// fetch version code from a file, '.version'
return loadVersionCodeFromFile('.version')
}
def incrementVersionCode() {
// fetch version code, update it, and save it back to a file
def newVersion = loadVersionCode() + 1
saveVersionCodeToFile('.version', newVersion)
}
apply plugin: 'com.android.application'
android {
// ... snip ...
defaultConfig {
// Set the version code to contents of .version (eg. 1)
versionCode loadVersionCode()
// ...
}
}
task incrementVersionCode << {
println "Old version code: " + android.defaultConfig.versionCode // prints 1
incrementVersionCode()
def newVersion = loadVersion() // this now returns 2
android.defaultConfig.versionCode = loadVersionCode()
// Also tried:
// android.defaultConfig.versionCode loadVersionCode()
println "New version code: " + android.defaultConfig.versionCode // prints 2
// android.defaultConfig.versionCode is now 2, but APK still has version 1 (until next time I run gradle)
}
Then:
# Build an APK with versionCode 1
$ ./gradlew assembleRelease
# This seems to change versionCode to 2, but still builds an APK with versionCode 1
#
# Prints:
# Old version code: 1
# New version code: 2
$ ./gradlew incrementVersionCode assembleRelease
I am using:
Gradle 2.5
Groovy 2.3.10
Ant 1.9.3
Java 1.8.0_45
Mac OS X 10.10.5
Android build tools 22.0.1
Is there any way I can change my version code from a task before invoking Android build tasks?
How to configure versionCode before a task is launched
You can use the DSL tasks.whenTaskAdded. You can read the official doc, chapter 58.6.2. Task creation.
You can receive a notification immediately after a task is added to a project. This can be used to set some default values or add behaviour before the task is made available in the build file.
You can define a task:
task incrementVersionCode << {
//do something
}
Then define the dependency :
tasks.whenTaskAdded { task ->
if (task.name == 'xxxxx') {
task.dependsOn incrementVersionCode
}
}
In your case you can do somenthing like this:
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig' || task.name == 'generateDebugBuildConfig') {
task.dependsOn 'increaseVersionCode'
}
}
How to configure versionCode with a function
In the top-level file you can configure a function like this:
ext {
buildVersionCode = {
//...
}
}
In your module/build.gradle you can do somehing like this:
defaultConfig {
versionCode buildVersionCode()
//....
}
Otherwise you can do in your build.gradle something like:
defaultConfig {
//...
versionCode getMyNumber()
}
def getMyNumber() {
return //.... ;
}

FindBugs Android Gradle No classes configured error

I am trying to use the FindBugs plugin for Gradle with an Android build.
The build.gradle file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
apply plugin: 'findbugs'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
}
But when I execute the check task it says
No classes configured for FindBugs analysis.
How do I configure classes for FindBugs analysis?
This is not possible at the moment as findbugs expect Gradle's normal Java SourceSets but the android plugin uses custom ones.
There's work planned in both Gradle and in the Android plugin to allow using the default SourceSets which will enable FindBugs.
You track this issue here: https://code.google.com/p/android/issues/detail?id=55839
In newer versions of Android Studio this problem could be attributed to the fact that the location of the classes directory has changed.
The new location (as of 0.8.2) is:
build/intermediates/classes/{build_variant_name}/
for example
build/intermediates/classes/debug/
task findbugs(type: FindBugs) {
excludeFilter = file('config/findbugs/findbugs.xml')
ignoreFailures = true
classes = fileTree('build/intermediates/classes/preproduction/')
source = fileTree('src/main/java/')
classpath = files()
effort = 'max'
reports {
xml {
destination "reports/findbugs.xml"
}
}
}
This is definitely possible. At least now.
The answer can be seen on https://gist.github.com/rciovati/8461832 at the bottom.
apply plugin: 'findbugs'
// android configuration
findbugs {
sourceSets = []
ignoreFailures = true
}
task findbugs(type: FindBugs, dependsOn: assembleDebug) {
description 'Run findbugs'
group 'verification'
classes = fileTree('build/intermediates/classes/debug/')
source = fileTree('src/main/java')
classpath = files()
effort = 'max'
excludeFilter = file("../config/findbugs/exclude.xml")
reports {
xml.enabled = true
html.enabled = false
}
}
check.doLast {
project.tasks.getByName("findbugs").execute()
}
The important part here is dependsOn: assembleDebug.
Without that you will get a No classes configured for FindBugs analysis error message.
Refer to this https://stackoverflow.com/a/7743935/1159930 for the exclude file.
I was able to solving the problem
by adding find bug as separate task
task findbugs(type: FindBugs) {
ignoreFailures = true
classes = fileTree('build/classes/debug/')
source = fileTree('src/main/java/')
classpath = files()
effort = 'max'
}
this task can run using
gradle findbugs
If you are using android-test plugin you have to exclude findbugsTestDebug task when build.
gradle build -x findbugsTestDebug
I have to specify the default sourceSet for findbugs. Initially it wasnt there so I was getting the error.
findbugs {
sourceSets = [] //Default sourceSet
toolVersion = "3.0.1"
ignoreFailures = true
reportsDir = file("$project.buildDir/findbugsReports")
effort = "max"
reportLevel = "low"
}
As other people wrote, you have to set sourceSets, i.e.:
task findbugs(type: FindBugs) {
// ignoreFailures = false
ignoreFailures = true
effort = 'max'
reportLevel = 'low'
excludeFilter = file("quality/findbugs/findbugs-filter.xml")
classes = files("${project.buildDir}/intermediates/javac/debug/classes",
source "${file(getProjectDir().getPath()).getAbsolutePath()}/src"
include '**/*.java'
exclude "${project.buildDir}/generated/**/*.java"
reports {
xml.enabled = true
xml {
destination file("findbugs/report.xml")
}
/*
html.enabled = true
html {
destination file("findbugs/report.html")
}
*/
/*
text.enabled = true
text {
destination file("findbugs/report.txt")
}
*/
}
classpath = files()
}
The problem is that when you upgrade version of Android Gradle Plugin, this path changes now and then.
In our project in different times it was of following values:
"${project.buildDir}/intermediates/classes/debug"
"${project.buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes"
"${project.buildDir}/intermediates/javac/debug/classes"
So if none of mentioned above values worked out, try to find actual classes in your build tree, maybe they just changed it again.

Categories

Resources