├── LibModule ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ └── strings.xml │ │ │ ├── drawable-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ └── drawable-xxhdpi │ │ │ │ └── ic_launcher.png │ │ └── AndroidManifest.xml │ └── androidTest │ │ └── java │ │ └── com │ │ └── github │ │ └── example │ │ └── libmodule │ │ └── lib │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── app ├── ic_launcher-web.png ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ ├── values-v11 │ │ └── styles.xml │ ├── values-w820dp │ │ └── dimens.xml │ ├── values-v14 │ │ └── styles.xml │ ├── menu │ │ └── main.xml │ └── layout │ │ └── activity_main.xml ├── AndroidManifest.xml ├── src │ └── com │ │ └── github │ │ └── ShinChven │ │ └── migratetogradle │ │ └── MainActivity.java ├── build.gradle └── proguard-project.txt ├── screenshots ├── Image_1.png ├── Image_2.png ├── Image_3.png ├── Image_4.png ├── Image_5.png ├── Image_6.png ├── Image_7.png ├── Image_8.png ├── gradle_0.png ├── gradle_1.png ├── gradle_2.png ├── gradle_3.png └── gradle_4.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── LICENSE.TXT └── README.md /LibModule/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':LibModule' 2 | 3 | -------------------------------------------------------------------------------- /app/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/app/ic_launcher-web.png -------------------------------------------------------------------------------- /screenshots/Image_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/Image_1.png -------------------------------------------------------------------------------- /screenshots/Image_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/Image_2.png -------------------------------------------------------------------------------- /screenshots/Image_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/Image_3.png -------------------------------------------------------------------------------- /screenshots/Image_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/Image_4.png -------------------------------------------------------------------------------- /screenshots/Image_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/Image_5.png -------------------------------------------------------------------------------- /screenshots/Image_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/Image_6.png -------------------------------------------------------------------------------- /screenshots/Image_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/Image_7.png -------------------------------------------------------------------------------- /screenshots/Image_8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/Image_8.png -------------------------------------------------------------------------------- /screenshots/gradle_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/gradle_0.png -------------------------------------------------------------------------------- /screenshots/gradle_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/gradle_1.png -------------------------------------------------------------------------------- /screenshots/gradle_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/gradle_2.png -------------------------------------------------------------------------------- /screenshots/gradle_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/gradle_3.png -------------------------------------------------------------------------------- /screenshots/gradle_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/screenshots/gradle_4.png -------------------------------------------------------------------------------- /LibModule/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | LibModule 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/app/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/app/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/app/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/app/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /LibModule/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/LibModule/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /LibModule/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/LibModule/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /LibModule/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/LibModule/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /LibModule/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShinChven/MigrateToGradle/HEAD/LibModule/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 16dp 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MigrateToGradle 5 | Hello world! 6 | Settings 7 | 8 | 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Dec 26 09:09:19 CST 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /app/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LibModule/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 64dp 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /LibModule/src/androidTest/java/com/github/example/libmodule/lib/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.github.example.libmodule.lib; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .gitignore support plugin (hsz.mobi) 2 | ### Android template 3 | # Built application files 4 | *.apk 5 | *.ap_ 6 | 7 | # Files for the Dalvik VM 8 | *.dex 9 | 10 | # Java class files 11 | *.class 12 | 13 | # Generated files 14 | bin/ 15 | gen/ 16 | 17 | # Gradle files 18 | .gradle/ 19 | build/ 20 | gradlew 21 | gradlew.bat 22 | 23 | # Local configuration file (sdk path, etc) 24 | local.properties 25 | 26 | # Proguard folder generated by Eclipse 27 | proguard/ 28 | 29 | # Log Files 30 | *.log 31 | 32 | # IDEA Files 33 | .idea/ 34 | *.iml 35 | -------------------------------------------------------------------------------- /LICENSE.TXT: -------------------------------------------------------------------------------- 1 | MigrateToGradle Example Copyright [2014] ShinChven@gmail.com 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /LibModule/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:/Runtime/android_sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /LibModule/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | repositories { 4 | jcenter() 5 | } 6 | 7 | android { 8 | compileSdkVersion 22 9 | buildToolsVersion "22.0.1" 10 | 11 | defaultConfig { 12 | minSdkVersion 8 13 | targetSdkVersion 22 14 | versionCode 1 15 | versionName "1.0" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | compile 'com.android.support:appcompat-v7:21.0.3' 28 | compile 'com.android.support:support-v4:21.0.0' 29 | } 30 | -------------------------------------------------------------------------------- /app/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /app/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/com/github/ShinChven/migratetogradle/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ShinChven.migratetogradle; 2 | 3 | import android.support.v7.app.ActionBarActivity; 4 | import android.os.Bundle; 5 | import android.view.Menu; 6 | import android.view.MenuItem; 7 | 8 | 9 | public class MainActivity extends ActionBarActivity { 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_main); 15 | } 16 | 17 | 18 | @Override 19 | public boolean onCreateOptionsMenu(Menu menu) { 20 | // Inflate the menu; this adds items to the action bar if it is present. 21 | getMenuInflater().inflate(R.menu.main, menu); 22 | return true; 23 | } 24 | 25 | @Override 26 | public boolean onOptionsItemSelected(MenuItem item) { 27 | // Handle action bar item clicks here. The action bar will 28 | // automatically handle clicks on the Home/Up button, so long 29 | // as you specify a parent activity in AndroidManifest.xml. 30 | int id = item.getItemId(); 31 | if (id == R.id.action_settings) { 32 | return true; 33 | } 34 | return super.onOptionsItemSelected(item); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | repositories { 4 | jcenter() 5 | } 6 | 7 | android { 8 | 9 | compileSdkVersion 22 10 | buildToolsVersion "22.0.1" 11 | 12 | 13 | defaultConfig { 14 | applicationId "com.github.ShinChven.migratetogradle" 15 | minSdkVersion 9 16 | targetSdkVersion 22 17 | versionCode 1 18 | versionName "1.0" 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false // proGuard 现在改成了 minifyEnabled 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 25 | proguardFiles 'proguard-project.txt' 26 | } 27 | debug { 28 | 29 | } 30 | } 31 | 32 | compileOptions { 33 | sourceCompatibility JavaVersion.VERSION_1_7 34 | targetCompatibility JavaVersion.VERSION_1_7 35 | } 36 | 37 | sourceSets { // 工程结构配置,如果你使用的是标准gradle 工程结构就需要把sourceSets 去掉 38 | main { 39 | java.srcDirs = ['src'] 40 | aidl.srcDirs = ['src'] 41 | renderscript.srcDirs = ['src'] 42 | res.srcDirs = ['res'] 43 | assets.srcDirs = ['assets'] 44 | jniLibs.srcDirs = ['libs'] // 配置此处才会打包jni 的.so 文件 45 | jni.srcDirs=['jni'] 46 | manifest.srcFile 'AndroidManifest.xml' 47 | } 48 | } 49 | } 50 | 51 | /** 52 | * https://gradle.org/docs/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html 53 | */ 54 | dependencies { 55 | compile fileTree(dir: 'libs', include: ['*.jar']) 56 | compile 'com.android.support:appcompat-v7:21.0.0' 57 | compile 'com.android.support:support-v4:21.0.0' 58 | compile(project(':LibModule')) // 包含module 59 | } 60 | -------------------------------------------------------------------------------- /app/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:/adt-bundle-x64/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the ProGuard 5 | # include property in project.properties. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | 20 | # This is a configuration file for ProGuard. 21 | # http://proguard.sourceforge.net/index.html#manual/usage.html 22 | 23 | -dontusemixedcaseclassnames 24 | -dontskipnonpubliclibraryclasses 25 | -verbose 26 | 27 | # Optimization is turned off by default. Dex does not like code run 28 | # through the ProGuard optimize and preverify steps (and performs some 29 | # of these optimizations on its own). 30 | -dontoptimize 31 | -dontpreverify 32 | # Note that if you want to enable optimization, you cannot just 33 | # include optimization flags in your own project configuration file; 34 | # instead you will need to point to the 35 | # "proguard-android-optimize.txt" file instead of this one from your 36 | # project.properties file. 37 | 38 | -keepattributes *Annotation* 39 | -keep public class com.google.vending.licensing.ILicensingService 40 | -keep public class com.android.vending.licensing.ILicensingService 41 | 42 | #Keep classes that are referenced on the AndroidManifest 43 | #-keep public class * extends android.app.Activity 44 | #-keep public class * extends android.app.Application 45 | #-keep public class * extends android.app.Service 46 | #-keep public class * extends android.content.BroadcastReceiver 47 | #-keep public class * extends android.content.ContentProvider 48 | #-keep public class com.android.vending.licensing.ILicensingService 49 | 50 | # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native 51 | -keepclasseswithmembernames class * { 52 | native ; 53 | } 54 | 55 | # keep setters in Views so that animations can still work. 56 | # see http://proguard.sourceforge.net/manual/examples.html#beans 57 | -keepclassmembers public class * extends android.view.View { 58 | void set*(***); 59 | *** get*(); 60 | } 61 | 62 | # We want to keep methods in Activity that could be used in the XML attribute onClick 63 | -keepclassmembers class * extends android.app.Activity { 64 | public void *(android.view.View); 65 | } 66 | 67 | # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations 68 | -keepclassmembers enum * { 69 | public static **[] values(); 70 | public static ** valueOf(java.lang.String); 71 | } 72 | 73 | -keep class * implements android.os.Parcelable { 74 | public static final android.os.Parcelable$Creator *; 75 | } 76 | 77 | -keepclassmembers class **.R$* { 78 | public static ; 79 | } 80 | 81 | 82 | 83 | # The support library contains references to newer platform versions. 84 | # Don't warn about those in case this app is linking against an older 85 | # platform version. We know about them, and they are safe. 86 | -dontwarn android.support.** 87 | -keep class android.support.**{ *; } 88 | 89 | -dontwarn android.test.** 90 | -keep class android.test.**{ *; } 91 | 92 | -dontwarn com.github.** 93 | -keep class com.github.**{ *; } 94 | 95 | 96 | # -libraryjars /libs/json-lib-2.2.3-jdk13.jar 97 | # -libraryjars /libs/xUtils-2.4.7.jar 98 | 99 | 100 | 101 | 102 | -dontskipnonpubliclibraryclassmembers 103 | 104 | -dontskipnonpubliclibraryclasses 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Migrate To Gradle 手动迁移ADT 的ANT结构工程至Gradle 2 | 3 | > ⚠️ **Deprecated:** 4 | > 这个文档很老了,建议去看官方文档。 5 | 6 | ## 前言 7 | 8 | 现在Android Studio 已经发布正式版,其带来的新的功能对于原来的eclipse 用户是非常友好的,只需要指定目录就能自动将代码导入并配置好相关的Gradle 脚本。 9 | 我们可以用正式版的AS中的可视化插件轻松的将旧工程升级到Gradle 构建系统下,而这个项目旨在向大家介绍Gradle 的Android 工程的结构和常用配置。 10 | 11 | ## 工具 12 | 13 | 1. Android Studio 14 | 2. Intellij IDEA 15 | 16 | ## 了解Gradle 17 | 18 | Gradle 以module 来管理project,在Gradle 构建的Gradle project中通常包含application module(com.android.application),与library module(com.android.library)两种module。 19 | 20 | 在Gradle 的project 中需要使用,基本上全都使用.gradle 文件来配置,是一个脚本化的工程构建,而非原先ADT中那种eclipse 的或视化构建。 21 | 22 | ## 迁移工程 23 | 1. 创建一个文件夹来放你的工程,比如 Migrate-to-Gradle。 24 | 25 | 2. Wrapper:你需要Gradle 的Wrapper 来下载和管理当前项目使用的Gradle 的版本,当你的环境中没有配置Gradle 时它可以自动下载Gradle 并配置到你的环境中去。 26 | 1. 如果你在天朝,那么配置Gradle 的时间可能会稍长,所以我一般都是直接从Android Studio 新建的工程中拷贝Wrapper 出来使用,以避免重复配置不同版本的Gradle。 27 | 2. 而如果你不想使用工具中的版,你还可以进行其它配置,见下一点。 28 | 29 | 3. 在文件夹中建一个app (或者其它什么名字)文件夹来存放你的application module,请将你原先的工程文件拷贝到app 文件夹中去。然后你还需要一个build.gradle 和settings.gradle文件。看起来如下图: 30 | 31 | ![Screenshot](https://raw.githubusercontent.com/ShinChven/ImmigrateToGradle/master/screenshots/gradle_1.png) 32 | 33 | 两个文件的配置如下: 34 | 35 | build.gradle -- 根目录的build.gradle 文件一般用来配置整个工程 36 | 37 | ``` groovy 38 | buildscript { 39 | repositories { 40 | jcenter() 41 | } 42 | dependencies { 43 | classpath 'com.android.tools.build:gradle:0.14.2' 44 | } 45 | } 46 | 47 | allprojects { 48 | repositories { 49 | jcenter() 50 | } 51 | } 52 | 53 | // 如果你想配置你自己制定的Gradle 版本,加入以下配置,然后在导入工程时选择use customizable gradle wrapper 54 | task wrapper(type: Wrapper) { 55 | gradleVersion = '2.1' 56 | } 57 | ``` 58 | 59 | settings.gradle -- 根目录的settings.gradle 文件用来制定哪个文件夹为module,以“:”符号给目录分级 60 | 61 | ``` groovy 62 | include ':app' // 根目录下的一级目录 63 | //include ':libs:module0' // 根目录下的二级目录,如果你需要这个module 的话 64 | ``` 65 | 66 | 4. 配置你的app module:在其中加入build.gradle,具体配置如下: 67 | 68 | ``` groovy 69 | buildscript { 70 | repositories { 71 | jcenter() //你所使用的仓库 72 | } 73 | dependencies { 74 | classpath 'com.android.tools.build:gradle:0.14.2' // Gradle 的Android 插件版本 75 | } 76 | } 77 | apply plugin: 'com.android.application' // 导入Android Application 插件,将此module 配置成application module 78 | 79 | repositories { 80 | jcenter() // 仓库 81 | } 82 | 83 | android { 84 | 85 | compileSdkVersion 21 // 使用SDK的版本,请配置你SDK中有的最新版本 86 | buildToolsVersion "21.1.1" // buildTools 版本,你SDK中有哪个版本配哪个版本,建议更新到最新的版本 87 | 88 | 89 | defaultConfig { 90 | applicationId "com.github.ShinChven.migratetogradle" // 原来的包名,现在叫applicationId 91 | minSdkVersion 9 92 | targetSdkVersion 21 93 | versionCode 1 94 | versionName "1.0" 95 | } 96 | 97 | buildTypes { // 配置打包的版本 98 | release { // 发行版 99 | runProguard false // 是否混淆 100 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' // 默认混淆文件 101 | proguardFiles 'proguard-project.txt' // 自定义混淆文件 102 | } 103 | debug { // debug 版 104 | 105 | } 106 | } 107 | 108 | sourceSets { // 如果你的工程是从ANT 中迁移过来,可以使用sourceSets 来配置工程结构,如果你使用的是标准Gradle 结构,可以不需要配置。 109 | main { 110 | java.srcDirs = ['src'] 111 | aidl.srcDirs = ['src'] 112 | renderscript.srcDirs = ['src'] 113 | res.srcDirs = ['res'] 114 | assets.srcDirs = ['assets'] 115 | jniLibs.srcDirs = ['libs'] // 配置此处才会打包jni 的.so 文件 116 | jni.srcDirs=['jni'] 117 | manifest.srcFile 'AndroidManifest.xml' 118 | } 119 | } 120 | } 121 | 122 | /** 123 | * https://gradle.org/docs/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html 124 | */ 125 | dependencies { 126 | compile fileTree(dir: 'libs', include: ['*.jar']) 127 | compile 'com.android.support:appcompat-v7:21.0.0' 128 | compile 'com.android.support:support-v4:21.0.0' 129 | compile(project(':LibModule')) // 包含module 130 | } 131 | ``` 132 | 133 | 完成这些你的工程就会看起来像这样: 134 | 135 | ![Screenshot](https://raw.githubusercontent.com/ShinChven/ImmigrateToGradle/master/screenshots/gradle_0.png) 136 | 137 | ## 运行工程 138 | 139 | 1. open project 140 | 141 | ![Screenshot](https://raw.githubusercontent.com/ShinChven/ImmigrateToGradle/master/screenshots/Image_3.png) 142 | 143 | 2. 选中build.gradle 文件 144 | 145 | ![Screenshot](https://raw.githubusercontent.com/ShinChven/ImmigrateToGradle/master/screenshots/gradle_4.png) 146 | 147 | 3. 导入: 148 | 149 | project 中包含Wrapper 选Use default gradle wrpper 150 | 151 | 在project 的task 配置来自动配置gradle 选Use customizable gradle wrapper 152 | 153 | Use local Gradle distribution 为使用系统变量中配置的gradle 154 | 155 | ![Screenshot](https://raw.githubusercontent.com/ShinChven/ImmigrateToGradle/master/screenshots/gradle_3.png) 156 | 157 | 4. 如果你是第一次运行Android Studio ,那么请先感谢郭嘉,然后等待配置完成 158 | 159 | ## jar包去重 160 | 161 | 如果你运气不好遇到了下图,说明你的工程中包含的v4 包或者其它什么包在你的dependencies 配置中出现了重复引用的冲突,你需要去重: 162 | 163 | ![Screenshot](https://raw.githubusercontent.com/ShinChven/ImmigrateToGradle/master/screenshots/gradle_2.png) 164 | 165 | 如果使用Gradle 构建工程,建议将libs 文件夹中的jar 包都转换成gradle dependencies(依赖),而不是直接存放在文件夹中。两者不可同时存在,否则因为包名、文件重复则报错。 166 | 167 | 例如,android-support-v4.jar 你可以在build.Gradle 中这样配置 168 | 169 | ``` groovy 170 | dependencies { // 依赖配置 171 | compile 'com.android.support:support-v4:21.0.0' 172 | } 173 | ``` 174 | 175 | ## 去除冲突的依赖 176 | 177 | 你所添加的那些依赖(dependencies)中的项目的可能会引用同一个项目,却是不同版本,在build 的时候可能会出错,如果要去除冲突,配置如下 178 | ``` groovy 179 | compile ('com.github.xxx:1.0'){ 180 | exclude module: 'appcompat-v7' 181 | } 182 | ``` 183 | 184 | ## build.Gradle 另外的一些配置,Good luck. 185 | 186 | 配置中的版本号,请根据最新sdk 中的版本进行配置 187 | ``` groovy 188 | bBuildscript { // 这段配置如果放入project 级的build.gradle 中则可以在module 中省掉。 189 | repositories { 190 | jcenter() 191 | } 192 | dependencies { // 加入android build tools 193 | classpath 'com.android.tools.build:gradle:1.1.1' 194 | } 195 | } 196 | apply plugin: 'com.android.application' 197 | 198 | repositories { // 配置仓库 199 | jcenter() 200 | } 201 | 202 | android { // android 工程配置 203 | compileSdkVersion 22 204 | buildToolsVersion "22.0.1" 205 | 206 | signingConfigs { // 签名,你可以配置多个签名,然后再在buildTypes 进行指定。 207 | release { 208 | storeFile file("keystore\\key.jks") // 签名文件存放路径 209 | storePassword "your.password" 210 | keyAlias "your.alias" 211 | keyPassword "password" 212 | } 213 | } 214 | 215 | packagingOptions { // 打包配置 216 | exclude 'META-INF/LICENSE' // 排除一些文件 217 | exclude 'META-INF/NOTICE' 218 | exclude 'META-INF/LICENSE.txt' 219 | exclude 'META-INF/NOTICE.txt' 220 | } 221 | 222 | lintOptions { // 打包时,也不因为出错中断。 223 | checkReleaseBuilds false 224 | abortOnError false 225 | } 226 | 227 | defaultConfig { // SDK 和版本号配置 228 | minSdkVersion 8 229 | targetSdkVersion 21 230 | versionCode 1 231 | versionName "1.0" 232 | } 233 | buildTypes { // 打包版本配置 234 | release { 235 | signingConfig signingConfigs.release // 指定签名配置 236 | runProguard false // 是否进行Proguard 237 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 238 | } 239 | debug { 240 | // signingConfig signingConfigs.release // 如果你的debug版本也需要签名,请将这一行配置解开注释 241 | } 242 | } 243 | 244 | sourceSets { // 工程结构配置 245 | main { 246 | java.srcDirs = ['src'] 247 | aidl.srcDirs = ['src'] 248 | renderscript.srcDirs = ['src'] 249 | res.srcDirs = ['res'] 250 | assets.srcDirs = ['assets'] 251 | jniLibs.srcDirs = ['libs'] // 配置此处才会打包jni 的.so 文件 252 | manifest.srcFile 'AndroidManifest.xml' 253 | } 254 | } 255 | } 256 | 257 | dependencies { // 依赖配置 258 | compile fileTree(dir: 'libs', include: ['*.jar']) // 整合所有libs 文件夹中的jar 包 259 | compile 'com.android.support:support-v4:21.0.0' 260 | compile 'com.android.support:gridlayout-v7:21.0.0' 261 | compile 'com.android.support:appcompat-v7:20.0.0' 262 | compile 'com.loopj.android:android-async-http:1.4.5' // Maven 中存在的项目 263 | compile('com.fasterxml.jackson.core:jackson-databind:2.4.1.3') { 264 | // 需要配置 packagingOptions 'META-INF/LICENSE' NOTICE 265 | transitive = true 266 | } 267 | } 268 | 269 | apply plugin: 'idea' // 加载idea 插件 270 | 271 | idea { 272 | module { 273 | downloadJavadoc = true // 下载依赖项目的文档 274 | downloadSources = true // 下载依赖项目的源码 275 | } 276 | } 277 | ``` 278 | 279 | ## library modue build.gradle 文件示例 280 | 281 | 请注意,以下的配置的某些版本号已经过时,请根据自己的sd配置最新的版本号 282 | 283 | ``` groovy 284 | 285 | apply plugin: 'com.android.library' // 配置为library 286 | 287 | repositories { 288 | jcenter() 289 | } 290 | 291 | android { 292 | compileSdkVersion 21 293 | buildToolsVersion "21.1.1" 294 | 295 | defaultConfig { 296 | minSdkVersion 9 297 | targetSdkVersion 21 298 | } 299 | sourceSets { // 工程结构配置 300 | main { 301 | java.srcDirs = ['src'] 302 | aidl.srcDirs = ['src'] 303 | renderscript.srcDirs = ['src'] 304 | res.srcDirs = ['res'] 305 | assets.srcDirs = ['assets'] 306 | jniLibs.srcDirs = ['libs'] // 配置此处才会打包jni 的.so 文件 307 | manifest.srcFile 'AndroidManifest.xml' 308 | } 309 | } 310 | } 311 | 312 | dependencies { 313 | compile fileTree(dir: 'libs', include: ['*.jar']) 314 | compile 'com.android.support:appcompat-v7:21.0.0' 315 | compile 'com.android.support:support-v4:21.0.0' 316 | } 317 | ``` 318 | 319 | ## local.properties文件 320 | 321 | 在OS X 系统中,你的工程需要这个文件来告诉工具的gradle 插件,你的sdk在哪。如果不进行配置的话将不能使用gradle 插件中的clean build 等方便的功能。 322 | 323 | ``` bash 324 | echo sdk.dir=$ANDROID_HOME >local.properties 325 | ``` 326 | 327 | --------------------------------------------------------------------------------