溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

eclipse下使用MultiDex解決65536限制

發(fā)布時(shí)間:2020-06-10 18:38:08 來(lái)源:網(wǎng)絡(luò) 閱讀:7135 作者:破軍少帥 欄目:移動(dòng)開(kāi)發(fā)


eclipse下使用MultiDex解決65536限制

 

 

采用Android dex分包方案解決,步驟如下:

 

一、安裝gradle 

 

1.1、下載gradle-2.2.1-bin.zip (也可以使用最新的gradle版本)

         (https://services.gradle.org/distributions/gradle-2.2.1-bin.zip) 

1.2、解壓到安裝目錄 

         如:D:\WorkInstall\gradle-2.2.1

         eclipse下使用MultiDex解決65536限制

1.3、配置環(huán)境變量  

         GRADLE_HOMEpath

         eclipse下使用MultiDex解決65536限制

        

         eclipse下使用MultiDex解決65536限制

1.4、驗(yàn)證環(huán)境變量 

         打開(kāi) cmd, 輸入 gardle -v,如下環(huán)境配置成功

         eclipse下使用MultiDex解決65536限制

 

 

 

二、配置Android SDK

 

2.1、生成 build.gradle 配置文件:

         打開(kāi)eclipse,選擇主工程,右鍵 -->Ecport --> Generate Gradle build files ,選擇主工程    以及相關(guān)的工程,Finish

         eclipse下使用MultiDex解決65536限制

         eclipse下使用MultiDex解決65536限制

 

2.2、配置 build-tools

         打開(kāi) build.gradle,查看屬性 buildToolsVersion,進(jìn)入 sdk/build-tools/,選擇對(duì)應(yīng)的版本。

         eclipse下使用MultiDex解決65536限制

        

         進(jìn)入sdk/extras/android/support/,把 multidex 文件夾復(fù)制到 sdk/build-tools/21.1.2/錄下。

         eclipse下使用MultiDex解決65536限制

 

2.3、配置子工程 build.gradle 文件

         1、 workspace配置:

         打開(kāi) workspace,里面會(huì)有build.gradle settings.gradle. 配置如下

         build.gradle:

         注意:gradle 版本對(duì)應(yīng)的Android配置(2.2.1-----對(duì)應(yīng) 0.14+,具體的對(duì)應(yīng)可以查看google     文檔)。

         //Top-level build file where you can add configuration options common to all     sub-projects/modules.

         buildscript{

               repositories {

                mavenCentral()

               }

               dependencies {

         classpath'com.android.tools.build:gradle:0.14.+'

               }

         }

         settings.gradle:【主要是工程配置】

         include':google-play-services_lib'

         include':easy3d-myshare-core-magic'

         include':magic-finger-china'

         include':pulltorefresh-library'

         include':android-support-v7-recyclerview'

         include':ImageViewTextEdit-library'

         include':ImageViewZoom-library'

        

         2、每個(gè)子工程 build.gradle 配置:

         首先,給每個(gè)子工程添加 local.properties 文件,文件內(nèi)容:sdk.dir=Android SDK路徑)

         sdk.dir=D\:\\WorkInstall\\sdk

         eclipse下使用MultiDex解決65536限制

        

         build.gradle:(子工程的 build.gradle配置都一樣)

         applyplugin: 'android-library'

        

         dependencies{

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

         }

        

         android{

              compileSdkVersion 22

        buildToolsVersion"21.1.2"

           enforceUniquePackageName=false

        

           defaultConfig {

                   minSdkVersion14

                   targetSdkVersion19 

                  

                 //Enabling multidex support.

                   multiDexEnabledtrue

           }

        

           dexOptions {

                   preDexLibraries= false

           }

           lintOptions { 

                   abortOnErrorfalse 

        }

        

              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>/... whichwould

                 //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')

              }

         }

 

2.4、配置主工程 build.gradle 文件

        

         applyplugin: 'android'

         dependencies{

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

              compileproject(':android-support-v7-recyclerview')

              compile project(':easy3d-myshare-core-magic')

              compile project(':google-play-services_lib')

              compile project(':pulltorefresh-library')

              compile project(':ImageViewTextEdit-library')

   

              compile'com.android.support:support-v4:23.1.0'

           compile 'com.android.support:multidex:1.0.0'

          

           compile(project(':android-support-v7-recyclerview')){

                   excludemodule: 'support-v4'

           }

         }

 

         android{

              compileSdkVersion 22

              buildToolsVersion "21.1.2"

 

           packagingOptions {

             exclude 'META-INF/DEPENDENCIES.txt'

             exclude 'META-INF/LICENSE.txt'

             exclude 'META-INF/NOTICE.txt'

             exclude 'META-INF/NOTICE'

             exclude 'META-INF/LICENSE'

             exclude 'META-INF/DEPENDENCIES'

             exclude 'META-INF/notice.txt'

             exclude 'META-INF/MANIFEST.MF'

             exclude 'META-INF/license.txt'

             exclude 'META-INF/dependencies.txt'

           }

           configurations {

                     all*.exclude module: 'support-v4'

           }

           dexOptions {

                   preDexLibraries= false

           }

           lintOptions { 

                   abortOnErrorfalse 

        }

              compileOptions {

                 sourceCompatibilityJavaVersion.VERSION_1_7

                 targetCompatibilityJavaVersion.VERSION_1_7

              }

 

           defaultConfig {

                   minSdkVersion14

                   targetSdkVersion19 

                  

                 //Enabling multidex support.

                   multiDexEnabledtrue

           }

           // 打包的簽名信息(storeFile 簽名文件的路徑)

           signingConfigs {  

                myrelease {  

                               keyAlias'magic_finger.keystore'

                            keyPassword'magic_finger_20150724'

                            storeFilefile('magic_finger.keystore')

                            storePassword'magic_finger_20150724'

                   }

           }

        

           buildTypes {

                   release{

                            //混淆

                            minifyEnabledtrue

                            //Zipalign優(yōu)化

                               //zipAlignEnabledtrue

                               //移除無(wú)用的resource文件

                               //shrinkResourcestrue

                            //proguardFile getDefaultProguardFile('proguard-android.txt')

                            //加載默認(rèn)混淆配置文件 progudard-android.txtsdk目錄里面,                                     //proguard.cfg是我們自己配的混淆文件

            proguardFilesgetDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'

                            signingConfigsigningConfigs.myrelease                           

                 }

           }

        

     sourceSets {

        main {

            manifest.srcFile'AndroidManifest.xml'

            java.srcDirs = ['src']

            resources.srcDirs = ['src']

            aidl.srcDirs = ['src']

            renderscript.srcDirs = ['src']

            res.srcDirs = ['res']

            assets.srcDirs = ['assets']

              jniLibs.srcDirs= ['libs']

        }

 

        // Move the tests to tests/java,tests/res, etc...

        instrumentTest.setRoot('tests')

 

        // Move the build types tobuild-types/<type>

        // For instance,build-types/debug/java, build-types/debug/AndroidManifest.xml, ...

        // This moves them out of them defaultlocation under src/<type>/... which would

        // conflict with src/ being used by themain source set.

        // Adding new build types or productflavors should be accompanied

        // by a similar customization.

        debug.setRoot('build-types/debug')

        release.setRoot('build-types/release')

     }

        

         afterEvaluate{

                   tasks.matching{

                            it.name.startsWith('dex')

                   }.each{ dx ->

                            if(dx.additionalParameters == null) {

                                     dx.additionalParameters= ['--multi-dex']

                            }else{

                                     dx.additionalParameters+= '--multi-dex'

                            } 

                   }

           }

         }

 

2.4、添加 android-support-multidex.jar

         android-support-multidex.jar(sdk\extras\android\support\multidex\library\libs)引入到   工程中。 

         (注:最好放在referenced libraies中,而不是放在libs下,這樣避免build時(shí)jar包重復(fù)

         同樣,以相同的方式把 android-support-v4.jar 添加進(jìn)去(PS:這里出現(xiàn)了 duplicate entry: com/nineoldandroids/animation/Animator$AnimatorListener.class

         eclipse下使用MultiDex解決65536限制

 

2.5 配置MultiDexApplication

         然后在Application中覆蓋方法attachBaseContext。 

         注:這里有三種方式: android.support.multidex.MultiDexApplication

         a.AndroidManifest.xmlapplication中聲明MultiDexApplication;

         b.如果你已經(jīng)有自己的Application類,讓其繼承MultiDexApplication; 

     c.如果你的Application類已經(jīng)繼承自其它類,那么可以重寫(xiě)attachBaseContext()方法: 

         根據(jù)自身情況選擇一種方式。

 

 

三、打包apk

         完成之后,可以開(kāi)始打包了。

         cmd,進(jìn)入工程所在的目錄,輸入:gradle build --stacktrace,最終Build successful為止。

         打開(kāi)apk包,可以看見(jiàn).dex被分成了多。

         eclipse下使用MultiDex解決65536限制

         eclipse下使用MultiDex解決65536限制

 

五、常見(jiàn)問(wèn)題:

以上步驟會(huì)出現(xiàn)一些問(wèn)題,目前的解決方案如下:

5.1、SDK location not found. Define location with sdk.dir in thelocal.properties file or with an ANDROID_HOME environment variable.

         請(qǐng)檢查所有工程里面是否包含了 local.properties,且 sdk.dir 是否配置正確

 

5.2、java.util.zip.ZipException: duplicate entry:xxxx.class。

         一定是jar沖突了,可能是多個(gè)project中都引入了相同的jar,需要剔除掉重復(fù)的jar。

 

5.3、Execution failed for task ':magic-finger-china:packageRelease'.

         這個(gè)是簽名文件不對(duì),請(qǐng)檢查

         eclipse下使用MultiDex解決65536限制

 

 

5.4、Failed to shrink resources: java.util.zip.ZipException: invalidentry compressed

 size(expected 4017 but got 4081 bytes); ignoring

         去掉混淆文件里面 zippalignEnable true.

 

5.5、Error:Execution failed for task ':app:proguardDemoRelease'.

> java.io.IOException: The same input jar xxxx is specified twice.

         proguard.cfg 文件,打開(kāi),并去掉所有的 -libraryjars libs/xxxx;

 

 

 

 

 

 

 

 

 

 


附件:http://down.51cto.com/data/2366261
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI