Android method references more than 65535 resolution

Source: Internet
Author: User

//in App/build.gradleAndroid {compilesdkversion -buildtoolsversion"24.0.1"defaultconfig {... minsdkversion -targetsdkversion -        ...        //set up support MultidexMultidexenabledtrue    }    ...} dependencies {Compile'com.android.support:multidex:1.0.1'}

Subclasses in application (if you need subclasses)

Package com.example;import Android.app.application;import android.content.Context; /* * *   */  Public class MyApplication extends Application {    @Override    protectedvoidbase ) {        super.attachbasecontext (base);        Multidex.install (this);}    }

If you don't need a subclass of application to inherit directly multidexapplication

Package com.example;import Android.support.multidex.multidexapplication;import android.content.Context; /* * * Extended multidexapplication   */  Public class MyApplication extends Multidexapplication {     //  do not need to rewrite Attachbasecontext     () // ..........}

Dex is a file format for Android Java code compilation, and early Android loaded and executed binary DEX files directly in the Dalvik virtual machine, android4,4 later added a new application run mode called Art, He will do a pre-compilation at the time of installation before the program is run, converting the Dex file to machine code exists locally,

The disadvantage of this file format is that the number of methods of the Android code refers to a single binary, because a large portion of the file format stores the number is two bytes in length, that is, the maximum number of methods is 65535, if an applied method reference exceeds the number compiled will fail;

Google has provided a way to call Multidex, which contains compile-time and run-time two parts, from the name can be seen, at compile time it will be the code into two or more Dex files, at run time, it will tell the ClassLoader how to find the corresponding class file from these Dex files, But this way also has a certain flaw, it will increase the program start time and can cause the program to be unresponsive on the old machine, how should be avoided?

If your method reaches 640000 or more, then you should try to reduce the method reference of your code or the method reference in the class library, how to do it as follows:
1, take a look at the direct and indirect dependencies of the program: adding a large dependent library is better than importing the relevant code into the program.
2. Use the Proguard tool to reduce the code you don't use

Android {    buildtypes {        release {            true            proguardfiles getdefaultproguardfile (' Proguard-android.txt'),                    'proguard-rules.pro'         }    }}
true :The default rule for the getdefaultproguardfile (' Proguard-android.txt') The proguard-rules. You can customize the rules

Android introduces third-party SDK to confuse error shrinkdebugmultidexcomponents solution

In Project Engineering, a third-party SDK is referenced, and its jar packages are already confused. Code compilation in the IDE is fine, but running on the phone is an error.

Error:execution failed for task ': App:shrinkdebugmultidexcomponents '.
Java.io.IOException:Can ' t read [Src\app\build\intermediates\multi-dex\debug\allclasses.jar] (Can ' t process class [ Com/dvrcam/dvrcamsdk/a$d.class] (Unknown verification Type [255] in stack map frame))

The general meaning is that the internal class D.class obfuscation fails under the JAR package directory com/dvrcam/dvrcamsdk/a/.

Solution Solutions

Create a folder in the root of the project Proguard, put in the proguard.jar file,:Proguard.jar

In the root directory of the build.gradle file, add

flatdir {dirs ' Proguard '} and classpath ' proguard.io:proguard:5.2.1 '

//top-level build file where can add configuration options common to all sub-projects/modules.Buildscript {repositories {flatdir {dirs'Proguard'}//1. Dependent Library class directoryjcenter ()} dependencies {Classpath'com.android.tools.build:gradle:1.2.3'Classpath'proguard.io:proguard:5.2.1'//2. Dependent Library Classes}}allprojects {repositories {jcenter ()}}

Follow the steps above and you will be able to run successfully!

Android method references more than 65535 resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.