Gradle: classpath dependency not resolved from nested project - android

I have a multi-project build set-up with gradle that will contain multiple android apps and libraries.
Sample of the project structure:
root (Project Root)
| android
| module1 (Android Application)
| module2 (Android Library)
| ios (in the future)
I want to apply certain gradle plugins only to some subprojects. (Such as the android gradle plugin only to the android subproject)
Therefore I added the classpath dependency in the :android -> build.gradle and the plugin declaration to the two android subproject: :android:module1 -> build.gradle -> apply plugin: 'com.android.application'and:android:module2 -> build.gradle -> apply plugin: 'com.android.library'
The problem is that gradle cannot found the android gradle plugin:
Error:(1, 1) A problem occurred evaluating project ':Shoppr:presentation'.
Plugin with id 'com.android.application' not found.
Also it is not a version problem as in some other questions (Gradle Version 3.1; Android Gradle Plugin Version: 2.2.1) because when defining the classpath dependencies in :root -> build.gradle or :android:moduleX -> build.gradle all is working as expected.
:root -> build.gradle
allprojects {
repositories {
mavenCentral()
jcenter()
}
}
:android -> build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.1' <-- Should only be applied for android project
}
}
:android:module1 -> build.gradle
apply plugin: 'com.android.application' --> Plugin with id 'com.android.application' not found.
:android:module2 -> build.gradle
apply plugin: 'com.android.library'

I have seen such folder arrangements especially with google sample projects that include and android folder and also a web folder. What you can try is to import the project using the android folder as the project folder since you may not be using gradle to build Ios and web if you have the two more folders for you project.
So close the project and re-import it using the android folder as the project root, i believe this way, gradle should run fine. My project folder is also like this because i have both web and android project in a single repo but during build with android studio, i use the android folder as the project root for the android.

I had a somewhat similar problem. I went from having an android folder with 3 subtasks to dividing it into 3 folders with subtasks:
| android
| shared
-- generates aar
| device1
-- generates 2 apks
| device2
-- generates 1 apk
| gradle
gradlew.bat
gradlew
Both device1 and device2 want to use shared. So I removed settings.gradle and build.grade from the android folder. device1, device2, and shared have their own build.grade and settings.gradle. I build device1 and device2 separately. In order for them to include shared and build, I put symbolic links to shared, gradlew, gradlew.bat, and the gradle folder from the android folder into device1 and device2. And voila, it works! (I'm on an Ubuntu computer, but should be able to do the same on Windows and Mac.)
The disadvantage is that shared is built each time. In our case, that's not too bad, it's not very big.
android gradle [tag:multi-level projects]

Related

What is the difference of two build.gradles in project root directory and in app folder?

I just noticed there are two build.gradle files in my project. Is it because I used gradle wrapper while importing the project?
In Android Studio what is the difference between build.gradle in project root directory and build.gradle in app folder?
The "app" folder is just one module. When writing more complex apps you may have multiple modules. The build.gradle at the module level relates only to that module whereas the build.gradle at the root relates to all modules in the project.
Example from project level build.gradle:
allprojects {
repositories {
jcenter()
}
}
This is saying all modules should use jcenter repository for resolving dependencies. Now a potentially confusing thing is a terminology clash between gradle and android studio. An android studio "module" is a gradle "project" hence "allprojects".

Two android projects sharing common module in same repository using gradle

We are creating an app (actually 2) that is split up in 2 separate projects but sharing the same GIT repository (2 separate folders in git root). It is one app for handheld and one for another platform. But they should share some code like Utils, API calls etc.
Folder structure looks like this:
-GIT Root
-- Project (Project)
--- App 1 (Android application)
--- App 2 (Android application)
--- Common (Android library)
App1 and App2 should be able to reach code from common but not the other way of course.
Tried to do like above and using Gradle but it doesn't seem to work. I know that sharing the same git repo for both apps may not be the best way to handle this scenario but I'm having no options here.
Do you think that this is possible to do somehow? (Maybe I'm just not understanding modules correctly or something like that)
I'm new to Gradle which makes this even harder..
This is not too hard to do if all three projects are in the same Git repository. Here is the skeleton of how your projects should be setup:
App1 (Android application)
|_ build.gradle
|_ src
App2 (Android application)
|_ build.gradle
|_ src
Common (Android library)
|_ build.gradle
|_ src
settings.gradle
build.gradle
The top-level build.gradle file can have common gradle settings that you will use for all sub-projects
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
mavenCentral()
}
}
Make sure the settings.gradle file includes all your projects:
include ':App1'
include ':App2'
include ':Common'
Setup Common to be a Android library project. In the build.gradle file for Common add the line:
apply plugin: 'com.android.library'
Setup the other two projects to be Android applications, and include the Common project as a dependency.
apply plugin: 'com.android.application'
dependencies {
compile project(':Common')
:
:
}
You should have three git repos for this. App1, App2 and Common.
Make common a library project using gradle.
apply plugin: 'com.android.library'
You can use the android maven plugin to build the aar locally and make it available to each child app. Add classpath 'com.github.dcendents:android-maven-plugin:1.2' AND apply plugin: 'com.github.dcendents.android-maven'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
}
}
apply plugin: 'com.github.dcendents.android-maven'
Define group and version of library app
group = 'com.foo.example'
version = '1.0.0-SNAPSHOT'
Install library app locally using ./gradlew installDebug
Child Apps
Now the parent app can be included in the child app as a dependency. Add dependency to build.gradle.
compile('com.foo.example:library:1.0.0-SNAPSHOT#aar') {transitive = true}
Each time you make a change to the library app you will have to reinstall it. Then you have to resync the gradle files in your children app. Tools -> Android -> Sync Project With Gradle Files

Android Studio library "error: package does not exist"

I have created Android library as Android Studio module. Added as dependency to my root module. While coding I can import any class from library package but while I'm trying run the application I'm getting an error package some.mylibrary.project does not exist.
build.gradle root module
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services:5.+'
compile project(':libraries:mylibrary')
}
android {
compileSdkVersion 17
buildToolsVersion "20.0.0"
lintOptions {
disable 'InvalidPackage'
checkReleaseBuilds false
abortOnError false
}
***
}
build.gradle library module
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'idea'
android {
compileSdkVersion 17
buildToolsVersion "20.0.0"
*****
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
settings.gradle
include ':libraries:mylibrary'
P.S. I have to mention that the project was exported from Eclipse IDE so the project structure is different from default one.
For Android Studio 2.2.2
Yes, in library module, it can't use the apply plugin: com.android.application statement in the module definition, yes, use apply plugin: com.android.library instead. (still in lib module)
But then you have to do the following:
Expose the same SDK versions in Gradle files for both modules.
Right click on your projects "app" module folder and click on -> open module settings
Click on the "dependencies" tab
Click on the + sign to add a new dependency and select "Module Dependency"
Look for the library you need and add it.
Also while naming your lib module avoid capitals.
If you have a library module, it can't use the apply plugin: 'com.android.application' statement in the module definition, or the build will silently fail as you're seeing. use apply plugin: 'com.android.library' instead.
A bug has been filed to request that the build system fail loudly instead of silently when this happens: https://code.google.com/p/android/issues/detail?id=76725
The answer above is somewhat lacking
If you project java add in Kotlin getting get this error
Tools Tab Kotlin and Configure Kotlin
(Select Android with Gradle) after select with Modules
Project build.gradle add
ext.kotlin_version = ‘1.3.21’
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Apps build.gradle
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Referance : https://medium.com/mindorks/enabling-kotlin-support-for-a-current-only-java-android-project-98c75e04384a
This error happens when you change the package name and try to run the code. this occurs because the android studio still has the cache of your package with an old name.
Also, cross-check all the imports as well as an imported package in your code so that no different package name exists. For example, is common this error is referring to another imported file near where the error is occurring. Check previous imports near.
To fix this error you can try to do an 'invalidate caches / Restart' option from the File menu in android studio. Choose “Invalidate and restart option” and close Android Studio.
Another reason for this error, is when one changes the project's path root folder or in any of the modules it depends. In this particular case, to fix this error you need to remove the affected modules, and re-add them again. Next don't forget to do an 'invalidate caches / Restart' option from the File menu in android studio. Choose “Invalidate and restart option” and close Android Studio.
Clean your project from android studio :
“Build -> Clean Project”. This will clear your build folders.
Remove your .gradle directory from the root of your project. It contains some Gradle cache files.
Delete also the .idea directory (make a backup before). It contains some project configuration files.
Restart Android Studio.
Finally
if the error still persists, you need to move the affected files to an external directory of the project's root folder. And on Android Studio, create manually each filename as previous, and copy the code inside from the old file. This will defiantly solve this error.
In build-gradle app, add this row:
implementation project(":your_name_library_here")
If you are facing this issue while using Kotlin and have
kotlin.incremental=true
kapt.incremental.apt=true
in the gradle.properties, then you need to remove this temporarily to fix the build.
After the successful build, you can again add these properties to speed up the build time while using Kotlin.
Reference: https://stackoverflow.com/a/58951797/3948854

Why are there two build.gradle files in an Android Studio project?

After having imported an Eclipse project into Android Studio, I see two build.gradle files:
1 - <PROJECT_ROOT>\build.gradle
2 - <PROJECT_ROOT>\app\build.gradle
The first version is shorter, the second version contains definitions for compileSdkVersion, etc.
What is the purpose behind having two separate files? Are there separate build tasks?
<PROJECT_ROOT>\app\build.gradle is specific for app module.
<PROJECT_ROOT>\build.gradle is a "Top-level build file" where you can add configuration options common to all sub-projects/modules.
If you use another module in your project, as a local library you would have another build.gradle file:
<PROJECT_ROOT>\module\build.gradle
For example in your top level file you can specify these common properties:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
}
}
ext {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
}
In your app\build.gradle
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
From the official documentation:
Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build.gradle file for build settings specific to that module.
Project Build File
<PROJECT_ROOT>\build.gradle or the Project Build File is for the entire project, so it will be used for global project configurations.
A typical Project Build File contains the following:
buildscript which defines:
repositories and
dependencies
Gradle Plugin version
By default, the project-level Gradle file uses buildscript to define the Gradle repositories and dependencies. This allows different projects to use different Gradle versions. Supported repositories include JCenter, Maven Central, or Ivy. This example declares that the build script uses the JCenter repository and a classpath dependency artifact that contains the Android plugin for Gradle version 1.0.1.
Module Build File
<PROJECT_ROOT>\app\build.gradle or the Module Build File is for a specific module so it will be used for specific module level configurations.
A Module Build File contains the following:
android settings
compileSdkVersion
buildToolsVersion
defaultConfig and productFlavors
manifest properties such as applicationId, minSdkVersion, targetSdkVersion, and test information
buildTypes
build properties such as debuggable, ProGuard enabling, debug signing, version name suffix and testinformation
dependencies
You can read the official documentation here:
Projects and modules build settings

Android Studio: Use AndroidAnnotations

So I wanted to try the new Android Studio and imported my eclipse projects (I generated a gradle build file). Worked pretty good.
The only library which does not seem to work is AndroidAnnotations.
I selected the androidannotations-2.7.jar file under File > Settings > Compiler > Annotation Processing.
As production source directory i selected "gen".
But the generated file like MainActivity_ are not generated. What did I wrong?
I had the same issues, followed the instructions for configuring aa with intelliJ, now it works like a charm does.
AA intelliJ config page will point you to this post...
http://www.ashokgelal.com/2012/12/setting-up-intellij-idea-12-with-maven-actionbarsherlock-roboelectric-androidannotations/
...the above post walks you through setting up various libs in intelliJ, scroll towards the bottom for AA.
The main thing I had to do that I did not have to do in eclipse was go to Preferences > Compiler > Annotation Processors and set my Processor Path to something like...
[PATH TO AA JARS]/androidannotations-2.7.jar:[PATH TO AA
JARS]/androidannotations-api-2.7.jar:[PATH TO AA
JARS]/codemodel-2.4.1.jar
This is what works for me:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
configurations {
apt
}
repositories {
mavenRepo url: 'https://oss.sonatype.org/content/repositories/snapshots/'
}
ext.androidAnnotationsVersion = '3.0-SNAPSHOT';
dependencies {
compile 'com.android.support:support-v4:18.0.+'
apt "org.androidannotations:androidannotations:$ext.androidAnnotationsVersion"
compile "org.androidannotations:androidannotations-api:$ext.androidAnnotationsVersion"
}
android {
compileSdkVersion 18
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 18
}
}
android.applicationVariants.all { variant ->
ext.aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
ext.aptOutput.mkdirs()
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.asPath,
'-AandroidManifestFile=' + variant.processResources.manifestFile,
'-s', ext.aptOutput
]
}
After that I need to mark build/sources/apt-generated/debug as source in Android Studio by right clicking it and selecting Mark Directory as > Source Root
If you don't have problems compiling and just after seeing the generated classes in the IDE, then you need to check if target/generated-sources/annotations is checked as Source Folder.
That would be File > Project Structure > Modules > Sources Tab, then look for the folder and tag it as Sources. The folder will turn blue and will be listed on the Source Folder list.
As Android Studio is based on IntelliJ did you try to follow the configuration guideline on AndroidAnnotation's wiki ?
If you're using gradle you should check this page which explains how to configure the AndroidAnnotation's plugin :
buildscript {
repositories {
mavenCentral()
}
def gradleAndroidAnnotationsPluginVersion = '0.3.0'
dependencies {
classpath "net.ealden.gradle.plugins:gradle-androidannotations-plugin:$gradleAndroidAnnotationsPluginVersion"
}
}
apply plugin: 'androidannotations'
apply plugin: 'idea'
androidAnnotationsVersion = '2.2'
I didn't try this new IDE yet. I'll check that soon.
It seems there is a way of making Android Studio work with AndroidAnnotations
http://code.google.com/p/android/issues/detail?id=55764
If you try to use Android Studio with a project running Android Annotations, you may run into a cryptic compiler issue:
incorrectly typed data found for annotation element public abstract int com.googlecode.androidannotations.annotations.EActivity.value() (Found data of type int)
Problem is the R class is not found. Android Studio doesn't place the R.java into the gen directory by default like eclipse. The solution is to go into Project Settings -> Facets -> Select the Android facet for your project -> Compiler tab, and change the "R.java and Manifest.java files" from "Run process-resources Maven task before Make" to "Generated by IDE".

Categories

Resources