【问题标题】:Cannot fit requested classes in a single dex file (# methods: 86965 > 65536)无法在单个 dex 文件中容纳请求的类(# 方法:86965 > 65536)
【发布时间】:2019-10-03 11:59:50
【问题描述】:

*更新:运行 ProGuard 以减少我的资源后,ProGuard 似乎正在删除重要代码: 错误:在 org.gradle.api.Project 类型的根项目“Google 地图演示”上找不到参数 [build_7b52k4yyeks9l2efdeec28zeo$_run_closure2@6cc7c31f] 的方法 android()。 更新 build.gradle 代码:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
        mavenLocal()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath 'com.google.gms:google-services:4.3.2'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        maven {
            url "https://maven.google.com"
        }
    }
}

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }

}


task clean(type: Delete) {
    delete rootProject.buildDir
}

我正在使用 android studio 使用 Android 的 Coding 教程 (https://www.youtube.com/watch?v=eiexkzCI8m8) 来实现 Google maps api。我在我的 Java 编译器中将此错误视为“D8 错误”和两个错误。 错误 #1:“原因:com.android.tools.r8.CompilationFailedException:编译未能完成”

错误 #2:“原因:com.android.tools.r8.utils.AbortException:错误:null,无法在单个 dex 文件中容纳请求的类(# 方法:86965 > 65536)”

有人发现问题了吗?

这是我的java代码:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback{

    GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;

        LatLng Brockton = new LatLng(42.0895988,-70.9798322);
        map.addMarker(new MarkerOptions().position(Brockton).title("Brockton"));
        map.moveCamera(CameraUpdateFactory.newLatLng(Brockton));
    }
}

【问题讨论】:

    标签: java android api google-maps


    【解决方案1】:

    这不是您的代码有问题,而是因为您对 Google 地图的依赖。

    这为您的应用添加了许多方法,超过了每个 dex 文件 64k 的限制。有两种解决方案:

    使用 MultiDex:https://developer.android.com/studio/build/multidex Android Studio会生成多个dex文件,所以没有达到上限。

    使用 ProGuard 删除未使用的方法:https://developer.android.com/studio/build/shrink-code 未调用的方法不会添加到您的 dex 文件中。结合混淆,这总是一个好主意。

    【讨论】:

    • 我尝试首先使用 ProGuard 缩小代码并返回此错误:在根项目 'Google maps Demo' 类型上找不到参数 [build_7b52k4yyeks9l2efdeec28zeo$_run_closure2@6cc7c31f] 的方法 android() org.gradle.api.Project.
    【解决方案2】:

    写:

    multiDexEnabled true
    

    在 build.gradle(app) 文件中

    【讨论】:

      【解决方案3】:

      解决方案:

      打开您的应用级 build.gradle 文件。并简单地写在 android->defaultConfig-> multiDexEnabled true

      更好理解的代码。

      android {
          defaultConfig {
              multiDexEnabled true
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-09
        • 2023-03-27
        • 1970-01-01
        • 2020-02-03
        • 2018-06-23
        相关资源
        最近更新 更多