Android Studio and Library Projects [duplicate] - android

I'm creating projects with dependencies in Android Studio. I know how to link projects by adding modules.
But I realized that 'importing modules' create a copy of the libProject inside the project.
Is there a way to prevent that ? Like an 'external module' ?
Since i'm in charge of both project, I want to be able to push changes to the libProject Repo, without having to copy paste files between folders.
Thanks

Yes, you can do it. The module needs to have a Gradle build file set up for it. If it's got that, then in the project you're linking to it, add this to the settings.gradle file at the project root:
include ':libraryName'
project(':libraryName').projectDir=new File('/path/to/library')
where the path you specify in the second line is the path to the directory containing the library's build.gradle file. The path can be relative or absolute.

The solution:
include ':libraryName'
project(':libraryName').projectDir=new File('/path/to/library')
was not working for me. After couple of wasted hours I figured out the issue. There are two build.gradle files, one for project and one for library name. If the library is in the folder '\MyLib' then there will be a build.gradle in '\MyLib' and another at '\MyLib\app'. You have to point to the '\MyLib\app' and not '\Mylib'.
Hopefully this saves some time for others.

If you have, like myself, have multiple modules (I only realised today that copies were included, I thought that the project included links to the source.)
You can have multiple modules/projects along the lines of :-
include ':app', ':sqlwords', ':dbindex', ':dbcolumn', ':dbtable', ':dbdatabase', ':displayhelp', ':pickdate'
project(':sqlwords').projectDir= new File('d:/Android_Applications/Modules/sqlwords')
project(':dbcolumn').projectDir= new File('d:/Android_Applications/Modules/dbcolumn')
project(':dbtable').projectDir= new File('d:/Android_Applications/Modules/dbtable')
project(':dbindex').projectDir= new File('d:/Android_Applications/Modules/dbindex')
project(':dbdatabase').projectDir= new File('d:/Android_Applications/Modules/dbdatabase')
project(':displayhelp').projectDir= new File('d:/Android_Applications/Modules/displayhelp')
project(':pickdate').projectDir= new File('d:/Android_Applications/PickDateShowCase/pickdate')

You can also use android { sourceSets{ main.java.srcDirs += '../../../library/src' }} in your app build.gradle . Not sure about supporting all android resources, for pure java library works well.

Related

Use different Git repo for different modules

In my Android application I've created a new library module. Now I have this structure:
Right now I have under version control on Bitbucket whole project less datingcorelib. I would like to use a different repo to this library module.
It's possible to use two different repos on the same project?
Yes, it is possible. You're looking for a thing called submodule.
However, it may be tricky to use such a submodule within a project as it will have its own structure. So you also need to include the right gradle project from that submodule.
Let's say you pushed your library project somewhere. Let it be git#github.com:Sami/my-library.git. We also assume that it has the common structure for Android library project, i.e. it has a root build.gradle file and a subfolder datingcorelib with actual source code. This is what we need to include into the app.
You need to delete datingcorelib from your app's project. Then add the library as a submodule:
git submodule add git#github.com:Sami/my-library.git libraries/datingcorelib
After that open settings.gradle file of your app's project and add a new line there:
project(':datingcorelib').projectDir = new File("$rootDir/libraries/datingcorelib/datingcorelib")
Sync the project. Now you should be able to use the code from another repository.

Compiling module from another project

I have my main project with some modules: :app, :anotherModule, ... .
Then I forked a library on github and pushed it down in another project. It has two modules, a library :library and a sample app :sample.
What I'm trying to do is to integrate :library in my project at compile time, i.e. without copying files (as someone has suggested here).
What I did
I edited my project settings.gradle and added:
include ':library'
project(':library').projectDir = new File(settingsDir,'../ForkedLibrary/library')
referencing the folder of the forked library. At the same time I added compile project(':library') to my app module.
What's wrong
Compiling fails with:
Could not find property 'ANDROID_BUILD_SDK_VERSION' on project ':library'
I know why it happens: ForkedLibrary/library/build.gradle is referencing some project values in ForkedLibrary/gradle.properties, which is something like:
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=23
ANDROID_BUILD_SDK_VERSION=23
ANDROID_BUILD_TOOLS_VERSION=23.0.0
... + LOTS of other stuff, POM_ info, version names, ...
I could fix this by copy-pasting these lines into my project gradle.properties, but it makes no sense to have :library-specific stuff in my own gradle properties files. That ForkedLibrary properties files includes things unrelated to my project, like original developer info and other POM_ fields.
So
How to solve this (without including ForkedLibrary properties in my own gradle.properties file)?
It should be a comment, but it is too long. I will delete it if it doesn't help you.
The ForkedLibrary/library/build.gradle as you described in your question is using the ForkedLibrary/gradle.properties.
It works only in the ForkedLibrary specific project because the other project uses its own gradle.properties.
I guess the library/build.gradle file has somenthing like:
project.VERSION_NAME
You can refer the official doc:
The configuration is applied in following order (if an option is configured in multiple locations the last one wins):
from gradle.properties in project build dir.
from gradle.properties in gradle user home.
from system properties, e.g. when -Dsome.property is set on the command line.
In this case you have to copy the properties in your build.properties
There is an alternative that I have never tried in this case, something like this:
processResources {
expand(project.properties)
}
As you can read in the official doc:
Copies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.
And the expand method is:
Expands property references in each file as it is copied.

Change build.gradle template file to include libraries upon creation

Whenever I made a new android studio project, I usually have to add the same handful of libraries to my build.gradle file. To save time, is there a way to modify the template for the generated build.gradle file to include these libraries? I found a way to do that for gradlew and for the parent gradle file but not for the app itself. Any help is appreciated.
Cheers!

libgdx gradle - forgot to make android project

I set up my libgdx projects with HTML5 and Desktop, but later decided to add Android support. How do I add the Android project?
I'm afraid if I use the setup UI again it'll overwrite my code..
Use the setup-ui once more with exactly the same parameters like you did when you were initially creating the projects, but use another destination folder. Then re-create all of them again.
When you are done with that, copy the android folder to your original projects. You will also need to copy some of the single files in the root folder. Those are settings.gradle and build.gradle. If you have changed your build.gradle file manually already, you should only copy the part for the android project to the original file instead of replacing it.
After that you should re-import all the projects into your IDE once more.

Include non-static library project in Android Studio?

What's the best way to include a non-static library project in Android Studio?
I've developed a library that is used by multiple projects and it is currently under active development. In Eclipse I can continue developing and include it in multiple projects just by being in the same workspace, is there an equivalent way to do it in AS?
When I make changes to the library source I don't want to have to keep copying the source manually into modules of all the dependent projects, and putting it on Maven is not an option. The library and all dependent projects are hosted in SVN.
Any ideas or suggestions? Thanks!
I'm not sure if this is the "right" or best way, but it works for me. I don't use SVN, so I have no idea how that would affect this methodology.
Where you specify sourcesets, you can specify additional directories. Suppose my library project's root directory is in the same directory as my current working project. I would change this line as shown under the sourcesets section:
java.srcDirs = ['src', '../myLibraryProject/src']
And you can do the same for res.srcDirs and so on as applicable.
This results in a funny looking directory tree, where it shows the additional source directory as a sibling to the project's root directory, and the project's own src directory is repeated as a sibling. I think this might be a bug, but it does allow me to go into the library project's source and edit it as if it is part of the same project.
I was pointed in the direction of this question, which solved my issue perfectly - Add local Library Project as a dependency to multiple Projects in Android Studio

Categories

Resources