How to add custom dependencies automatically in android for ever a new project is created?

流过昼夜 提交于 2019-11-30 10:10:18
Kiran Benny Joseph

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

Go to

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

And edit build.gradle.ftl like this

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
}

For your Project level Gradle template go to

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


Note:

  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.

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=>above file


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

See Gradle documentation of dependency management

And you can customise much more in template folder.

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