Dowemo
0 0 0 0

In the front of the .

In the last few days, there was a problem in development, and log information was as follows:

E/AndroidRuntime(10943): FATAL EXCEPTION: main
E/AndroidRuntime(10943): Process: com.freeme.gallery, PID: 10943
E/AndroidRuntime(10943): java.lang.NoClassDefFoundError: com.freeme.gallery.data.DataManager$DateTakenComparator
E/AndroidRuntime(10943): at com.freeme.gallery.data.DataManager.<clinit>(DataManager.java:65)
E/AndroidRuntime(10943): at com.freeme.gallery.app.GalleryAppImpl.getDataManager(GalleryAppImpl.java:77)
E/AndroidRuntime(10943): at com.freeme.gallery.provider.GalleryProvider.onCreate(GalleryProvider.java:101)
E/AndroidRuntime(10943): at android.content.ContentProvider.attachInfo(ContentProvider.java:1656)
E/AndroidRuntime(10943): at android.content.ContentProvider.attachInfo(ContentProvider.java:1627)
E/AndroidRuntime(10943): at android.app.ActivityThread.installProvider(ActivityThread.java:5060)
E/AndroidRuntime(10943): at android.app.ActivityThread.installContentProviders(ActivityThread.java:4634)
E/AndroidRuntime(10943): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4567)
E/AndroidRuntime(10943): at android.app.ActivityThread.access$1500(ActivityThread.java:153)
E/AndroidRuntime(10943): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1404)
E/AndroidRuntime(10943): at android.os.Handler.dispatchMessage(Handler.java:110)
E/AndroidRuntime(10943): at android.os.Looper.loop(Looper.java:193)
E/AndroidRuntime(10943): at android.app.ActivityThread.main(ActivityThread.java:5351)
E/AndroidRuntime(10943): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(10943): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(10943): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)
E/AndroidRuntime(10943): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
E/AndroidRuntime(10943): at dalvik.system.NativeStart.main(Native Method)

From the error message, it isn't found that the internal class of DateTakenComparator isn't found and is a runtime exception that isn't associated with classloader.
Then first, start with the code reason, start from the gradle and gradle version of the. But it isn't ok to verify it.

So where's the problem.
so start by node troubleshooting, and after a few key nodes, finally get a conclusion: The introduced a specific library.

However, this library is imported directly from maven, and the library itself has no problem. It appears that the clue is broken. At this time, the colleague is advised to see the apk package size. I don't know what you see. It's a big way to see it. There's a big twist in apk.

An .dex package contai two files: And classes.dex and classes2.dex, and then look at java.lang.NoClassDefFoundError, the result is obvious. But you've configured multiDexEnabled true and added android.support.multidex in build. Exe, and why do you've an error. It was just forgotten to inherit MultiDexApplication.

Next, we'll learn how to limit the number of 64k methods.

text

With the increase of the new features, apk will become larger and larger, and there may be a problem of problems after reaching a certain scale.
Earlier ve & ion error messages are as follows:

Conversion to Dalvik format failed:
Unable to execute dex: method ID not in [0, 0xffff]: 65536

A newer ve & ion error message is as follows:

trouble writing output:
Too many field references: 131000; max is 65536.
You may try using --multi-dex option.

The digital 65536 is the key, and the short type is used to index the methods in the dex file when the java virtual machine balancers of the android platform. It means that the total number of methods a single dex file can be referenced is 64x1024, 65536. It includes:

  • Methods of the android framework
  • Library method
  • Our own method.

To break this limitation, you need to use multidex to generate multiple ColorSync files.

android5. 0 ( api level 21 ) before version support multidex.

Before android5. 0 to execute the application code with the runtime, the default limit each apk has only one byte code classed.dex file. To break this limitation, you can use the multidex support library to manage additional dex files ( including code ).

android5. 0 and later support multidex.

A android5. 0 and higher ve & io & use an art runtime that supports loading multiple dex files from apk, loading classed (. N ), and compiling a .oat file to support running on an android device to support running on android devices. For more information about android 5.0, see art introduction.

Note: while using instant run, studio automatically configures your app for multidex when your app s 21 or.

If you use instant run transconductance, studio automatically configures support multidex when minSdkVersion is greater than or equal to 21, but only debug version is valid, the release version still needs to configure multidex to break the 64k limit.

to avoid 64k restrictio &

Before configuring multidex, you might be able to reduce the total number of methods ( including references, libraries in the library, and how you write them ).

  • excludes unused dependencies this step typically avoids 64k limitatio &.
  • uses proguard to remove unused methods to configure proguard for release release and can effectively eliminate some useless methods

Using the techniques above can effectively avoid changing the build configuration to reference more methods while reducing the apk size, allowing users to co.

to configure multidex

Android sdk build tools 21.1 or later support multidex, and make sure that you've to configure multidex. Android SDK Build Tools And Android Support Repository updates to newer versions.

Configure multidex by following these steps:

  • Change the gradle configuration to support multidex
  • Modify the manifest to support the MultiDexApplication class

the builde. Gradle file, and modify the following:

android {
 compileSdkVersion 21
 buildToolsVersion"21.1.0"
 defaultConfig {
. . .
 minSdkVersion 14
 targetSdkVersion 21
. . .
//Enabling multidex support.
 multiDexEnabled true
 }
. . .
}
dependencies {
 compile 'com.android.support:multidex:1.0.0'
}

in the manifest file, add a reference to MultiDexApplication class, as follows

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

In the above steps, multidex can be supported.

Note: if your app uses extends the application class, you can override the attachBaseContext ( ) method and call multidex. Install ( this ) to. For.

If you've already inherited application from your application, you can support multidex by duplicating the attachBaseContext() method and calling MultiDex.install(this) to support the For more information, see MultiDexApplication.

:
can also directly change inheritance Application to inherited MultiDexApplication without modifying the manifest file or copying attachBaseContext() method.

multidex support library

multidex support library has some known restrictions, please be aware that you need to test it.

  • If the classes2. Dex file is larger, the data area I & talled to the device to the device is a complex process, which may result in an error in the application 's response. In this case, you should try to reduce the size of dex files and delete useless code.

  • Before android 4.0 ( api level 14 ), multidex could be a failure due to LinearAlloc bugs ( problem 22586 MCAUSER ). If you want to run the version of the android system before level 14, make sure the complete test and use. Optimized code can reduce or potentially eliminate these potential problems.

  • Application uses multiedex configuration, which causes large memory allocation to apply. Could also cause crash of virtual machines ( problem 78035 ). This allocation limit is added on android 4.0 ( api level 14 ), but the previous version of android5. 0 ( api level 21 ) still has this restriction.

  • A multidex build tool doesn't support specifying which classes must be included in the first dex file, so that some libraries can't be used.

optimization multidex development and build

Multidex will extend the time to build applications, and this will probably slow down your speed.
In order to speed up the build process, we can configure productFlavors les in a gradle:a development flavor and a production flavor .

Instead, change minSdkVersion to 21 using art, which speeds up the build speed. Change to the appropriate minSdkVersion at release time so that it takes longer than the release time.

Build. Gradle is configured as follows:

android {
 productFlavors {
//Define separate dev and prod product flavors.
 dev {
//dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
//to pre-dex each module and produce an APK that can be tested on
//Android Lollipop without time consuming dex merging processes.
 minSdkVersion 21
 }
 prod {
//The actual minSdkVersion for the application.
 minSdkVersion 14
 }
 }
. . .
 buildTypes {
 release {
 runProguard true
 proguardFiles getDefaultProguardFile('proguard-android.txt'),
 'proguard-rules.pro'
 }
 }
}
dependencies {
 compile 'com.android.support:multidex:1.0.0'
}

After completing the above configuration, you can use the devDebug variant buildType that combines the dev productFlavor and attributes.
This variant app includes the following features:

  • To close the confusion ( proguard )
  • Support multidex
  • Minsdkversion is set to the android api level 21.

These settings will make the gradle as follows:

  • Compiling each module of the application ( including dependencies ) is a standalone dex file, called pre-dexing.
  • Don't modify the include, every dex file to the apk.
  • More importantly, these modules won't merge, so avoid dividing the main dex file to speed up the steps.

It's important to note that the devdebug variant of the above configuration can only run on the android 5.0 device.

At the same time, you can build other variants, or you can use the gradel command in the terminal to achieve packaging. For more information on flavors and Gradle tasks information, see gradle plugin ( chinese translation ).

build variant app in android studio

Building variant apps is very useful when using multidex to manage the build process. Android studio allows users to choose their own.

Build a variant app in android studio, as follows:

  • Open the Build Variants window from the left column
  • Click build variant to select different variations, as shown in figure:

  • test multidex application

Test multidex applicatio &, and configure multidextestrunner in build.gradle:

android {
 defaultConfig {
. . .
 testInstrumentationRunner"com.android.test.runner.MultiDexTestRunner"
 }
}

Note: with android plugin for is than 1.1. You need to add the following dependency for multidex-instrumentation:

If the gradle ve ion is below 1. 1, you also need to add multidex-instrumentation dependencies:

dependencies {
 androidTestCompile('com.android.support:multidex-instrumentation:1.0.1') {
 exclude group: 'com.android.support', module: 'multidex'
 }
}

note: link to an official link, please crawl the wall!

reference.
1. building apps with over 64K.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs