Android studio 1.4 and vector image

2019-05-11 11:49发布

问题:

Today I up to date android studio to 1.4 version. I saw in the changelog that you can compile an app with vector image also for api < 21. I tried it. I changed the gradle version from 1.3.0 to 1.4.1 and implemented vector image instead of raster. During building process I get this error:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.transform.api.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 2

Sorry for my bad english. I hope you understand. Thanks in advance.

回答1:

Step 1. First try out Cleaning & restarting your Project and see if it works out for you. In mostly cases it will solve out your issues.

Step 2. If none of this works out for you that means you will have to enabled MultiDex mode.

Multidex support for Android 5.0 and higher

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files. ART performs pre-compilation at application install time which scans for classes(..N).dex files and compiles them into a single .oat file for execution by the Android device. For more information on the Android 5.0 runtime, see Introducing ART.

That means your app would working fine on API level 21 or above.

Multidex support prior to Android 5.0

Versions of the platform prior to Android 5.0 use the Dalvik runtime for executing app code. By default, Dalvik limits apps to a single classes.dex bytecode file per APK. In order to get around this limitation, you can use the multidex support library, which becomes part of the primary DEX file of your app and then manages access to the additional DEX files and the code they contain.

So, Firstly making sure you have imported correct dependency, which It seems you could do by below way.

dependencies {
  // Change as per the latest dependency
  compile 'com.android.support:multidex:1.0.1'
}

In your manifest add the MultiDexApplication class from the multidex support library to the application element.

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

Alternative to that, If your app extends the Application class, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex.

public void onCreate(Bundle arguments) {
    MultiDex.install(getTargetContext());
    super.onCreate(arguments);
    ...
}

Finally, you will need to update your build.gradle file as below by adding multiDexEnabled true :

defaultConfig {  
        applicationId '{Project Name}'  
        minSdkVersion 15  
        targetSdkVersion 23  
        versionCode 1  
        versionName "1.0"  
        multiDexEnabled true  
    }  

I hope it will help you out.



回答2:

Try to enable multiDex properties to your build.gradle file :

defaultConfig {
    multiDexEnabled true
}


回答3:

I had the same issue when I was using a lower build tools version. Try to upgrade your build tools version, for example it works for me when I'm using v23.

buildToolsVersion "23.0.1"