【问题标题】:Android support multidex library implementationAndroid支持multidex库实现
【发布时间】:2014-11-14 07:47:33
【问题描述】:

我已经达到了魔法 dex 限制,因为我的应用程序使用了很多 jars(驱动 API、greendao、文本到 pdf、支持..)。

我目前的解决方案是,我真的为我从主 apk 调用的谷歌驱动器创建了第二个 apk。但现在我发现android终于用这个库支持这个了。我的问题只是我不知道如何实现它(最好没有 gradle)。我找不到任何好的教程。

好吧,我想实现这个我疯了...我找到了this

我补充说:

 android:name="android.support.multidex.MultiDexApplication"

到我的清单文件和

protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
}

到我的 mainactivity.java

还为eclipse安装了gradle插件,导出gradle得到build.gradle文件,我改成:

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':android-support-v7-appcompat')
    compile project(':Sync')
    compile project(':gdrive:google-play-services_lib')
}


android {
    compileSdkVersion 14
    buildToolsVersion "21.1.1"


    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src-gen','src']
            resources.srcDirs = ['src-gen','src']
            aidl.srcDirs = ['src-gen','src']
            renderscript.srcDirs = ['src-gen','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')
    }

    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'
      }
   }
}

但是错误还是一样的:(

【问题讨论】:

    标签: android android-gradle-plugin android-library dex


    【解决方案1】:

    博客是旧的解决方案。

    使用 Android Studio 0.9.2 和 Gradle 插件 0.14.1,您只需:

    1. 添加到 AndroidManifest.xml:

    .

    android:name="android.support.multidex.MultiDexApplication" 
    

    添加

    MultiDex.install(this);
    

    在您的自定义应用程序的 attachBaseContext 方法中

    或您的自定义应用程序扩展 MultiDexApplication

    1. 在你的 build.gradle 中添加 multiDexEnabled = true

    .

    android {
        defaultConfig {
            ...
            multiDexEnabled = true
        }
    }
    

    完成。

    对不起我的英语不好

    相关资源:

    http://developer.android.com/tools/building/multidex.html

    https://plus.google.com/+XavierDucrohet/posts/1FnzwdcBnyC

    【讨论】:

    • 我可以在 eclipse 中而不是在 android studio 中执行此操作吗?
    • 目前没有。 Eclipse 的 ADT 插件不再处于积极开发中。
    【解决方案2】:

    你必须做一些事情,

    1- 在您的 gradle 中,您需要指定 multidex 并添加支持库:

    android {
        defaultConfig {
            ...
            multiDexEnabled true
            ...
        }
    }
    
    dependencies {
      ...
      compile 'com.android.support:multidex:1.0.0'
      ...
    }
    

    2- 在您的清单中,您必须将您的应用程序设置为 multidex 应用程序:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.package.name">
        <application
            ...
            android:name="com.package.name.my_application">
            ...
        </application>
    </manifest>
    

    3.1- 在您的应用程序类中,您必须扩展 MultiDexApplication

    public class my_application extends MultiDexApplication
    {
        ...
    }
    

    3.2- 或覆盖attachBaseContext() 方法:

    public class my_application extends Application
    {
        protected void attachBaseContext(Context base)
        {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    }
    

    multidex 支持库的限制

    multidex 支持库有一些已知限制,当您将其合并到应用构建配置中时,您应该注意并测试这些限制:

    • 在启动期间将 .dex 文件安装到设备的数据分区很复杂,如果辅助 dex 文件很大,可能会导致应用程序无响应 (ANR) 错误。在这种情况下,您应该使用 ProGuard 应用代码压缩技术来最小化 dex 文件的大小并删除未使用的代码部分。
    • 由于 Dalvik linearAlloc 错误(问题 22586),使用 multidex 的应用程序可能无法在运行早于 Android 4.0(API 级别 14)的平台版本的设备上启动。如果您的目标 API 级别早于 14,请确保使用这些版本的平台执行测试,因为您的应用程序在启动时或加载特定类组时可能会出现问题。代码缩减可以减少或可能消除这些潜在问题。
    • 由于 Dalvik 线性分配限制(问题 78035),使用 multidex 配置的应用程序发出非常大的内存分配请求可能会在运行时崩溃。 Android 4.0(API 级别 14)增加了分配限制,但在 Android 5.0(API 级别 21)之前的 Android 版本上,应用可能仍会遇到此限制。
    • 在 Dalvik 运行时执行时,对主 dex 文件中需要哪些类有复杂的要求。 Android 构建工具更新处理 Android 需求,但其他包含的库可能有额外的依赖要求,包括使用自省或从本机代码调用 Java 方法。在更新 multidex 构建工具以允许您指定必须包含在主 dex 文件中的类之前,某些库可能无法使用。

    资源:http://developer.android.com/tools/building/multidex.html

    【讨论】:

    • 这可以在eclipse中完成还是只能在android studio中完成?
    • Google 已停止支持 Eclipse ADT。所以可以用 eclipse 做到这一点,但 Android Studio 是前进的方向。我真的建议您将项目导出到 Android Studio:developer.android.com/sdk/installing/migrate.html
    • 因为我的项目是 40.000 行 + 并且包括多个库,如 greenDAO 我在迁移到 android studio 时遇到了极大的麻烦,这就是为什么我想在 eclipse 中这样做但我遇到了很多麻烦那。你知道这样做的解决方案是 eclipse 吗?
    • 我花了 3 天时间试图做到这一点,但没有运气,因为我是用公司的钱做的,所以我不能为此浪费更多时间。如果您有关于如何在 eclipse 中执行此操作的解决方案,我将不胜感激。
    • Step 1, Download android studio. 第二步,在Android studio中新建一个同名的项目。 Step 3, copy all the classes and resources from the eclipse project folder into the android studio.第四步,按下运行键。
    【解决方案3】:

    根据https://developer.android.com/studio/build/multidex.html#avoid的文档

    如果你的 minSdkVersion 是 21 及以上,你需要做的就是

    android {
        defaultConfig {
            ...
            minSdkVersion 21 
            targetSdkVersion 25
            multiDexEnabled true
        }
        ...
    }
    

    如果你的 minSdkVersion 是 20 或以下,你需要使用支持 图书馆

    android {
        defaultConfig {
            ...
            minSdkVersion 15 
            targetSdkVersion 25
            multiDexEnabled true
        }
        ...
    }
    
    dependencies {
      compile 'com.android.support:multidex:1.0.1'
    }
    

    伴随

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myapp">
        <application
                android:name="android.support.multidex.MultiDexApplication" >
            ...
        </application>
    </manifest>
    

    【讨论】:

      【解决方案4】:

      更新:

      如果你使用的是 androidX,你可以使用这个:

      implementation 'androidx.multidex:multidex:2.0.1'

      参考所有google maven,在这里可以搜索google的artifacts:
      https://maven.google.com/web/index.html

      【讨论】:

        猜你喜欢
        • 2015-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多