Build Android Project with Gradle, androidgradle
Now with the integration of statistical tools such as umeng in projects, playing different APK packages based on different channels has become one of the daily work. Shenma? Are you still Manually changing the channel number to package one by one ?! Then you must be a very diligent programmer --! I tried to use Android Studio and learned a little about the building tool used by Android Studio: Gradle.
What is Gradle?
You can check the Gradle homepage.
In short, Gradle is an automated compilation and deployment test tool. Gradle has a wide range of content, and there are also dedicated books to introduce Gradle. Android Studio uses some of the Gradle functions.
Build. gradle
Build. gradle is the Gradle file used by Android Studio. The following is a simple example of build. gradle:
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.+' }}apply plugin: 'android'android { compileSdkVersion 17}
Buildscript {...} configures the compilation code, which basically does not need to be edited;
Apply plugin: 'android' indicates that the android plug-in is used;
Android {...} is the configuration of the android plug-in. For specific configuration content and values, see here
Build Task
In Gradle, tasks are used to represent a series of operations, which is somewhat similar to the Target in Makefile. Common Tasks include:
- Assemble
- Build
- Check
- Clean
Based on the name, you can understand the role of the Task, where build = assemble + check.
To execute a task, you can directly execute gradle [task] in the command line, for example, gradle assemble.
Gradle assumerelease
Build parameter configuration
Parameters in the Gradle script are DSL-compliant, that is, using a method similar to the Java package name:
a.b.c = 1a { b { c 1 }}
The preceding two methods indicate one meaning. Parameters that can be configured are not described here. Please go to http://tools.android.com/tech-docs/new-build-system/user-guideto view them.
The following describes the main parameters used.
Build Types
TODO
Signing Configurations
When generating an APK, one step is to sign the APK with a Key. The signature configuration items are in signingConfigs:
signingConfigs { myConfig { storeFile file("path-to-keystore") storePassword "******" keyAlias "******" keyPassword "******" }}
Then reference it in the compilation type:
buildTypes { release { ... signingConfig signingConfigs.myConfig }}
In this way, the signature configuration is fine. However, according to the above configuration, there is a problem: build. gradle is often submitted to the code management system, but it is not safe to put the key plaintext in the code base (especially if you are writing an open source project ). An alternative is to enter the password or store the password in the environment variable. See this link: http://stackoverflow.com/questio... k-file-using-gradle
... signingConfigs { release { storeFile file(System.getenv("KEYSTORE")) storePassword System.getenv("KEYSTORE_PASSWORD") keyAlias System.getenv("KEY_ALIAS") keyPassword System.getenv("KEY_PASSWORD") }}
Product flavors
If a project wants to generate different APK packages with different package names or resources, it is time to use Product flavors.
android { .... productFlavors { flavor1 { packageName "com.example.flavor1" versionCode 20 } flavor2 { packageName "com.example.flavor2" minSdkVersion 14 } }}
The preceding example generates two different APK packages with Different Package Names and attributes. For the attributes that can be set in Product flavors, see http://tools.android.com/tech-docs/new-build-system/user-guide.
Umeng multi-channel Packaging
After reading the above, let's take an example: Write a build. gradle to complete multi-channel packaging of umeng. Umeng channel differentiation is achieved through <meta-data> In AndroidManifest. xml:
<meta-data android:value="UMENG_CHANNEL_VALUE" android:name="UMENG_CHANNEL"/>
The basic idea is to replace the strings in AndroidManifest. xml in the package to create different APK packages. The key parts of build. gradle are as follows:
Android {compileSdkVersion 19 buildToolsVersion '19. 0.0 'defaultconfig {minSdkVersion 7 targetSdkVersion 19} // signature information, all of which contains the signingConfigs {release {storeFile file (System. getenv ("KEYSTORE") storePassword System. getenv ("KEYSTORE_PASSWORD") keyAlias System. getenv ("KEY_ALIAS") keyPassword System. getenv ("KEYSTORE_PASSWORD") }}buildtypes {release {runProguard true proguardFile getdefaproproguardfile('proguard-android-optimize.txt ') signingConfig signingConfigs. release // use the above signature information} productFlavors {defaultFlavor {proguardFile 'proguard-rules.txt '} // The code below for different versions of xiaomi {} LeTv {}}}// is excerpted from
After modifying build. gradle, you must use the sync Project with gradle files function of Android Studio to synchronize the Project settings. Then enter gradle build in Terminal of Android Studio (gradlew build for windows). Wait a moment and all the generated APK will be under the build/apk of your project!
Android studio cannot build Gradle projects info for several hours when creating a project.
The solution is fan qiang.
The server developed by android is blocked by the firewall.
The project created by android studio remains in building gradle project info.
There is no development tool worse than it. With eclipse, why should google develop such a bad tool? The development is fine. Why should it be released before it is complete, if it is not perfect, it will be released. Why is it so bad after such a long release ?! The first time a project is created, it cannot be opened when the layout file is loaded. After the sdk is set, it will die. After the upgrade, a new project will be created, and the interface will be stuck. It turns out that there are so many predecessors who have encountered the same problem, the ridiculous studio ......