您好,登录后才能下订单哦!
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。