Android Gradle build with sub projects - android

I am currently in the process of converting one of our projects to Gradle from maven. The folder structure is as follows:
gitRoot
settings.gradle
build.gradle
ProjectA
build.gradle
src/main/java
Libraries
SomeLib (git submodule)
ProjectBRoot (git submodule)
settings.gradle
build.gradle
ProjectB
build.gradle
src/main/java
Libraries
FacebookSDK/facebook
build.gradle
src
So already it looks complicated. But the idea is that ProjectB is a library project and it should be able to be built and packaged separately, which is why it has its own settings.gradle and as far as i can tell it seems to be working ok, i have it building and its finding facebook just fine.
The ProjectB/build.gradle contains this line
compile project(':libraries:facebook-android-sdk:facebook')
The ProjectBRoot/settings.gradle contains this line
include ':ProjectB', ':libraries:facebook-android-sdk:facebook'
The gitRoot/settings.gradle contains this line
include ':ProjectA', ':Libraries:ProjectBRoot:ProjectB'
The ProjectA/build.gradle contains this line
compile project(':Libraries:ProjectBRoot:ProjectB')
When I run the build i get this error
The TaskContainer.add() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the create() method instead.
FAILURE: Build failed with an exception.
* Where:
Build file '/gitRoot/Libraries/ProjectBRoot/ProjectB/build.gradle' line: 17
* What went wrong:
A problem occurred evaluating project ':Libraries:ProjectBRoot:ProjectB'.
> Project with path ':libraries:facebook-android-sdk:facebook' could not be found in project ':Libraries:ProjectBRoot:ProjectB'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.652 secs
So my guess as to whats wrong is that facebook is not in a direct subfolder from ProjectB...but that doesn't matter when building within ProjectBRoot. This is probably due to the face that I am referencing ProjectB directly and not through the ProjectBRoot/gradle.build but I tried that and it also did not work. Can someone please help me out, I have looked through the documentation and it doesn't talk about multiple projects that have their own settings.gradle files and I think thats the thing that is messing me up.
Update:
So I followed Xav's answer and I am now able to build with the command line however i can't import/build with android studio. I know the problem is still with the facebook project. The error i get is that it could not configure ProjectB.
Gradle: A problem occurred configuring project ':ProjectA'.
> Failed to notify project evaluation listener.
> A problem occurred configuring project ':Libraries:ProjectBRoot:ProjectB'.
> Failed to notify project evaluation listener.
> Configuration with name 'default' not found.
The error is caused by the line
compile project(':facebook-sdk')
inside the ProjectB/build.gradle

settings.gradle must define all the modules. It won't load other settings.gradle found in the tree to load more module.
You'll have to either
define a module for the facebook SDK in the top level settings.gradle. Yes it's redundant with the other settings.gradle.
publish Project B somehow (as well as its dependencies so in this case the facebook SDK library), somewhere (a corporate artifact repository for instance) and access it from Project A.
While #1 is better, it makes the ProjectB -> Facebook dependency tricky as the path will be different depending on the settings.gradle used. One way to fix this is to split the module name/path from its actual location on disk. this is done inside the settings.gradle file.
In your top level settings.gradle file, do
include 'facebook-sdk'
project(':facebook-sdk').projectDir = new File('Libraries/ProjectBRoot/Libraries/FacebookSDK/facebook')
In the setting.gradle file inside your Project B, do the same with the different relative path:
include 'facebook-sdk'
project(':facebook-sdk').projectDir = new File('Libraries/FacebookSDK/facebook')
This makes both project setup define the same 'facebook-sdk' module located at the same absolute location on disk.
All projects depending on this module should just declare the dependency as
compile project(':facebook-sdk')

This isn't the answer that you are looking for, but...
I spent about 4 days trying to migrate my complex project structure (much like yours actually), into gradle. In the end, I ditched the entire sub-project concept (since in my case, it just didn't work), and I opted to use the local maven repository approach, as described in this article: http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds
So, in my Library projects that had sub-projects, I basically created complete library projects for each of those, and then I used the link above to publish those to the maven local repository. Then in the parent project, I removed any references to sub projects, and in the dependencies, I just referenced the published libraries. And then in my main project, I removed all references to sub-projects, and I referenced published maven local versions of my libraries.
In the end, it all works very well, but it did take some time to convert everything over. I had about 6 library projects with sub projects, that that sub projects, etc, and now everything works fine.
The thing that I dislike about systems like Gradle and Maven is that they really expect you to change "how" you structure your code/projects. So, if you are migrating to Gradle vs starting with Gradle, then the process can be quite frustrating. Once you figure it out though, the next time is much easier ;)

I would just have the app with the libraries all at the same level. You can still build and package each library based on the build.gradle file. I might be missing something, but the structure isn't as important as what's in the build.gradle files. You could still have projectB depend on Facebook, even if they are at the same folder level.

Had the same problem, in my case, I just forgot to add the project in settings.gradle. After that, it worked

I have finally managed to build the project (without gradle errors). Answer from Xavier was very helpful.
I spend almost 3 days trying to setup my project, I know I am very close to be finsihed but I have UNEXPECTED TOP-LEVEL EXCEPTION at very last step of gradle build process (dexDebug).
My project setup is very similar to Stoyan's however I have multiple android-library projects that are referencing android-support libraries. I suggest that if you have problems with compiling your top root project (error saying support android is already added) than you need to move the jar into separate android library project (decompose/separate into stand alone instance).
Example
--| ProjectARoot
--| ProjectA (where main/java etc are)
--| build.gradle
--| settings.gradle
--| libraries
--| ProjectBRoot
--| settings.gradle
--| ProjectB
--| libraries
--| android-supports (android lib project)
--| libs
--| android-support-v4.jar
--| android-support-v13.jar
--| build.gradle
--| libA
--| build.gradle (referencing android-supports)'
Example build scrip for libA referencing android supports projects
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android-library'
dependencies {
compile project(':android-support')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 17
}
}
// top root settings.gradle
include 'ProjectB', 'android-support', ':ProjectA' (notice Project B is first than android-support and lastly Project A)
project(':android-support').projectDir = new File('libraries/ProjectBRoot/libraries/android-support')
project(':ProjectB').projectDir = new File('libraries/ProjectBRoot/ProjectB')
Currently when I run gradle build I get this error
:ProjectA:dexDebug
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
at com.android.dx.command.dexer.Main.processClass(Main.java:490)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
at com.android.dx.command.dexer.Main.access$400(Main.java:67)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
at com.android.dx.command.dexer.Main.processOne(Main.java:422)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
at com.android.dx.command.dexer.Main.run(Main.java:209)
at com.android.dx.command.dexer.Main.main(Main.java:174)
at com.android.dx.command.Main.main(Main.java:91)
1 error; aborting
:ProjectA:dexDebug FAILED

Related

Import library module that has dependency on local aar file

I have an Android Instant App with following structure:
B: base module
Installed: installed app module
Instant: instant app module
F: feature with functional specific to Installed app. F depends on local aar library local-lib located in project\F\libs.
F's build.gradle is following:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
api ":local-lib#aar"
}
I tried to include F module to Installed app module like this:
dependencies {
implementation project(':B')
implementation project(':F')
}
But gradle couldn't resolve local-lib, giving error:
Error:Could not resolve all dependencies for configuration ':Installed:releaseCompileClasspath'.
> Could not find :local-lib:.
Searched in the following locations:
... some remote repositories ...
Required by:
project :Installed > project :F
I tried to duplicate libs folder to project\Installed\libs, and it worked. So basically I need 2 copies of local-lib to make this work? How should I organise imports to avoid duplication? Perfect case would be if libs folder was inside F module.
IMHO, the cleanest way to make it work would be to use some local repository, like a Maven, publish your local-lib here, and reference it from here, and do the same for each of your libraries. When you publish to an artifact repository manager - let's say a Maven - you will have your .aar coupled with a pom file, containing all the needed dependencies.
You have to keep in mind here that your aar is kind of a flat file, meaning, while you reference it somewhere, there is not way to keep track of the transitive dependencies of it (that's the job of the pom files on Maven).
This means that when you reference F in Installed, the F aar is added, but Installed doesn't know that it has to get local-lib in order for F to work properly, or doesn't know where. That's why you have lines on the remote repositories: gradle searches everywhere (in every possible place = in every repository you have listed) for the dependency.
When you copy/paste the code as a module of your project, the gradle knows what are the transitive dependencies because it can access the gradle file for each dependency.
When you copy the aar directly inthe Installed/libs folder, it also works because gradle checks here (you probably have a compile line in your gradle checking for that folder).
If you want to keep the flat file, you should try putting somewhere reachable by all modules, on the same folder level (take a look at that question), or you could try to add the local-lib as an Android module project, and not just put it in the libs folder.

Android Studio: Resolving Duplicate Classes

When I try to run my android application on an Android device, the gradle console reports the following error:
Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/loopj/android/http/AsyncHttpClient$1.class
When I search for the "AsyncHttpClient" class, I see that it's indeed being found in two separate locations:
/Users/Afflatus/.gradle/caches/modules-2/files-2.1/com.loopj.android/android-async-http/1.4.9/5d171c3cd5343e5997f974561abed21442273fd1/android-async-http-1.4.9-sources.jar!/com/loopj/android/http/AsyncHttpClient.java
/Users/Afflatus/.ideaLibSources/android-async-http-1.4.9-sources.jar!/com/loopj/android/http/AsyncHttpClient.java
The first path seems to suggest it's a "cache" file... so I've tried invalidating & restarting my cache, but both files are still there after the gradle gets rebuilt and I try to run the application. I've read in alternate posts that it can be resolved by deleting one of the files... So I went to the cache location and deleted all the files found in the "1.4.9" folder... unfortunantly after reopening Android Studio, a new cache file gets created and I get the same error.
Other posts (here, here,here, and here) suggest if I add "./gradlew clean" to the root directory it would rebuild the gradle again just for the run (as far as I understand). So I tried doing that as well:
Which made my app's folder look like this:
But unfortunantly, that didn't help things I still get the same error. What am I doing wrong? What should I be doing?
I added this line to my gradle.properties file and my app worked
android.enableJetifier=true
Sometimes duplicate classes exception means that one of your dependencies uses implicitly the older or newer (with +) version of some library you also use in your project,
To resolve this issue you may add such block of code (put your library version after 'force') to your build.gradle file (Module:app):
configurations {
all {
resolutionStrategy {
// do not upgrade above 3.12.0 to support API < 21 while server uses
// COMPATIBLE_TLS, or okhttp3 is used in project
force 'com.squareup.okhttp3:okhttp:3.12.0'
force 'com.squareup.okhttp3:logging-interceptor:3.12.0'
}
}
}
You may also exclude some group from your dependencies.
For a single dependency you way write:
dependencies {
// example
implementation('log4j:log4j:1.2.15') {
exclude group: 'javax.jms', module: 'jms'
}
}
Tested to work on Android Studio with Gradle Plugin version 3.4.2 and Gradle version 5.4.1.
Credits go to Dimitar Dimitrov and Schalk Cronjé from gradle org discussion group
That's because you have added some library two times in libs folder, this could happen sometimes when you have multiple versions of the same library in the libs folder. Check it and remove any duplicate jar files.
And the second option could be you have also added the dependency in gradle.build and also have a jar in libs folder.
So check both places and remove duplicate entries and then clean and build APK again.
Delete files with duplicate jar extensions in the libs folder. However, if there are no duplicate files and there is still a "Duplicate classes" error, look for the name in the rest of the "Duplicate classes ...." clause in the error section. For example, "duplicated classes 'dagger' bla bla". Delete the file named 'dagger' from the libs folder. (Be careful not to delete it with shift.)
In my case, I am using sensorocloud.jar and the compile 'com.loopj.android:android-async-http:1.4.9' in my gradle which caused the same error as yours. Because sensoro cloud SDK used loopj's async-http.
I managed to solve it by manually removing the duplicate .class files in the jar file. (i.e.
changing the extension from jar to zip
extract it
remove the com.loopj.android .class files)
(P.S. I have tried to search through the web to see if I could exclude certain class of a jar in gradle, but not succeed, e.g. I referenced this SO post)
This error can be caused by several things;
misconfigured package name
Activity views that is not well binded. - simply go to your launcher activity view and ensure context is defined well e.g "com.yourdomain.package"
Re-create your BuildConfig and set it up well.
Check if your project build.gradle. There it might be some maven duplicate dependency
Here's another situation that can cause duplicate class during the mergeDexClasses task. This can happen with later versions of android gradle.
If your build.gradle.kts script has a dependency in the form:
implementation(project(":mylib", configuration="default"))
that can cause duplicate classes. The correction is simple. Just change it to:
implemenation(project(:mylib"))
Here's the Android Studio's Team explanation:
Having both project(":lib") and project(path: ":lib", configuration: "default") in the runtime classpath means that AGP gets both build/classes/java/main and build/libs/lib.jar (run ./gradlew :lib:outgoingVariants --all to verify). Because paths differ, we'll get 2 dexing transforms happening: 1 incremental that produces dex per class under build/.transforms (the one processing dir) and another one which produces single dex (the one processing jar). Later on during merging this causes failure.
AGP never publishes to the default configuration, in fact java-library plugin does it only so it does not break older build scripts. Having an explicit configuration name used in the dependency declaration is discouraged and Gradle attributes should be used instead.
In an older version of AGP, I ran into a problem where adding the configuration value "default" fixed some issue I was having. Well that no longer works, and adding the "default" configuration you can get duplicate classes.

Dependency error when using aar library

I have a module with a .aar file in libs folder. I used the solution posted here
[1]: http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/ to add the .aar file as dependency and was able to compile the module properly.
Now I want to use this module as a dependency to the main module in my project and compile. However when i try to compile, i do see an error which says that gradle was not able to find the particular .aar file. why would my main module not find a file which is in the libs folder of my sub module. Was wondering if anyone came across this issue.
my project structure is like this
--mainmodule
--build.gradle (submodule as a dependency)
--submodule
--libs
-- abc.aar
Here is the error gradle throws: When unzipping library ':abc:, either group, name or version is empty
If I understand your problem right and you've followed the steps described in the link you shared, then adding this to your mainmodule's build.gradle should do the job:
flatDir {
dirs "../submodule/libs"
}
You basically have the same issue that you fixed in your submodule, since the mainmodule is struggling to resolve transitive dependencies (abc.aar) of submodule.
Recommended way:
While the answer above should fix your issue, Android Studio supports a better way to do this. Import a local aar file via the File>New>New Module>Import .JAR/.AAR Package option in Android Studio v1.3+.
You can then have your submodule depend on that aar-module as follows:
dependencies {
compile project(':aar-module')
}

Android Studio: Setup external project as module

I need to use an external project(ResuableProject) as module in multiple projects(one of which is ProjectOne). What I've done here so far is:
Added following in settings.gradle of ProjectOne
include ':ProjectOne', ':ResuableProject'
project(':ResuableProject').projectDir = new File(settingsDir, '../ResuableProject/module')
Added following in build.gradle
dependencies {
compile project(':ResuableProject')
}
After that gradle sync without any error and in Project Explorer the module is included but when I use any class from ResuableProject it gives me an error that class cannot be found until I click on class name and it give me an option to add dependency on ResuableProject. When I select the option the class is accessible and it seems all working fine.
But when I Build the project and try to run it gives me this:
Error:Execution failed for task ':ProjectOne:compileDevDebugJava'.
> Compilation failed; see the compiler error output for details.
Error:(10, 30) error: package <packagename of ResuableProject> does not exist
Am I missing something or do I have to include source directory of ResuableProject in ProjectOne, if yes the how?
Also is there a better way to use one common project across multiple apps? (not jar or maven cause I need to include source with every project and common project will be hosted in a separate git repo)
Previously I was using this approach with Eclipse (one common Library project) and including it in multiple apps.
UPDATE: Adding file system structure
workspace
----ResuableProject (Android Common stuff)
--------.git
--------module
----ProjectOne (Android App One)
--------.git
--------ProjectOne (module)
------------build.gradle
--------settings.gradle
----ProjectOne (Android App Two)
--------.git
--------ProjectOne (module)
It turned out that you can add ResuableProject but cannot use its source if build.gradle has value apply plugin: 'android' i.e. the type of the project is application.
I changed that to apply plugin: 'android-library' i.e. it must be a library and now everything is working fine.

java.lang.NoClassDefFoundError: butterknife.ButterKnife

I've recently started doing Android development again and I am not yet used to the new gradle build system. Any dependency I add (so far I've tried Joda DateTime and butterknife), it always throws an error NoClassDefFoundError.
Here is what I've done. I'm using IntelliJ.
build.gradle:
compile 'joda-time:joda-time:2.1'
compile 'com.jakewharton:butterknife:4.0.1'
File > Project Structure > Modules > MyModule > Dependencies > Add Library, download from Maven, click export and bump it up the list.
My dependency list now includes a maven module com.jakewharton:butterknife:4.0.1
What am I doing wrong here? My IDE resolves everything fine, but at runtime none of the classes can be resolved.
Side note: My BUILD would not even complete until I added the above line to build.gradle. So I have to add the dependencies in Project > Project Structure AND to the build.gradle?
Also my project structure is:
UITest (parent)
UIBeta (android application)
UILib (android module)
I see the butterknife lib in the UITest\libs folder, shouldn't the children be able to find this?
Well I was going to delete this but maybe it will help someone else:
My gradle clean was broken because some aar was locked and it could not delete it, I had to close out IntelliJ, rename the aar (appcompat7 I think it was), then restart. I might not even have had to rename the file as I'm guessing the lock was released upon closing.
After restarting my clean worked, upon next run no more error.

Categories

Resources