Gradle Android 工程开启 multiDex

Posted zhangjianying

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gradle Android 工程开启 multiDex相关的知识,希望对你有一定的参考价值。

环境  Gradle 2.2 (如果版本不同.下面的脚本是需要调整 com.android.tools.build:gradle 这个版本号的)


build.gradle  (注意标红的地方)

buildscript 
    repositories 
        mavenCentral()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:0.14.+'
    
	tasks.withType(JavaCompile) 
		options.encoding = "UTF-8"
	

apply plugin: 'android'

dependencies 
    compile fileTree(dir: 'libs', include: '*.jar')


android 
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

	lintOptions 
		abortOnError false
	
	
	compileOptions 
		sourceCompatibility JavaVersion.VERSION_1_6
		targetCompatibility JavaVersion.VERSION_1_6
	
	
	defaultConfig 
		minSdkVersion 14
		targetSdkVersion 21
		multiDexEnabled = true
	
	
    sourceSets 
        main 
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    
	
	
	signingConfigs 
		release 
			storeFile file("TalkwebMBI.keystore")
			storePassword "***"
			keyAlias "***"
			keyPassword "***"
		
	
	
	dexOptions 
		preDexLibraries = false
	 
	
	afterEvaluate 
		tasks.matching 
		   it.name.startsWith('dex')
		.each  dx ->
		   if (dx.additionalParameters == null) 
			  dx.additionalParameters = ['--multi-dex']
		    else 
			  dx.additionalParameters += '--multi-dex'
		   
		
	 
	
	//声明此发布构建在签名之前需要运行proguard
	buildTypes 
		release 
			minifyEnabled true
			proguardFile getDefaultProguardFile('proguard-android.txt')
			proguardFile 'proguard.cfg'
			signingConfig signingConfigs.release
		
	




proguard.cfg

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
#-dontoptimize
-dontpreverify
#-dontshrink

# If you want to enable optimization, you should include the
# following:
#-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
#-optimizationpasses 5
#-allowaccessmodification
#-dontpreverify

#
# Note that you cannot just include these flags in your own
# configuration file; if you are including this file, optimization
# will be turned off. You'll need to either edit this file, or
# duplicate the contents of this file and remove the include of this
# file from your project's proguard.config path property.
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes *Annotation*
-keep @interface *
-keep enum *  *; 

# Android:
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgent
-keep public class * extends android.preference.Preference
#-keep public class * extends android.support.v4.app.Fragment
#-keep public class * extends android.app.Fragment
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * 
    native <methods>;


-keep public class * extends android.view.View 
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);


-keepclasseswithmembers class * 
    public <init>(android.content.Context, android.util.AttributeSet);


-keepclasseswithmembers class * 
    public <init>(android.content.Context, android.util.AttributeSet, int);


-keepclassmembers class * extends android.app.Activity 
   public void *(android.view.View);


# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * 
    public static **[] values();
    public static ** valueOf(java.lang.String);


-keep class * implements android.os.Parcelable 
  public static final android.os.Parcelable$Creator *;
  <init>(android.os.Parcel);


-keepclassmembers class **.R$* 
    public static <fields>;


# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**
-dontwarn javax.xml.**

-keep class * extends java.util.ListResourceBundle  protected Object[][] getContents(); 

#------------LIBS--------------------
-keep class android.support.**  public *; 


以上是关于Gradle Android 工程开启 multiDex的主要内容,如果未能解决你的问题,请参考以下文章

Android Gradle 插件Gradle 构建机制 ① ( 空白工程 Gradle 构建文件 | IntelliJ IDEA 工程构建文件 | Android Studio 工程构建文件 )

Android Gradle 插件Android Gradle 工程结构简介 ( Gradle 默认输出目录 | Gradle 配置目录 | gradlew 可执行文件 )

《Gradle权威指南》--自定义Android Gradle工程

Android Gradle 插件Gradle 构建机制 ⑤ ( 在 Android Studio 中查看 Android Gradle 插件源码 )

Android Gradle 插件Android Studio 工程 Gradle 构建流程 ② ( settings.gradle 构建脚本分析 | 根目录下 build.gradle 分析 )

Android Studio的settings.gradle文件