Reputation: 101
Say for example I have a folder on my machine, which is located at D:\AndroidLibraries, and a colleague has the same folder, yet in C:\Users\Jim\AndroidLibraires.
We're both working on android project A, in android studio, and we both want to use libraries (jar, and aar) from that library.
In these question we see that we can define a project dependency as a reference to its location on the hard disk (unless we understood it wrong):
Is it possible to declare git repository as dependency in android gradle?
Reuse modules in Android Studio
Now the question is, how can we abstract the file-system path, using environment variables? For example something like this:
project( ':security' ).projectDir =
new File(settingsDir, '%android_libraries_root%\security' )
Upvotes: 2
Views: 9386
Reputation: 4319
For a more portable / long-term solution, you could use a local 'flatdir' repository, as discussed https://docs.gradle.org/current/userguide/dependency_management.html#sec:flat_dir_resolver and How to add local .jar file dependency to build.gradle file?
So for instance:
final String androidLibs = System.getenv('android_libraries_root') ?: "${System.getProperty('user.home')}/AndroidLibraries"
// TODO: Might want to warn/error out if the folder is not present
repositories {
flatDir {
dirs "${androidLibs}/security"
}
}
Essentially, in this example, it assumes the default will be under the user home dir, when no ENV variable is defined, which may or may not work for you.
And then you can declare actual dependencies, e.g.
compile 'com.android.support:multidex:1.0.1'
The flatDir repository will only need to have the versioned file directly under it, i.e. %android_libraries_root%\security\multidex-1.0.1.jar. In other words, it ignores the 'org' segment.
Of course, if your env variable is not defined and/or the default doesn't work, it will fail to resolve the dependency, so you may want to add some code to at least warn the user that the flatDir repo won't be able to find security libraries.
OK, so this is a bit more work than referencing the files directly, but one advantage of starting like this, is that you can later switch to a shared or public repo with no changes to your dependency declarations (you would just need to add (or replace the flatDir repo) a repo declaration from where the android libraries can be retrieved.
Upvotes: 0
Reputation: 10012
System,getenv("variable name") works in gradle to get the path. I got the reference from this So this should get the actual path of your folder which contains the jar and aar file
Upvotes: 2