Separate an Android project in several other dependents libraries - android

I have a project named 'app-project' (abbreviate here as AP) that have a module "app". To make things reusable, this project depends on another project library named 'lib-android-network' (LAN) and his module lib. It depends on another project named 'lib-android-base' (LAB) and his module "lib". All these projects stay in the same directory in the hierarchy. I use Gradle and Intellij on Windows 8.1.
ex: directories:
root/app-project
root/lib-android-network
root/lib-android-base
I start with project AP depends on project LAN, without project LAB.
to make AP see LAN my settings.gradle of AP was:
include ':app', "../lib-android-network", "../lib-android-network:lib"
and in build.gradle of 'app' module I have:
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':../lib-android-network:lib')
I don't know how (I have to make a lot of attempts until reach this configuration) but it works perfectly until now, when need the third, LAB, project.
As I say now I have LAN depends on LAB. So I put in LAN settings.gradle file:
include ':lib', '../lib-android-base', '../lib-android-base:lib'
And in build.gradle of LAN 'lib' module:
compile project(':../lib-android-base:lib')
It's the same idea that works for AP - > LAN dependency, but when I run Gradle against my first project I get this error:
Error:(37, 0) Project with path ':../lib-android-base:lib' could not be found in project ':../lib-android-network:lib'.
I think it's because the dependent projects should stay inside the parent project as modules, but Its not interesting for me, because I don't want to have duplicate the project one inside another to make it work. I want to leave same directory hierarchy level. I don't want to add Intellij modules dependencies too (unless sure work), cause before I reach the first worked configuration, I have a lot of problems doing this.
EDIT
Based on this answer and this example project I change somethings in my own project to make it more correct:
AP project:
settings.gradle:
include ':lib-android-network', ':lib-android-network:lib'
project(':lib-android-network').projectDir = new File(settingsDir, '../lib-android-network')
project(':lib-android-network:lib').projectDir = new File(settingsDir, '../lib-android-network/lib')
app/build.gradle:
compile project(':lib-android-network:lib')
LAN project:
settings.gradle:
include ':lib-android-base', ':lib-android-base:lib'
project(':lib-android-base').projectDir = new File (settingsDir, '../lib-android-base')
project(':lib-android-base:lib').projectDir = new File (settingsDir, '../lib-android-base/lib')
lib/build.gradle:
compile project(":lib-android-base:lib")
But I'm still getting the same error:
Error:(39, 0) Project with path ':lib-android-base:lib' could not be found in project ':lib-android-network:lib'.
--- EDIT 2 ---
it works if I repeat the settings.gradle configuration of network project into app project, but
include ':lib-android-base', ':lib-android-base:lib', ':lib-android-network', ':lib-android-network:lib'
project(':lib-android-base').projectDir = new File(settingsDir, '../lib- android-base')
project(':lib-android-base:lib').projectDir = new File(settingsDir, '../lib-android-base/lib')
But I guess I shouldn't have to repeat this configuration in my base application whenever I had to add a dependency to lib-android-network project. It will get confused...
Is there a "Best Practice" to deal with a several library projects? Which is the best way to deal with it? Is it adding each lib as sub module for the top project?

Although I haven't found a perfect solution. So far, the best solution I found was based on this blog post found by #hgoebl and posted in comments:
To describe here what I did:
I make a change on project lib-android-base: To test another different configuration I changed the module name 'lib' to 'lib_base' and left so, but there was no influence.
I change the IDE from Intellij 14.1.4 to Android Studio 1.3.1: I thought the android studio coped better with the imports made in Gradle.
On lib-android-network in the settings.gradle file I added:
//":base" here can be any word you use to name lib-android-base project in lib-android-network context.
include ':base'
project (':base').projectDir = new File ('../lib-android-base/lib_base/')
On lib-android-network/lib build.gradle file I added:
compile project (':base')
On my App Project settings.gradle file I added:
include ':base'
include ':network'
project(':base').projectDir = new File ('../lib-android-base/lib_base/')
project(':network').projectDir = new File('../lib-android-network/lib/')
and in App Project module build.gradle file I just added:
compile project (':network')
The only problem I not found a solution was the fact I had to reference the 'android-lib-base' in the App project, since I don't use it directly. But for me it's ok for now.

Related

Android studio add external maven project on disk as dependancy

I have a similar problem as described in this
Question
So I have an android project which depends on this popular library XChange. As XChange is not very android friendly (Uses JDK 8 streams etc and Android does not support them fully (Link) and I have some other custom requirements for the library, then I decided to fork the project.
So currently I am using IntteliJ Idea to develop the XChange maven project, commit the changes and use Jitpack.io to include the new commit as dependency to Android studio. This works, but is horribly time consuming.
So I have tried or though of the following ways to overcome this.
1) Convert the Maven project to gradle and use the Android studio import feature - This makes a copy of the source code and therefore the git support drops and I cannot pull the changes from the main origin of XChange
2) Convert to gradle project and try to include it through settings.gradle and include it as dependancy as in the referenced question accepted answer at the beginning. This does not work unfortunately, because as XChange has a lot of subprojects and the subproject dependancy import fails.
include ':xchange-core'
include ':xchange-kraken'
project(':xchange-core').projectDir = new File(settingsDir, '../XChange/xchange-core')
project(':xchange-kraken').projectDir = new File(settingsDir, '../XChange/xchange-kraken')
3) Use MavenLocal somehow, but I have no idea how to get this to work (Using IntelliJ idea)
XChange has the following setup
\XChange (has its own pom.xml with settings for all subprojects)
+ xchange-core (main dependency for all subprojects)
+ xchange-kraken
+ xchange-binance
+ ...
So to sum up, Does anyone has a way to save some time on building the project when I need to include my own fork of dependency from disk to Android Studio?
EDIT
Example problem
Settings.gradle
include ':app'
include ':xchange-core'
project(':xchange-core').projectDir = new File(settingsDir, "../XChange/xchange-core/")
Build.gradle
...
compile project(':xchange-core')
Error
Error:(4, 0) Could not find method compile() for arguments [{group=com.github.mmazi, name=rescu, version=2.0.1}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Here is how I resolved my problems.
Step 1. clone project (in my case the XChange fork) as git submodule to you main app folder, so you can keep version control
git submodule add 'YOUR_GIT_REPOSITORY'
Step 1.5. If you had a maven library project, then run gradle init from terminal at your library folder. This will convert your maven project to gradle one. Necessary for Android Studio.
Step 2. If you have any top level build.gradle or settings.gradle, then forget about those. Defenitely not a perfect solution, but the only way I got it working. In you main apps settings.gradle, you must define each submodule like this
include ':app'
include ':xchange-core'
...
project(':xchange-core').projectDir = new File(settingsDir, "../XChange/xchange-core/")
...
If you had a top level build.gradle in your library project, then put anything you can in your apps top build.gradle file and everything else I copied to the library submodule build.gradle files (Yes, to each and every one)
Step 3. In your apps build.gradle files, now you can include you library submodules as
...
implementation project(':xchange-core')
...
Also, this Medium post was very useful Link

Importing project from Eclipse to Android Studio - Dependencies

On Eclipse I have one application as lib.
Then I have 10 more applications that import this lib.
When I'm programming, I do this on the lib project, so I run the application that import this lib. In the application project I just do especific developments.
The code is shared by all aplications.
How can I do this in Android Studio? Because I need to have just one code for all projects.
I've already imported my lib to Android Studio but I don't know how to link it to my application, having in mind that all changes in the lib needed to reflect the other projects (when I open it and compile again).
Thank you all!
---Edit:
My lib settings:
include ':actionbar'
include ':myMainLib'
My myMainLib Gradle:
dependencies {
compile project(':actionbar')
}
My Application settings:
include ':myApp'
include ':myLibProject:myMainLib'
project(':myLibProject:myMainLib').projectDir = new File(settingsDir, '../myLibProject/myMainLib')
My Application gradle:
dependencies {
compile project(':myLibProject:myMainLib')
compile 'com.android.support:support-v4:18.0.0'
}
Error message:
Error:(22, 0) Project with path ':myLibProject:actionbar' could not be
found in project ':myApplication'.
When I clicked to check the error, it's open the lib Gradle.
The actionbar source is in the lib's root dir.
As you have the lib and your apps on your hard drive, you can configure Gradle to resolve the dependency from the local file system.
You have to add these lines to your settings.gradle file, to have the library project available:
include 'yourlib:lib'
project(':yourlib:lib').projectDir = new File(settingsDir, '../yourlib/lib')
This code assumes that both project folders are in the same folder. You can adjust the relative reference or change it to an absolute reference if that fits better for you.
Then you can add the dependency to your app's build.gradle file:
compile project(':yourlib:lib')

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')
}

Library projects in Android Studio only work if nested in the project tree?

I'm trying to build a project dependent on ActionbarSherlock in Android Studio.
After much reading I've ended up having a working build, but only if I nest the ABS project inside my main project like so:
MainProject
MainProject\ABS (copied here and then used import module)
MainProject\MainModule (the one created by the wizard)
I had to edit the various Gradle files, settings.gradle is as follows:
include ':OnTop'
include ':ActionbarSherlock'
I fixed the dependencies in the build.gradle files accordingly.
This is suboptimal, I think. Surely I don't want to replicate the ABS module in every new project that uses it? What should I be doing instead?
Every step are here : How do I add a library project to Android Studio?
If you want to more about gradle see the Google I/O conference by Xavier Ducrohet on youtube : http://www.youtube.com/watch?v=LCJAgPkpmR0
I searched this issue enough and finally these are my results:-
This sample project
-> project root
d:\asprojects\thisproject
-> module
d:\asprojects\thisproject\MyModule
-> libraries
d:\asprojects\lvl
d:\asprojects\MyLibrary
-> jars
D:/asprojects/android-support-v4.jar
D:/asprojects/GoogleAdMobAdsSdk-6.4.1.jar
-If you want to use external (outside the project) libraries then the settings.gradle has to point to it like so:-
settings.gradle
include ':lvl', ':MyLibrary', ':MyModule'
project(':MyLibrary').projectDir = new File('D:/asprojects/MyLibrary')
project(':lvl').projectDir = new File('D:/asprojects/lvl')
-Then you need to setup the build.gradle for the MyModule correctly like so:-
build.gradle
dependencies
{
compile files('D:/asprojects/android-support-v4.jar')
compile files('D:/asprojects/GoogleAdMobAdsSdk-6.4.1.jar')
compile project(':lvl') compile project(':MyLibrary')
}
-But the IDE will not like these absolute paths and will say not found.
-But gradlw from commandline work justs fine:-
gradlew clean
gradlew build
gradlew installRelease
etc

Proper way to add global library in android-studio/gradle

First of all, I know how to add a local library to the build.gradle file, it was discussed in several questions here already (which are all basically the same), see here, here and here. But you have to hardcode the paths in the compile files('/path/to/lib.jar') statements in the build.gradle file, which isn't nice, not redistributable, etc, IF you use a library not within the project's folder structure. I prefer to maintain this library for all my projects in the same place (so it is always up to date for all projects etc.). So I would like to know how to add a library, which is not available via Maven, to an Android-Studio project using gradle, in a sane way, given that the library is added as a global library in AS's preferences.
What I have done so far:
I use Google's new Android-Studio, which uses gradle for the build management, to build an Xposed framework module. For that, I have to include an external library, XposedLibrary, which I downloaded from the respective Github repository to keep it up-to-date.
It contains the jar XposedLibrary/XposedBridgeApi.jar, which I added in AS as a global library (Ctrl+Shift+Alt+S -> Global Libraries -> green plus to add the folder XposedLibrary). The compilation failed, complaining that it doesn't know the imported classes. So I had to manually add the library to the build.gradle file, adding the respective line in the dependencies like so:
dependencies {
compile files('libs/android-support-v4.jar')
compile files('/home/sebastian/dev/android/XposedMods/XposedLibrary/XposedBridgeApi.jar')
}
I tried out to just add compile files('XposedBridgeApi.jar') or compile files('XposedLibrary/XposedBridgeApi.jar') but this didn't work.
So, what is a nice way to add an AS global library to the dependencies without using full paths? (I don't like the idea of symlinking to the jar file from within the lib/ folder ;) )
when referencing a file via
files("relative/path/to/a.jar")
the relative path is evaluated relative to the buildscript this snippet is in. so when your build.gradle file is located in let's say '/a/project/build.gradle' then the jar should be in '/a/project/relative/path/to/a.jar'. In a multiproject gradle build you can put the the jar in a folder relative to the root - project and reference it in all subprojects via
rootProject.files("relative/to/root/a.jar")
hope that helps,
cheers,
René
This post describes how to get XposedBridgeApi.jar working with Gradle in Android Sudio: http://forum.xda-developers.com/showpost.php?p=41904291&postcount=1570
I think here is the proper way:
Import Xposed in Android Studio
Edit the /app/build.gradle like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
provided fileTree(dir: 'deps', include: ['*.jar'])
}
The best way is to use "provided files('src/XposedBridgeApi-54.jar')" as the lib isn't allowed to be included in the module, because the XposedBridge is already installed on the phone.
With Android Studio, you have to first understand that the IDE uses the same model for a project that your command line build (gradle) uses. That is why the Project Structure dialog has a pop up that says edits here will have no effect. So adding a global library will also have no effect.
The correct way to fix such issues is to edit your gradle build scripts so that the command line gradle build works properly. Then you should just have to click on "Tools | Android | Sync Project with Gradle files" menu item to refresh the project structure in the IDE.
Finally, if your dependencies are not going to be in Maven Central, then you'd have to create a local maven repository. Read the thread here: https://groups.google.com/d/msg/adt-dev/eCvbCCZwZjs/vGfg-4vNy9MJ for background.

Categories

Resources