Add an android library to the default list of libraries in Studio/SDK

筅森魡賤 提交于 2019-12-10 01:48:21

问题


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

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.


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/39116639/add-an-android-library-to-the-default-list-of-libraries-in-studio-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!