Android studio add external maven project on disk as dependancy - android

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

Related

Android imported AAR how to keep changes in sync with original AAR project?

If I have project-A which is a SDK and its being exported as a AAR into project-B which imports the module as a AAR how could i go about linking the two modules so changes made to the AAR in project-B don't have to be duplicated in the original project-A?
Create a Project A (library), create project B(application) and put them in the same project.
update the settings.gradle file like this:
include ':projecta', ':projectb'
And make sure the projects are in the same directory
.gradle
.idea
projecta
projectb
etc
By doing this you won't have to explicitly add the aar to project B because they will be in the same project and both automatically be built when you make a release.
Edit about linking with multiple projects
In the case you want to have links to multiple projects your best bet is to create a maven repository.
Doing so will allow you to use your project in the following way:
build.gradle
dependencies{
implementation com.mydomain.projecta:1.0.1
}
Any project you want to use you project A with can be used by writing the line above.
When you update project A you update your maven server and change the version to 1.0.2
You also will have to update the dependencies in the linked projects:
dependencies{
implementation com.mydomain.projecta:1.0.2
}
I think that is the most stress free way to distribute your SDK in one place and easily resuse it in multiple places.
Here are some links about setting up a maven server
https://inthecheesefactory.com/blog/how-to-setup-private-maven-repository/en
https://www.androidauthority.com/add-a-github-library-to-android-studio-using-maven-jcenter-jitpack-777050/

Git submodules in existing gradle project

I have some application which has few modules that i want to to replace in submodule.
Can I do that in the current project. Create new repo with these modules.
Yes you can achieve this in following way :
Say your main project is in X_REPO\Project\app
Move your module in a separate repo say Y_REPO
Then add Y REPO as submodule in X_REPO\Module1
Now make the following changes in
settings.gradle
include ':Module1'
project(':Module1').projectDir=new File('../Module1/module_directory')
X_REPO\Project\app\build.gradle
compile project(':Module1')
Since Android Studio uses same gradle build system, same can be achieved in Android Project as well.

Separate an Android project in several other dependents libraries

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.

how do you clone a project from github into the same project

In android studio how do you git clone a project from github into the same project?
If I got your question correctly, you want to check out some library and add it as a dependency to your (gradle based?) project.
The prefered way would be to checkout it separately and built it as a library project. And if you have maven you can then install it to your local repository.
The other way (and probably what are you trying to achieve) is to add it as a submodule of your local repository.
git submodule add git://repourl.git yourSubmoduleFolder
Then you can add this library as a module dependency to your application module. (You don't have to use git submodules, you can always just get the repo as a zip and extract it into your project)
If you put your library under libraries folder in your project then you can do it like this.
Add this to your settings.gradle
include ':libraries:yourLibraryModuleName'
Add this to your build.gradle
dependencies {
compile project(':libraries:yourLibraryModuleName')
}
You should now be able to build your application with the submodule
Please note that this was just a quick answer, you can find more info on both gradle submodules and gradle module dependencies here on StackOverflow. I guess this should be enough to point you in a right direction.
It does not. However it's a good practice to put it in libraries or libs,...folder

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

Categories

Resources