I have a problem while building a project on Android using Gradle.
I have a project structure as following:
root
settings.gradle
build.gradle
- Project 1 (android studio "module")
build.gradle
- Project 2 (android studio "module")
build.gradle
If I select the project 1 and compile, it works
If I select the project 2 and compile, it works as well
Now, I would like to have a dependancy from the project 2 on the project 1 to reuse some of my application logic.
Following the doc I try to add in the project 2 build.gradle
dependencies {
compile project(':Project1')
}
but it doesn't work.
My settings.gradle contains:
include ':Project1', ':Project2'
You should apply the plugin android-library in your library project (project1) :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 17
}
Link to the Gradle documentation about making library projects.
If you want to reuse some logic from Project1 you should create a library project, Project3-lib. Move your reusable code to this new project and let Project1 and Project2 depend on it.
This is how you can do it:
settings.gradle
include ':Project3-lib', ':Project1', ':Project2'
In your Project1 and Project2 you add your new lib project
dependencies {
compile project(':Project3-lib')
}
In your top-level gradle.build file (so that you dont need it in all of your project build files)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.6'
}
}
In your new Project3-lib project you have to apply the android-library plugin and not the android plugin
apply plugin: 'android-library'
Related
I downloaded Android Studio project. At the root of the project only one build.gradle with next structure:
apply plugin: 'com.android.library'
android {
}
dependencies {
}
When I run build.gradle script I get the error:
Plugin with id 'com.android.library' not found.
I know that there must be another Top-level gradle file like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
allprojects {
repositories {
jcenter()
}
}
But it is not present.
How can i add this build file? Or maybe another way to fix this problem?
The project was created in the old version of android studio. Perhaps he had worked for another way in old version? I am waiting for help. Thank you.
You have downloaded a single module (library).
You can use it in an Android Studio project building a structure like this.
Just create an empty new project and add your module.
root
|--build.gradle //top level
|--settings.gradle
|--mymodule //your module downloaded
|----build.gradle
In settings.gradle
include ':mymodule'
Just add the top level build.gradle content to the top of the downloaded projects' build.gradle.
The project you downloaded is most likely being imported into another project, and thus does not need a top level build.gradle file as it will inherit it from the project it is imported into.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.library'
android {
}
dependencies {
}
I'm using this library in my Android app. (https://github.com/yazeed44/MultiImagePicker)
Before now, I was adding it to my project this way:
compile 'net.yazeed44.imagepicker:imagepicker:1.3.0'
The problem with importing it that way is, as far as I know, that I can't override any code because I'll lose all the changes after building the project again. (I need to change some code)
For that reason, I have downloaded the source code and I've imported the project as a module with this name: 'imagepicker'
After that, I added this line to my app build.gradle:
compile project(':imagepicker')
and this to my settings.gradle (Android Studio did it)
include ':app', ':imagepicker'
After doing that, I try to run the project and Android studio shows this error:
Gradle 'Project' project refresh failed
Error:Plugin with id 'com.github.dcendents.android-maven' not found.
Since you are using the module locally you have to add in your top-level build.gradle or in the imagepicker/build.gradle same config added in the imagepicker build.gradle project.
buildscript {
repositories {
jcenter()
}
dependencies {
//ADD THESE DEPENDENCIES
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
An alternative can be modify the imagepicker/build.gradle removing the last 2 lines. But you have to test this way.
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
If you check these files you will find the
apply plugin: 'com.github.dcendents.android-maven'
In your case you don't need these files because they are useful only to uplaod in a maven repo the aar file.
I added below code in Project:gradle.build file and its resolved the problem :
allprojects {
repositories {
jcenter()
maven {
url "https://repo.commonsware.com.s3.amazonaws.com"
}
}
}
EDIT
If you still facing after adding above maven dependencies
Change url "https://repo.commonsware.com.s3.amazonaws.com" to url "https://s3.amazonaws.com/repo.commonsware.com".
Recently I have migrated my android project from Eclipse to Android Studio.
Currently my project has the following Gradle script structure:
Top-level gradle build file
Main module (my app) gradle build file
module A (my app) gradle build file
module B (my app) gradle build file
module C (my app) gradle build file
The Content of my main gradle build file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.1'
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven { url "http://dl.bintray.com/populov/maven" }
mavenCentral()
}
}
I would like to make sure crashlytics would be present for the whole project.
I initially tried to add
apply plugin: 'crashlytics'
to the main project gradle file, but I encountered the following error:
Error:(2, 0) Crashlytics was applied to a project without an Android plugin. Please make sure the Crashlytics plugin is applied after the appropriate Android plugin for your project.
I then moved the apply plugin: 'crashlytics' to the main module (my app) gradle build file and the build was successful.
Since I am new to Gradle & Android Studio, I was not sure if I need to it also to the submodules A, B, C to allow crashlytics to capture exceptions generating from those modules. Also I wonder why can't I (or how can I) add the " apply plugin: 'crashlytics' " to the main project gradle file.
Could anyone clarify it for me?
I am trying to migrate my android app from eclipse to Android studio. (0.5.4)
The project has several dependencies. (Sherlock etc)
I exported the app to Gradle and imported it in Android studio and managed to get the project to build successfully.
It appears however that only the dependencies are built.
Adding erronous lines in the app code does not trigger compile errors.
When I view project | packages, the package for my app does not show, Only the external libraries are shown.
My root level build.gradle only contains repositories and dependencies:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
}
I tried manually adding an android section with the intent to make gradle look at the source files but I had problems adding the section:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
}
android {
compileSdkVersion 17
buildToolsVersion "19.0.2"
}
As i now get the following error
Could not find method android() for arguments xxx on root project 'zzz'.
If proceeded to add
apply plugin: 'android'
Just before the android section, but now I get
A problem occurred evaluating root project 'zzz'.
Plugin with id 'android' not found.
Can anyone shed some light as to why i cannot have an Android section at the root level.
If the problem lies somewhere else any help would also be much appreciated.
In case it is needed, here is my settings.gradle
include ':external:PullToRefresh:SmoothProgressBarLib'
include ':external:ActionBarSherlock'
include ':'
include ':external:pulltorefresh-abs'
include ':external:MyAwesomeLibrary'
include ':external:PullToRefresh:pulltorefresh'
include ':external:SherlockNavigationDrawer'
include ':external:sdk:MyAwesomeSDK'
You've included an android block inside a buildscript block in your top-level build file, but this is incorrect. Instead it should be structured like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 17
buildToolsVersion "19.0.2"
}
dependencies {
//Your app dependencies go here
}
All this is assuming that you truly have an Android application module at your project root (meaning that at your project root directory there's a src directory that has Android sources in it). It seems to be that you're trying to set it up this way because you also have this in your settings.gradle file:
include ':'
If that's the case, then rearranging your top-level build file as indicated above should fix it.
If you don't have a module at the project root, then you should restore the top-level build file to its original condition (take out apply plugin and android), take out that include ':' line from settings.gradle, and add an include statement that points to your application module.
In your settings.gradle file, I don't see where you included your main module. You should add:
include ':mainmodule-directory'
Your project should, ideally, have two build.gradle files. One at the root level, one at the module level. In your main module, you specify that the module is an Android module by adding apply plugin: android in the module's build.gradle file. Then you specify that the entire project would need the Android plugin by using the following in the root build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
I have a build.gradle file in my library project, the content of it looks like below:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
dependencies {
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile 'com.android.support:support-v4:18.0.+'
.......
when I do gradle build, it gives error:
Could not find com.actionbarsherlock:actionbarsherlock:4.4.0.
If I change the line "apply plugin" from 'android-library' to 'android', it can compile fine.
So is it a bug with the gradle android plugin? that it cannot find actionbarsherlock correctly in maven repository if it's from a library project?
Make sure you select from Project Settings -> Gradle, "Use local gradle distribution" pointing to you local Gradle copy. Mine is 1.9 and I managed to make it work.