Building android project with gradle - android

Im trying to build a simple android app using gradle build tools. but im getting an error like this
No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile() is applicable for argument types: (java.lang.String) values: [org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT]
Possible solutions: module(java.lang.Object)
ang here's a simple configuration of build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
compile 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
processResource {
expand (project.properties)
}
task configureDebug << {
jar.classifier = "debug"
}
task configureRelease << {
proguard.enabled = true
}

When applying a plugin you want tell you build script to use it in its classpath. It is not required for compilation so just change the configuration compile to classpath. More more information see 51.5.1. Using your plugin in another project in the Gradle user guide.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
EDIT: At the moment the plugin does not support r20 of the Android SDK. For more information see this issue.

Make sure you are writing the dependency block on your application build.gradle "YourProjectName->yourprojectname->build.gradle" in android studio hierarchy .

Use android gradle tools
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}

Related

Gradle cannot resolve local aar dependency

I wanted to integrate my own library to an Android application.
Before I push it on remote maven repo I want to check the integration with an application locally. So I've pushed it to my mavenLocal().
Maven was installed via brew install maven
And there is my problem - Gradle cannot resolve dependency to my library.
Error I get is just ERROR: Failed to resolve: com.op.rlgen:0.0.1
In library I use maven-publish plugin:
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
groupId 'com.op'
artifactId 'rlgen'
version '0.0.1'
artifact("$buildDir/outputs/aar/rlgen-release.aar")
pom.withXml {
// generating pom logic here
}
}
}
// For `publish` task use mavenLocal
repositories {
mavenLocal()
}
}
to publish a library on my maven I use:
./gradlew :rlgen:publishToMavenLocal
In following path i have 2 files, one with aar extension, the second one with pom:
~/.m2/repository/com/op/rfgen
As I thought the problem is caused because of quite complicated scripts in my main app, I created a sandbox, clear Android Application.
In sandbox's gradle I have:
// project level
allprojects {
repositories {
mavenLocal()
google()
jcenter()
// also tried with
maven {
url "~/.m2/repository"
}
}
}
// app level
dependencies {
compile('com.op.rfgen:0.0.1#aar) {
transitive = true
}
}
I check if mavenLocal points to proper folder and it seems ok:
// in gradle
repositories {
println(mavenLocal().url) // file:/Users/op_user/.m2/repository/
}
And with this configuration I cannot add dependency to that aar because of:
ERROR: Failed to resolve: com.op.rlgen:0.0.1:
Affected Modules: app
Thanks for your help!
The problem was that line:
compile('com.op.rfgen:0.0.1#aar). Should be... compile('com.op:rfgen:0.0.1#aar)... :D

Android Studio not found Libraries

I have been many issues with the new Android Studio release (3.2.1) when I try to build the project.
I'm working with android-sunflower (jetpack integration) project and I'm getting the following error.
Is someone else getting this error?
Plugin [id: 'com.diffplug.gradle.spotless', version: '3.13.0'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.diffplug.gradle.spotless:com.diffplug.gradle.spotless.gradle.plugin:3.13.0')
Searched in the following repositories:
Gradle Central Plugin Repository
Open File
Adapt this for your top level build.gradle file for your project . Add plugins and spotless.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
plugins {
id "com.diffplug.gradle.spotless" version "3.4.0"
}
allprojects {
repositories {
jcenter()
}
buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" }
}
}
apply plugin: 'com.diffplug.gradle.spotless'
spotless {
java {
target "**/*.java"
trimTrailingWhitespace()
removeUnusedImports()
googleJavaFormat()
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Spotless documentary say this for using it:
To use it in your buildscript, just add the Spotless dependency, and
configure it like so:
spotless {
format 'misc', {
target '**/*.gradle', '**/*.md', '**/.gitignore'
trimTrailingWhitespace()
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
endWithNewline()
}
format 'cpp', {
target '**/*.hpp', '**/*.cpp'
replace 'Not enough space after if', 'if(', 'if ('
replaceRegex 'Too much space after if', 'if +\\(', 'if ('
// Everything before the first #include or #pragma will
// be replaced with whatever is in `spotless.license.cpp`
licenseHeaderFile 'spotless.license.cpp', '#'
}
}

In Android, compiling with Gradle, how to share code between projects?

What is the preferred way to share some code (E.g. a Utils class) between two projects when building two apps using Gradle to build?
Can I do this without creating extra jar files? I just want my code to sit outside the app projects, be imported/compiled into both app projects. Or is this simply not possible?
I'm familiar with the approach that uses jars or Android library projects, but both seem a bit unwieldy.
My favorite way of doing this is by keeping it in a local Maven repo. The repo can even live in your SCM so it's the same across workspaces.
Create a new Android Studio project and then set it as a maven project your build.gradle config:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
apply plugin: 'maven'
repositories {
mavenCentral()
}
configurations {
archives {
extendsFrom configurations.default
}
}
group = 'com.mypackage.mylibrary'
version = '1.0.0'
uploadArchives {
configuration = configurations.archives
repositories {
mavenDeployer {
repository(url: uri("relative/path/to/localrepo"))
pom.project {
artifactId 'mylibrary'
name 'My Library'
packaging 'aar'
}
}
}
}
android {
// copy old android config here
}
You'll need to deploy the library before you can use it. Do this by using the uploadArchives task [./gradlew uploadArchives]
Now you should be able to use this library in any project by doing this:
repositories {
maven { url 'relative/path/to/localrepo' }
}
dependencies {
compile ('com.mypackage.mylibrary:1.0.0')
}
When you make changes to your library, you'll have to re-deploy (uploadArchives) with a new version, then update the dependency reference in whatever project needs the new version.

Android Support Library Setup error

I followed instruction from http://developer.android.com/tools/support-library/setup.html to add the support library to my android project, but got an build error like this
Gradle: A problem occurred evaluating project ':projectname'.
No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile() is applicable for argument types: (java.lang.String) values: [com.android.support:appcompat-v7:18.0.+]
Possible solutions: module(java.lang.Object)
Does anyone know the reason for that? Is it a syntax error in gradle? Below is a piece of code in my build.gradle.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
compile 'com.android.support:appcompat-v7:18.0.+'
}
}
This should be like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
// this is for Gradle build system so it finds
// android plugin used below
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:appcompat-v7:18.0.+'
// this is for your application
}
Don't forget to install Android Support Repository in Android SDK Manager.

Maven Dependencies with Android Studio / Gradle

I am using the Gradle build system bundled with Android Studio. So far, I am able to build multi-project setups using dependencies that are stored in my project structure. I would now like to use a maven dependency, to no avail. I have written a very simple build.gradle file that always fails :
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile 'com.google.android:support-v4:r7'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
with the following message :
* What went wrong:
A problem occurred configuring project ':absabs'.
> Failed to notify project evaluation listener.
> Could not resolve all dependencies for configuration ':absabs:compile'.
> Could not find com.google.android:support-v4:r7.
Required by:
absabs:absabs:unspecified
....
Caused by: org.gradle.api.internal.artifacts.ivyservice.ModuleVersionNotFoundException: Could not find com.google.android:support-v4:r7.
It happens with any artifact I have tried so far. Any idea of what's wrong ?
Thanks
The "repositories" block in the buildscript section only applies to the build environment. You also need to specify which repository to use when resolving dependencies for building your project, so you need to put something like the following in your build.gradle file:
repositories {
mavenCentral()
}
Your complete file would look something like this:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.android:support-v4:r7'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
Note the two instances of "repositories". You can read more about what this actually means in the gradle docs.
Seems that current Android Studio version doesn't pick up new dependencies immediately. Try to restart IDE.
Edit:
This is not needed for Android Studio >= 0.1.4v. It has build in action Sync Project with Gradle file. You can find it under Tools > Android > Sync Project with Gradle file or just button in Toolbar.
http://tools.android.com/tech-docs/new-build-system/user-guide
Note: This only affects the code running the build, not the project. The project itself needs to declare its own repositories and dependencies. This will be covered later.
So you have to declare
repositories {
mavenCentral()
}
in your project scope once more.
I did it this way:
In Top build.gradle (Project: YourProject) I added:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.h2database:h2:1.4.187'
//NOTE: you can get the latest version in http://mvnrepository.com/artifact/com.h2database/h2
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
NOTE: I added this along with the predefined jcenter() repositories.
And then for my app/build.gradle file I added whichever library or dependency I needed on:
dependencies {
....//Add dependency here
}
I recently had to use a maven dependency in gradle, maybe this little example will be of use for someone.
In maven:
<dependency>
<groupId>com.turo</groupId>
<artifactId>pushy</artifactId>
<version>0.12.1</version>
</dependency>
In grade that turns into:
repositories {
mavenCentral()
}
dependencies {
compile 'com.turo:pushy:0.12.1'
}
maven
Maven continues using XML as the format to write build specification. However, structure is diametrically different. Maven has its own problems. Dependencies management does not handle well conflicts between different versions of the same library (something Ivy is much better at). XML as the build configuration format is strictly structured and highly standardized. Customization of targets (goals) is hard. Since Maven is focused mostly on dependency management, complex, customized build scripts are actually harder to write in Maven than in gradel.

Categories

Resources