简体   繁体   English

Android支持multidex库实现

[英]Android support multidex library implementation

I have hit the magic dex limit because my application uses a lot of jars (drive API, greendao, text to pdf, support.. ).我达到了神奇的 dex 限制,因为我的应用程序使用了很多 jar(驱动 API、greendao、文本到 pdf、支持..)。

My current solution was that I literally created a second apk just for google drive which I called from the main apk.我目前的解决方案是我真的为我从主 apk 调用的谷歌驱动器创建了第二个 apk。 But now I found out that android finally supports this with this library.但现在我发现 android 终于支持这个库了。 My problem is just that I don't know how to implement it(preferably without gradle).我的问题只是我不知道如何实现它(最好没有 gradle)。 I can't find any good tutorials for it.我找不到任何好的教程。

Okey I am losing my mind trying to implement this... I have found this好吧,我在尝试实现这一点时失去了理智......我发现了这个

And I added:我补充说:

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

To my manifest file and到我的清单文件和

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

To my mainactivity.java到我的 mainactivity.java

Also installed gradle plugin for eclipse, exported gradle to get build.gradle file which I changed to:还为 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'
      }
   }
}

But The error is still the same :(但错误仍然相同:(

The Blog was the old solution.博客是旧的解决方案。

With Android Studio 0.9.2 & Gradle Plugin 0.14.1, you only need to:使用 Android Studio 0.9.2 和 Gradle 插件 0.14.1,您只需要:

  1. Add to AndroidManifest.xml:添加到 AndroidManifest.xml:

. .

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

or或者

Add添加

MultiDex.install(this);

in your custom Application's attachBaseContext method在您的自定义应用程序的attachBaseContext方法中

or your custom Application extend MultiDexApplication或您的自定义应用程序扩展MultiDexApplication

  1. add multiDexEnabled = true in your build.gradlemultiDexEnabled = true添加multiDexEnabled = true

. .

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

Done.完毕。

Sorry for my poor English对不起我的英语不好

Related Resources:相关资源:

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

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

There are a few things you have to do,有几件事你必须做

1- In your gradle you need to specify multidex and add the support library: 1- 在您的 gradle 中,您需要指定 multidex 并添加支持库:

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

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

2- In your manifest you have to set your application to a multidex application: 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- In your application class you have to either extend MultiDexApplication : 3.1- 在您的应用程序类中,您必须扩展MultiDexApplication

public class my_application extends MultiDexApplication
{
    ...
}

3.2- Or Overide the attachBaseContext() method: 3.2- 或attachBaseContext()方法:

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

Limitations of the multidex support library multidex 支持库的限制

The multidex support library has some known limitations that you should be aware of and test for when you incorporate it into your app build configuration: multidex 支持库有一些已知的限制,当您将其合并到应用程序构建配置中时,您应该注意并测试这些限制:

  • The installation of .dex files during startup onto a device's data partition is complex and can result in Application Not Responding (ANR) errors if the secondary dex files are large.在启动期间将 .dex 文件安装到设备的数据分区上很复杂,如果辅助 dex 文件很大,可能会导致应用程序无响应 (ANR) 错误。 In this case, you should apply code shrinking techniques with ProGuard to minimize the size of dex files and remove unused portions of code.在这种情况下,您应该使用 ProGuard 应用代码收缩技术来最小化 dex 文件的大小并删除未使用的代码部分。
  • Applications that use multidex may not start on devices that run versions of the platform earlier than Android 4.0 (API level 14) due to a Dalvik linearAlloc bug (Issue 22586).由于 Dalvik linearAlloc 错误(问题 22586),使用 multidex 的应用程序可能无法在运行早于 Android 4.0(API 级别 14)的平台版本的设备上启动。 If you are targeting API levels earlier than 14, make sure to perform testing with these versions of the platform as your application can have issues at startup or when particular groups of classes are loaded.如果您的目标 API 级别早于 14,请确保使用这些版本的平台执行测试,因为您的应用程序可能会在启动时或加载特定类组时出现问题。 Code shrinking can reduce or possibly eliminate these potential issues.代码缩减可以减少或可能消除这些潜在问题。
  • Applications using a multidex configuration that make very large memory allocation requests may crash during run time due to a Dalvik linearAlloc limit (Issue 78035).由于 Dalvik linearAlloc 限制(问题 78035),使用 multidex 配置的应用程序发出非常大的内存分配请求可能会在运行时崩溃。 The allocation limit was increased in Android 4.0 (API level 14), but apps may still run into this limit on Android versions prior to Android 5.0 (API level 21).在 Android 4.0(API 级别 14)中增加了分配限制,但在 Android 5.0(API 级别 21)之前的 Android 版本上,应用程序可能仍会遇到此限制。
  • There are complex requirements regarding what classes are needed in the primary dex file when executing in the Dalvik runtime.在 Dalvik 运行时中执行时,主 dex 文件中需要哪些类有复杂的要求。 The Android build tooling updates handle the Android requirements, but it is possible that other included libraries have additional dependency requirements including the use of introspection or invocation of Java methods from native code. Android 构建工具更新处理 Android 要求,但其他包含的库可能具有额外的依赖项要求,包括使用内省或从本机代码调用 Java 方法。 Some libraries may not be able to be used until the multidex build tools are updated to allow you to specify classes that must be included in the primary dex file.在 multidex 构建工具更新以允许您指定必须包含在主 dex 文件中的类之前,某些库可能无法使用。

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

According to the docs at https://developer.android.com/studio/build/multidex.html#avoid根据https://developer.android.com/studio/build/multidex.html#avoid上的文档

If you're minSdkVersion is 21 and above, all you need to do is如果你的 minSdkVersion 是 21 及以上,你需要做的就是

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

if you're minSdkVersion is 20 or below, you need to use the support library如果您的 minSdkVersion 为 20 或以下,则需要使用支持库

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

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

Along with随着

<?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>

update:更新:

if you're using androidX you can use this:如果你使用 androidX 你可以使用这个:

implementation 'androidx.multidex:multidex:2.0.1'

reference to all google maven, where you can search for google's artifacts:参考所有 google maven,您可以在其中搜索 google 的工件:
https://maven.google.com/web/index.html https://maven.google.com/web/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM