Android 当'preDexLibraries'为true时,棒棒糖上的应用程序崩溃

Android 当'preDexLibraries'为true时,棒棒糖上的应用程序崩溃,android,performance,android-layout,unity3d,android-multidex,Android,Performance,Android Layout,Unity3d,Android Multidex,我在我的项目中添加了一个aar文件。aar文件的大小约为50MB,这是一款在应用程序内部运行的unity游戏。我当前应用程序的apk大小约为82MB,因此使用此Unity库,apk大小需要130MB 默认情况下,我使用这些dex选项 dexOptions { javaMaxHeapSize "4g" //specify the heap size for the dex process preDexLibraries false } 现在,当preDexLibraries设置为

我在我的项目中添加了一个
aar
文件。aar文件的大小约为50MB,这是一款在应用程序内部运行的unity游戏。我当前应用程序的
apk
大小约为82MB,因此使用此
Unity库
,apk大小需要
130MB

默认情况下,我使用这些dex选项

dexOptions {
    javaMaxHeapSize "4g" //specify the heap size for the dex process
    preDexLibraries false
}
现在,当
preDexLibraries
设置为false时,gradle操作将失败,并显示以下错误消息

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: Translation has been interrupted
如果我将
preDexLibraries
设置为
true
,它将在lollipop+设备上成功构建并运行,但在lollipop上失败,并显示以下错误消息

11-21 17:43:25.229 14213-14213/com.myapp.app E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.myapp.app, PID: 14213
  java.lang.RuntimeException: Unable to instantiate application com.myapp.app.AnalyticsSampleApp: java.lang.ClassNotFoundException: Didn't find class "com.myapp.app.AnalyticsSampleApp" on path: DexPathList[[zip file "/data/app/com.myapp.app-2/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp.app-2/lib/arm, /vendor/lib, /system/lib]]
    at android.app.LoadedApk.makeApplication(LoadedApk.java:628)
   android.app.ActivityThread.handleBindApplication(ActivityThread.java:4966)
   at android.app.ActivityThread.access$1600(ActivityThread.java:177)
   android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5912)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
其中as
AnalyticsSampleApp
是我的应用程序类,它从
Application

我在谷歌上搜索了这个,但仍然无法理解
preDexLibraries
实际上做了什么

preDexLibraries(第三个问题的答案):它从库中构建dex文件,以便在增量构建(而不是构建)中使用 库的每次索引文件)。因此,清洁时使用此项 build会让一切都变慢


请在gradle中的defaultConfig{…}中添加multiDexEnabled true

例如:

并在依赖项{….}中添加compile'com.android.support:multidex:1.0.1'

例如:

如果没有锻炼,请创建一个类

import android.content.Context;
import android.support.multidex.MultiDexApplication;

    public class EnableMultiDex extends MultiDexApplication {
        private static EnableMultiDex enableMultiDex;
        public static Context context;

        public EnableMultiDex(){
            enableMultiDex=this;
        }

        public static EnableMultiDex getEnableMultiDexApp() {
            return enableMultiDex;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            context = getApplicationContext();

        }
    }
并在清单中给出android:name=“packagename.EnableMultiDex”

例如:



既然扩展了应用程序,就必须重写
attachBaseContext
函数,然后调用
super.attachBaseContext(base)在它里面,然后是
MultiDex.install(这个)
@Programmer:我的目标是棒棒糖+设备,我还需要这样做吗?我刚刚从gradleThanks启用了multidex,但添加了它之后,崩溃仍然存在。是的,相同的错误日志,这只发生在棒棒糖设备中
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile project(':aars')
    compile 'com.yalantis:ucrop:2.2.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
}
import android.content.Context;
import android.support.multidex.MultiDexApplication;

    public class EnableMultiDex extends MultiDexApplication {
        private static EnableMultiDex enableMultiDex;
        public static Context context;

        public EnableMultiDex(){
            enableMultiDex=this;
        }

        public static EnableMultiDex getEnableMultiDexApp() {
            return enableMultiDex;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            context = getApplicationContext();

        }
    }
<application
    android:name="YourPakageName.EnableMultiDex"
    android:hardwareAccelerated="true"
    android:icon="@drawable/wowio_launch_logo"
    android:label="@string/app_name"
    android:largeHeap="true"
    tools:node="replace">