14

So first of all, just to clear any doubts, i know how to add a library to an android project. My question here is that, i want to add a library to the list of default libraries in my android studio.

Let me explain with an example. Let's say i want to add the Glide library to my project. To do that firstly, i have to go to glide github page and then, from there i copy the compile 'com.github.bumptech.glide:glide:3.7.0' text and paste in in my build.gradle file . Then the android studio would download the library over the internet. So every time i want to use the Glide library in any of my project, i have to do this. What i want , is that, i want to include a specific libary like Glide in my Default list of libraries that are available to me

enter image description here

So Here, is where i want to include my library project, so that i can use it, in any of my projects. Thanks in advance, for reading.

Nick Asher
  • 726
  • 5
  • 19
  • 1
    Have a look at Android Studio's Live Templates feature https://www.youtube.com/watch?v=4rI4tTd7-J8 – user Aug 24 '16 at 07:24
  • 1
    Check this post http://stackoverflow.com/a/39116613/1770868 – Ahmad Aghazadeh Aug 24 '16 at 07:26
  • @Luksprog, Firstly thank you for reading, It is an alternate way of reusing code. – Nick Asher Aug 24 '16 at 07:29
  • @ ahmad aghazadeh, This does not solve the problem though – Nick Asher Aug 24 '16 at 07:31
  • Found [source code](https://android.googlesource.com/platform/tools/adt/idea/+/ae76870f096769f556c0fcb895dfc1327be0a578/android/src/com/android/tools/idea/structure/MavenDependencyLookupDialog.java) for the shown dialog, if this can be helpful for you. – R. Zagórski Aug 24 '16 at 10:30
  • @NickAsher Adding a library to a project is a line of code, isn't that what you want to do? The idea was to create a live template for the line 'com.github.bumptech.glide:glide:+' and assign it a shortcut(for example libGlide). Then in every project you could simply go to the gradle file, start typing libGlide and hit ENTER. – user Aug 24 '16 at 12:11
  • @Luksprog But can't i add the library to the default list directly – Nick Asher Aug 24 '16 at 16:40

2 Answers2

4

There is a way to add the library to every new project/module automatically. There is a folder with templates in the Android Studio directory. Look for:

..\Android Studio\plugins\android\lib\templates\gradle-projects\NewAndroidModule\root

In this directory there is a file:

build.gradle.tfl

Modify it according to your needs.
Example:

dependencies {
    <#if dependencyList?? >
    <#list dependencyList as dependency>
    compile '${dependency}'
    </#list>
    </#if>
    compile fileTree(dir: 'libs', include: ['*.jar'])
<#if WearprojectName?has_content && NumberOfEnabledFormFactors?has_content && NumberOfEnabledFormFactors gt 1 && Wearincluded>
    wearApp project(':${WearprojectName}')
    compile 'com.google.android.gms:play-services:+'
</#if>
<#if unitTestsSupported>
    testCompile 'junit:junit:${junitVersion}'
</#if>
    compile 'com.github.bumptech.glide:glide:3.7.0'
}

As you can see, the Glide library has been added at the end.

When this file is modified, every new Android Phone & Table module will have this library included.

Of course there is nothing that prevents you from adding your own module or project template.

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
3

I am afraid it is not so simple.

I looked at the newest Android Studio source code (branch master) and the code for this dialog (MavenDependencyLookupDialog.java). As you can see by looking at this line the dependencies are held inside ImmutableList and are hardcoded. There are held only not-support dependencies.

The support dependencies are held in different place (RepositoryUrlManager.java). But they are also hardcoded.

You have three choices:

  • Change module template as described in my previous answer
  • Create a Live template to automatically type glide and Intellij would give you a hint to insert glide dependency
  • Change Android Studio source code, make a Pull Request and convince people responsible for it to make your changes into consideration.
Community
  • 1
  • 1
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90