如何在Android中为任何新项目自动添加自定义依赖项?

如何在Android中为任何新项目自动添加自定义依赖项?

问题描述:

如果我通常使用这些库,我希望有一种方法可以将它们包含在我创建的任何项目中.

If I use these libraries usually, I want a way to include them in any project I create.

 implementation 'com.android.support:recyclerview-v7:25.1.1'
 implementation 'com.squareup.picasso:picasso:2.5.2'
 implementation 'com.jakewharton:butterknife:8.5.1'
 implementation 'com.squareup.okhttp3:okhttp:3.6.0'
 implementation 'com.facebook.stetho:stetho-okhttp3:1.4.2'

我该怎么办?

您可以找到您的

You can find your App level (In which you can add your dependencies as shown below) Gradle template

转到

android-studio-path \ plugins \ android \ lib \ templates \ eclipse \ projects \ NewAndroidApplication \ root \ build.gradle.ftl

并像这样编辑 build.gradle.ftl

buildscript {
    repositories {
<#if mavenUrl == "mavenCentral">
        mavenCentral()
<#else>
        maven { url '${mavenUrl}' }
</#if>
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:${gradlePluginVersion}'
    }
}
apply plugin: 'android'

repositories {
<#if mavenUrl == "mavenCentral">
    mavenCentral()
<#else>
    maven { url '${mavenUrl}' }
</#if>
}

android {
    compileSdkVersion ${buildApi}
    buildToolsVersion "${buildToolsVersion}"

    defaultConfig {
        minSdkVersion ${minApi}
        targetSdkVersion ${targetApi}
    }
}

dependencies {   /// ADD YOUR DEPENDENDENCIES HERE
    compile 'com.android.support:support-v4:${v4SupportLibraryVersion}'
    compile 'com.android.support:recyclerview-v7:25.1.1' //add your dependancies here
    compile 'com.squareup.picasso:picasso:2.5.2' 
    compile 'com.jakewharton:butterknife:8.5.1'
    apt 'com.jakewharton:butterknife-compiler:8.5.0'
    compile 'com.squareup.okhttp3:okhttp:3.6.0'
    compile 'com.facebook.stetho:stetho-okhttp3:1.4.2'
}

allprojects {
    repositories {
        jcenter()
<#if mavenUrl != "mavenCentral">
        maven {
            url '${mavenUrl}'
        }
</#if>
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

对于您的 项目级别 Gradle模板转到

For your Project level Gradle template go to

android-studio-path \ plugins \ android \ lib \ templates \ gradle-projects \ NewAndroidProject \ root \ build.gradle.ftl


注意 :

Note:

  1. 我的android-studio-path是 C:\ Program Files \ Android \ Android Studio .替换为您自己的
  2. 执行此操作之前,请备份build.gradle.ftl文件.
  1. My android-studio-path is C:\Program Files\Android\Android Studio. Replace with your own
  2. Before doing this make a backup of build.gradle.ftl file.

编辑

如果您遇到 访问被拒绝 的问题,授予您的编辑者管理权限.在Windows中,在搜索框中键入您的编辑器(例如记事本)名称,然后右键单击并选择以管理员身份运行.然后

Edit

If you are facing access denied problem then grant your editor administrative privileges. In windows type your editor (eg. Notepad) name in the search box and right click and select run as Administrator. And then

File => open =>上方文件

File=>open=>above file


如果您再次面对,则必须关闭android studio并重试.应该可以.


If you're facing again you must close android studio and try again. It should work.

请参阅Gradle 依赖性管理文档

See Gradle documentation of dependency management

您还可以在 template 文件夹中自定义更多内容.

And you can customise much more in template folder.