Android Studio Gradle Build

Source: Internet
Author: User

Software Requirements:

    1. Installing Gradle
      • Download the Gradle compressed package and unzip it locally.
      • Set environment variables, create new system variables, gradle_home:e:\installspace\gradle-1.11
      • Increase in path;%gradle_home%\bin
      • Test succeeded, command: gradle-v
    2. Install Android Studio (if using Android Studio)
      • Download: http://developer.android.com/sdk/installing/studio.html, run the installation

Create a project

    1. File-new Project
    2. In Application Name field input Buildsystemexample

    3. Keep the default, always next until finish.

Project Structure The Android Studio project contains a default app module

Component

Location

Source files

app/src/main/java/<package>/

Resource files

app/src/main/res/

Manifest file

App/src/main/androidmanifest.xml

Build file

App/build.gradle

New Library Modile

    1. Click File--new Module
    2. Select the Android Libraryin the Windows window and next 3. Leave the default unchanged, always next, enter LibActivity1 in the Activity Name field, and then finish.

This project contains two modules and each module contains an activity.

open activity from a library module In this example, the mainactivity in the app module will open the Lib module's LibActivity1'

  1. Open the Love App module's mainactivity, find the App/src/main/res/layout/activity_main.xml, replace the code in the file, as follows

    <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"android:layout_height= "Match_parent"tools:context= "Com.buildsystemexample.app.MainActivity" ><buttonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"android:layout_height= "Wrap_content"android:text= "@string/button1"android:onclick= "onbutton1clicked"/></LinearLayout>
  2. Find android:text= "@string/button1"in this file, and then shortcut Alt+enter., as prompted, add value "Open" in string resource. LibActivity1 ".

  3. Also found android:onclick= "onbutton1clicked", shortcut key alt+enter., as prompted, in mainactivity add onbutton1clicked Method.

  4. Copy the following code to onbutton1clicked method.
    public void onbutton1clicked (view view) {    Intent Intent = new Intent (this, libactivity1.class);     startactivity (Intent);  }
  5. Shortcut key alt+enter., follow the prompts to import the individual error points.

Add dependency to the Libray module

    • Open the App/build.gradle and add the following dependencies.
      ...  Dependencies {    ...     Compile project (": Lib")}

Building projects

Click Build--MakeProject

You can see if it was successful by looking at the Gradle Console in the right part of the Windows window.

If you want to see the available build tasks, you can click on the Gradlewith windows in the IDE and double-click any of the build tasks to run. The APK will be generated in app/build/apk.

Command line Gradle Build

On Windows

> Gradlew.bat assembledebug

On Mac OS and Linux platforms, type these commands:

$ chmod +x gradlew$./gradlew Assembledebug

To view a series of build tasks, the command is as follows

$./gradlew Tasks

Build Release version

    • On the right side of the IDE, click Gradle, expand Buildsystemexample. Then in the expand: app, double-click Assemblerelease.

Configuration Build

Basic structure

Apply plugin: ' Android 'Android {Compilesdkversion 19Buildtoolsversion "19.0.0"Defaultconfig {Minsdkversion 8Targetsdkversion 19Versioncode 1Versionname "1.0"}Signingconfigs {//To sign the APKRelease {StoreFile file ("Myreleasekey.keystore")Storepassword "Password"Keyalias "Myreleasekey"Keypassword "Password"}}Buildtypes {//Obfuscation code, Proguard-android.txt is the Android SDK comes with, Proguard-rules.txt is the newRelease {Runproguard trueProguardfiles getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.txt 'Signingconfig signingconfigs.release//Signature Configuration}}}dependencies {"Declaration of dependencies" 3 waysModule DependencyCompile project (": Lib") Remote binary DependencyCompile ' com.android.support:appcompat-v7:19.0.1 'Local binary DependencyCompile Filetree (dir: ' Libs ', include: [' *.jar '])}

Reference: Http://developer.android.com/sdk/installing/studio-build.html#configBuild

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.