Android Studio Gradle Configuration

Source: Internet
Author: User

Gradle Basic Concepts

First we learn a few Gradle script syntax, master these several grammar, you can very simple use Gradle Build Pack Android project. First, let's look at the next most simple Android build.gradle .

Build.gradle

    //setting the runtime environment for scripts     buildscript {          //supports java  dependent Library Management (MAVEN/IVY) for project dependencies.  repositories {            mavencentral ( )         }        //the definition of a dependent package. Support Maven/ivy, remote, local library, also support single file         dependencies {             classpath  ' com.android.tools.build:gradle:0.4 '         }    }    //declares the type of project built, This is of course Android     apply plugin:  ' Android '     // Set parameters for compiling Android projects     android {         Compilesdkversion 17        buildtoolsversion  "            defaultConfig { "            minSdkVersion 8             targetSdkVersion 17         }        //android Default configuration          sourcesets {            main  {                 manifest.srcfile  ' Androidmanifest.xml '                  java.srcdirs = [' src ']                 resources.srcdirs = [' src ']                 aidl.srcdirs = [' src ']                 renderscript.srcdirs = [' src ']                 res.srcdirs  = [' Res ']                 assets.srcdirs = [' Assets ']             }            //the path where the test is located, this is assumed to be the tests folder, No can not write this line             instrumenttest.setroot (' Tests ')         }                 //This is the code to solve lint error          Lintoptions {            abortonerror false         }        /**          *  Signature Settings          */         signingConfigs {             myConfigs {                 storefile file ("Signature file address")                  keyAlias  "..."                  keyPassword  "..."                  storePassword  "..."             &nbSP;}         }        /**          *  Obfuscation Settings           */        buildTypes {             release {                 signingConfig signingConfigs.myConfigs                 runProguard true                 proguardFiles  Getdefaultproguardfile (' proguard-android.txt '),  ' Proguard-rules.pro '              }        }         /**         *  Channel packaging (different package name)           */        productFlavors {             qqqq {                 applicationId =  ' package name '              }             hhhhh {                 applicationid= ' package name '             }         }    }    /**    Import of   * .so files      */    task copynativelibs ( Type: copy)  { &NBsp;      from filetree (dir:  ' libs ', include:  ' armeabi/*.so ' )  into  ' Build/lib '     }    tasks.withtype (Compile)  {         options.encoding =  "UTF-8"     }         tasks.withtype (Compile)  {         compileTask -> compileTask.dependsOn copyNativeLibs     }    clean.dependsOn  ' Cleancopynativelibs '      Tasks.withtype (com.android.build.gradle.tasks.PackageApplication)  { pkgTask ->         pkgtask.jnifolders = [new file (buildDir,  ' lib ')]     }    //Dependent Libraries     dependencies {     compile filetree (dir: ‘ Libs ',  include: [' *.jar '])     }

Local dependency

Gradle as a build tool, it is easy to use the local jar package, the following is the code block used.

dependencies {//single file dependent compile files (' Libs/android-support-v4.jar ')//A folder under all dependent compile Filetree (dir: ' Libs ', includ E: ' *.jar ')}android {}

Remote dependency

Gradle also supports Maven,ivy, because Ivy I have not used, so with Maven as an example, the following is the code block:

Repositories {//from the Central Library to obtain the dependency mavencentral ()//or using the specified local MAVEN library Maven{url "file://F:/githubrepo/releases"}//or using the specified remote MA VEN library Maven{url "Remote Library Address"}}dependencies {//Application format: PackageName:artifactId:versioncompile ' COM.GOOGLE.ANDROID:SUPPORT-V4: R13 '}android {}

Android Library Dependent

For a project that relies on the Android library, it's not dependent on a jar, so it's easy to use the Gradle mulit project mechanism here. in the past, the Android library did not have a good package management method, in short, before the advent of Gradle, the official does not have a way to manage the Android library dependency package, generally we are directly download other people's Android The library project source code is integrated, while for third parties The Android-maven-plugin is in apklib format.

Now, officially, the official launch of an Android library packaging format, the extension of the *.aar . As mentioned earlier, the current Android Gradle plugin does not support the local direct use *.aar of the file, however, support package management Library Reference Way, below, I say for everyone, how to release the Android library to use.

Pack Android Library

The Android library is packaged directly under the library project gradle build then, you'll see two *.aar in the Build/libs directory file, a debug pack, one is release under the use, see personal needs, here we use the release version of the. aar file.

The reference script is similar to the previously mentioned dependency library

dependencies {Compile (name: ' Pulltorefresh ', ext: ' AAR ')}

Finally, share a MAVEN library address: http://mvnrepository.com/


Android Studio Gradle Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.