Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom dependencies automatically in android for any new project?

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'

What should I do?

like image 463
Mohamed AbdelraZek Avatar asked Mar 09 '17 08:03

Mohamed AbdelraZek


1 Answers

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.

like image 141
Kiran Benny Joseph Avatar answered Oct 13 '22 02:10

Kiran Benny Joseph