Software Requirements:
- 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
- Install Android Studio (if using Android Studio)
Create a project
- File-new Project
In Application Name field input Buildsystemexample
- 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
- Click File--new Module
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'
-
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>
Find android:text= "@string/button1"in this file, and then shortcut Alt+enter., as prompted, add value "Open" in string resource. LibActivity1 ".
Also found android:onclick= "onbutton1clicked", shortcut key alt+enter., as prompted, in mainactivity add onbutton1clicked Method.
- Copy the following code to onbutton1clicked method.
public void onbutton1clicked (view view) { Intent Intent = new Intent (this, libactivity1.class); startactivity (Intent); }
Shortcut key alt+enter., follow the prompts to import the individual error points.
Add dependency to the Libray module
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