Android Studio Tutorial (v): Gradle command to understand and import a third party package _android

Source: Internet
Author: User
Tags wrapper

The combination of Android Studio + Gradle is handy, and many third-party open source projects have migrated to Studio, and today is the way to view, compile, and import third-party open-source projects.

Sublime + Terminal compile and view the source code

First of all, to introduce a simple and personal favorite way. Many times we see a good open source project on the GitHub, generally have two kinds of requirements, read the source code and see the running effect, if it is pure view source I prefer to use some lightweight editor, such as Vim,sublime, Vim is not very skilled, So the individual is accustomed to using sublime to view and read the source code (actual development or Android Studio, Eclipse and other Ides more efficient); As for viewing the effect first need to apk, for some open source project has provided apk download, it is good to do, Directly installed to the mobile phone can be. For some that do not provide apk downloads, you need to manually compile the package yourself.

The following is an example of 9GAG to explain the process of compiling the command line Gradle.

1. Switch to the root directory of the 9GAG project, execute ./gradlew-v to view the Gradle version of the project

If you are going to download the Gradle for the first time, this process is very slow to flip the wall

Immediately following the download success you will see this message:

------------------------------------------------------------
gradle 2.2.1
----------------------------- -------------------------------build

time:2014-11-24 09:45:35 UTC build
number:none
Revision:  6FCB59C06F43A4E6B1BCB401F7686A8601A1FB4A

Groovy:  2.3.6
ant:   Apache Ant (TM) version 1.9.3 compiled On December 2013
JVM:   1.7.0_60 (Oracle Corporation 24.60-b09)
OS:   Mac os X 10.9.5 x86_64

2. Then execute ./gradlew Clean

Executing this command will download some of the Gradle dependencies, and when the download succeeds and compiles, you will see the following message:

: App:clean up-to-date
: Extras:ShimmerAndroid:clean up-to-date build

successful

3, the final implementation ./gradlew Build

This command compiles and generates the corresponding APK file directly, and if you see the following words, the build succeeds.

Build successful total

time:31.456 secs

And then in the 9gag/app/build/outputs/apk directory you'll see something like app-debug-unaligned.apk, App-release-unsigned.apk and so on, see the name should be able to understand the meaning, unaligned representative did not perform zip optimization, unsigned representative did not sign. You can then install APK directly to see how it works.

The above is my personal habit of reading the third party source code, about the above mentioned some of the Gradle commands you should not understand, the following will be explained.

Import Android Studio

But if you still want to import Android studio, here's a quick introduction to how to import studio and some caveats.

1, select File->import Project, select the directory of local 9GAG project

2, the first time will still download gradle, in fact, they have been downloaded in the command line, but this time still have to download (still want to turn over the wall), may be a bug

3, after the import you need to pay attention to the following places under each module under the build.gradle under the buildtoolsversion, that is, 9gag/app/build.gradle and 9gag/ Extras/shimmerandroid/build.gradle , you can open the SDK Manager to view the local SDK Build-toolsyou have installed, as shown in the following figure, If the appropriate version is not installed, please download first.

The version of the Gradle plug-in under build.gradle under the project root, such as the 9gag/build.gradle content:

Classpath ' com.android.tools.build:gradle:1.0.0 '

Android Studio 1.0 must specify the version of the Gradle plugin 1.0

The Gradle wrapper version, in the 9gag/gradle/wrapper/gralde-wrapper.properties file, has the following contents:

#Thu Dec 16:02:24 CST 2014
distributionbase=gradle_user_home
distributionpath=wrapper/dists
Zipstorebase=gradle_user_home
zipstorepath=wrapper/dists
distributionurl=https\://services.gradle.org/ Distributions/gradle-2.2.1-all.zip

The above content has developed Gradle version use 2.2.1, in studio if you manually changed the Gradle plug-in version will be prompted to update the Gradle wrapper version.

If the above is OK, then import Android Studio should be no problem, some old projects may be gradle version older, still using some old syntax, if the compilation is still not a reference to the Android Studio series four- The Gradle base changes to the latest syntax.

Gradle Common Commands

The above people have contacted some commands such as ./gradlew-v ./gradlew clean ./gradlewbuilds, note here is ./gradlew, /< /c4> on behalf of the current directory,Gradlew representative Gradle Wrapper, meaning a layer of gradle packaging, you can understand that the project locally on the package gradle, that Gradle wrapper, in 9gag/ the directory and version it points to are declared in the Gradle/wrapper/gralde-wrapper.properties file. As long as the download succeeds you can replace the Global gradle command with the Grdlew Wrapper command.

Understanding the concept of Gradle wrapper, the following common commands are easy to understand.

./gradlew-v Version Number

./gradlew clean clears the build folder under the 9gag/app directory

./gradlew build check dependencies and compile package

The note here is that ./gradlew Build command to call the debug, release environment of the package, if the official launch of the package can only be released, what should be done, here is a useful command **assemble * *, such as

./gradlew Assembledebug compile and play the debug package

./gradlew assemblerelease Compile and release packages

In addition, assemble can also be used in conjunction with productflavors, specifically in the next multi-channel packaging further explained.

./gradlew installrelease Release mode package and install

./gradlew Uninstallrelease Uninstall Release Mode pack

The next article introduces the convenient multi-channel packaging with Gradle, and the complete Gradle configuration in the project. There are questions or suggestions welcome to the direct Blog message.

Related Article

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.