您好,登錄后才能下訂單哦!
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
1.3、配置環境變量
GRADLE_HOME和path
1.4、驗證環境變量
打開 cmd, 輸入 gardle -v,如下環境配置成功
二、配置Android SDK
2.1、生成 build.gradle 配置文件:
打開eclipse,選擇主工程,右鍵 -->Ecport --> Generate Gradle build files ,選擇主工程 以及相關的工程,Finish。
2.2、配置 build-tools:
打開 build.gradle,查看屬性 buildToolsVersion,進入 sdk/build-tools/,選擇對應的版本。
進入sdk/extras/android/support/,把 multidex 文件夾復制到 sdk/build-tools/21.1.2/目錄下。
2.3、配置子工程 build.gradle 文件
1、 workspace配置:
打開 workspace,里面會有build.gradle 和 settings.gradle. 配置如下
build.gradle:
注意:gradle 版本對應的Android配置(2.2.1-----對應 0.14+,具體的對應可以查看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、每個子工程 build.gradle 配置:
首先,給每個子工程添加 local.properties 文件,文件內容:(sdk.dir=Android SDK路徑)
sdk.dir=D\:\\WorkInstall\\sdk
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優化
//zipAlignEnabledtrue
//移除無用的resource文件
//shrinkResourcestrue
//proguardFile getDefaultProguardFile('proguard-android.txt')
//加載默認混淆配置文件 progudard-android.txt在sdk目錄里面, //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時jar包重復)
同樣,以相同的方式把 android-support-v4.jar 添加進去(PS:這里出現了 duplicate entry: com/nineoldandroids/animation/Animator$AnimatorListener.class)
2.5 配置MultiDexApplication:
然后在Application中覆蓋方法attachBaseContext。
注:這里有三種方式: (android.support.multidex.MultiDexApplication)
a.在AndroidManifest.xml的application中聲明MultiDexApplication;
b.如果你已經有自己的Application類,讓其繼承MultiDexApplication;
c.如果你的Application類已經繼承自其它類,那么可以重寫attachBaseContext()方法:
根據自身情況選擇一種方式。
三、打包apk
完成之后,可以開始打包了。
cmd,進入工程所在的目錄,輸入:gradle build --stacktrace,最終Build successful為止。
打開apk包,可以看見.dex被分成了多。
五、常見問題:
以上步驟會出現一些問題,目前的解決方案如下:
5.1、SDK location not found. Define location with sdk.dir in thelocal.properties file or with an ANDROID_HOME environment variable.
請檢查所有工程里面是否包含了 local.properties,且 sdk.dir 是否配置正確
5.2、java.util.zip.ZipException: duplicate entry:xxxx.class。
一定是jar沖突了,可能是多個project中都引入了相同的jar,需要剔除掉重復的jar。
5.3、Execution failed for task ':magic-finger-china:packageRelease'.
這個是簽名文件不對,請檢查
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 文件,打開,并去掉所有的 -libraryjars libs/xxxx;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。