Android Studio shows inexplicable warnings for build.gradle - android-studio

I've created an Android library project in Android Studio and prepared the build.gradle to automate deployment to the Maven Central repository, followed the official instructions from Sonatype.
Particularly, I've added metadata according the Metadata Definition and Upload section
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
//...
pom.project {
name 'Example Application'
description 'A application used as an example on how to set up pushing its components to the Central Repository.'
url 'http://www.example.com/example-application'
scm {
connection 'scm:svn:http://foo.googlecode.com/svn/trunk/'
developerConnection 'scm:svn:https://foo.googlecode.com/svn/trunk/'
url 'http://foo.googlecode.com/svn/trunk/'
}
licenses {
license {
name 'The Apache License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'manfred'
name 'Manfred Moser'
email 'manfred#sonatype.com'
}
}
}
}
}
}
Android Studio shows warnings like 'name' cannot be applied to '(java.lang.String)' for the entries
pom.project/name,
pom.project/description,
pom.project/licenses/license/name,
pom.project/organization/name and
pom.project/developers/developer/name.
Running ./gradlew --info clean uploadArchives shows no such warnings. The generated pom.xml contains the defined metadata.
These warnings are somewhat annoying, because Android Studio intercepts every commit that includes the build.gradle to inform me about the existence of warnings.
The question: It there actually a problem with the build.gradle or is there something wrong with Android Studio's interpretation? If it is a problem with the build.gradle, how do I fix it?

Related

How to make Android Studio build fail on lint errors

Is it possible to make Android Studio build fail on lint check errors ?
I have problems with ImageViews when I convert icon from .png to vector drawable .xml
Sometimes I forgot to change
android:src="#drawable/ic_minus"
to
app:srcCompat="#drawable/ic_minus"
and the app crashes on older OS devices.
?
If there's a lint check for that you can change the severity to FATAL and then when building the release version of your APK it should fail.
android {
lintOptions {
fatal 'MY_LINT_CHECK_ID'
}
}
Also you can execute the Gradle lint task which will fail. If you also want warnings to let your build fail you can use this.
android {
lintOptions {
warningsAsErrors true
}
}
You can also use the below code inside android block in module/app level build.gradle file
For build.gradle
android {
applicationVariants.all {
// Example lint task, your verification task can be anything
def lintTask = tasks["lint${name.capitalize()}"]
assembleProvider.get().dependsOn(lintTask/*, detekt*/) // add list of all the tasks which should fail the build
}
}
For build.gradle.kts(Kotlin DSL)
android {
applicationVariants.all {
// Example lint task, your verification task can be anything
val lintTask = tasks["lint${name.capitalize()}"]
assembleProvider.get().dependsOn.addAll(listOf(lintTask/*, tasks["detekt"]*/)) // add list of all the tasks which should fail the build
}
}
Above code makes the build assemble task which run when we run build app or run app, depends on the listed verification tasks and hence fails it when those tasks fails
Make sure your verification tasks(in our case lint task) are set to fail the build when they are run and there are some issues found in them. All verification tasks has their own flags to enable this behaviour.
For lint you can enable the build failure on warning as below(build.gradle.kts for Kotlin DSL)
android {
lint {
isWarningsAsErrors = true
}
}

Trouble Publishing Android Studio Library on jCenter with Bintray

I'm following this tutorial to publish an example Android Studio library on Jcenter:
http://crushingcode.co/publish-your-android-library-via-jcenter/
It seems very clear.
I've created my GitHub repository with this library at this link:
https://github.com/alessandroargentieri/mylibview
I've also Signed in to Bintray.com, and created a new repository which must contain my library (as explained in the tutorial above).
To publish a repository on Bintray I must create an organisation, then you create the repository. So these are my data:
Bintray username: alessandroargentieri
organisation: alexmawashi
repository: https://bintray.com/alexmawashi/my_android_repository
then, in Android Studio, in the gradle file of my library module, I've this data:
apply plugin: 'com.android.library'
ext {
bintrayRepo = 'my_android_repository' //maven
bintrayName = 'mylibview' // Has to be same as your library module name
publishedGroupId = 'mawashi.alex.mylittlelibrary'
libraryName = 'MyLibView'
artifact = 'mylibview' // Has to be same as your library module name
libraryDescription = 'Android Library to use a custom view'
// Your github repo link
siteUrl = 'https://github.com/alessandroargentieri/mylibview'
gitUrl = 'https://github.com/alessandroargentieri/mylibview.git'
githubRepository= 'alessandroargentieri/mylibview'
libraryVersion = '1.0'
developerId = 'alexmawashi'
developerName = 'Alessandro Argentieri'
developerEmail = 'alexmawashi87#gmail.com'
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
...
...
apply from: 'https://raw.githubusercontent.com/nisrulz/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nisrulz/JCenter/master/bintrayv1.gradle'
When I use the terminal and write:
gradlew clean build install bintrayUpload --stacktrace
After a few minutes, I get this error:
What went wrong:
Execution failed for task ':mylittlelibrary:bintrayUpload'.
> Could not create package 'alessandroargentieri/my_android_repository/mylibview': HTTP/1.1 404 Not Found [message:Repo 'my_android_repository' was not found]
What am I doing wrong?
Thanks.
There also might be a problem here:
https://raw.githubusercontent.com/nisrulz/JCenter/master/bintrayv1.gradle
If your repo belong to your organisation then you are going to need the userOrg parameter set.
See https://github.com/bintray/gradle-bintray-plugin#step-4-add-your-bintray-package-information-to-the-bintray-closure step 4
Also see: HTTP/1.1 401 Unauthorized when uploading binary on bintray
For this to work properly, your gradle.properties file needs to have a bintray.user and a bintray.apikey (which is your bintray API key) configured (see the include in https://raw.githubusercontent.com/nisrulz/JCenter/master/bintrayv1.gradle )

Android Studio: How to exclude Icon\r file?

Google Drive automatically generates Icon$'\r' in each synced folder in OSX. I'd like to exclude this file Icon$'r' recursively from compilation in Android Studio.
I tried #1 (din't work):
Adding !/**/Icon$'\r' and !/**/Icon' in the following field:
File -> Other Settings -> Default Settings
Build, Execution, Deployment -> Compiler
Resource patterns:
I tried #2 (didn't work):
Adding the following in build.gradle under module:
sourceSets {
main {
java {
srcDir 'src'
exclude '**/Icon$"\r"'
// exclude '**/Icon'
}
}
}
Note:
I already excluded Icon$'\r' in .gitignore
If Icon$'\r' is both excluded at compilation AND hidden, that'd be the best solution.

gradle.buildStarted not triggered

I am not able to have buildStarted triggered in my gradle build, not sure what I am doing wrong.
I have a root project gradle file like this
version '1.0'
buildscript {
repositories {
maven { url 'http://repo.jfrog.org/artifactory/gradle-plugins' }
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.1.0')
}
}
gradle.buildStarted {
println "buildStart"
}
gradle. buildFinished {
println "buildFinished"
}
apply plugin: 'groovy'
apply plugin: 'maven'
.........
buildFinished works as I am able to see corresponding println but buildStarted never seem to get triggered.
EDIT
Include init.gradle that uses build listener
class MyBuildAdapter extends BuildAdapter {
void buildStarted(Gradle gradle) {
println "buildStarted"
}
}
gradle.addBuildListener new MyBuildAdapter()
The problem is that the buildStarted event occurs before you get a chance to register a callback in a build script. You'll have to use one of the other hooks. For details, see Gradle.addBuildListener in the Javadoc.
I had the same issue in a custom plugin I wrote, and found that only projectsEvaluated callback is the one that gets executed before buildFinished in the BuildListener.
It's been 7 years, and finally Gradle got around to admitting that it didn't make sense to have this method in the first place. It'll be removed in Gradle 7.
https://docs.gradle.org/6.5.1/userguide/upgrading_version_5.html#apis_buildlistener_buildstarted_and_gradle_buildstarted_have_been_deprecated

How to use uploadConfigurationName and buildConfigurationName

In gradle documentation we can read:
For each configuration in your project, Gradle provides the tasks uploadConfigurationName and buildConfigurationName [18].
As I understand I can create build which looks like this (without any plugin because I don't want to use plugins in this project):
configurations {
productSrc
}
// create zip file which will be published
buildProductSrc(type: Copy) << {
// do the job
}
// publish zip which were build by buildProductSrc
uploadProductSrc {
repositories {
ivy {
url "http://ivy.repo/repo"
}
}
}
So if I run gradle buildProductSrc uploadProductSrc it will build zip and piblish it on ivy repository. Do I understand it correctly becouse it doesn't work?
[UPDATE]
According to Peter Niederwieser answer this is a working version of build:
apply plugin: 'base'
configurations {
productSrc
}
// create zip file which will be published
buildProductSrc << { // unable to create specific task, for example 'type: Copy'
// do the job
}
// publish zip which were build by buildProductSrc
uploadProductSrc {
repositories {
ivy {
url "http://ivy.repo/repo"
}
}
}
To get uploadConfigurationName and buildConfigurationName tasks, you'll have to apply the base plugin, or a plugin that in turn applies the base plugin (java, groovy, etc.). Alternatively, you can declare and configure such tasks yourself (but it takes more effort).

Resources