现在无法访问dex任务,从1.4.0开始

时间:2016-01-06 03:42:22

标签: android android-studio gradle

我无法将我的项目升级为gradle 1.5:

build.gradle(root)项目刷新失败:从1.4.0开始,现在无法访问dex任务。 错误:1.4.0引入了一个新的Transform API,允许操作.class文件。

有什么建议吗?

root build.gradle:

buildscript {
    repositories {
        jcenter()
    }
dependencies {
    classpath 'com.android.tools.build:gradle:1.5.0'
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

subprojects {
    task allDependencies(type: DependencyReportTask) {}
}

app build.gradle:

buildscript {
repositories {
    mavenCentral()
    maven { url 'http://download.crashlytics.com/maven' }
}

dependencies {
    classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.14.7'
    classpath "com.newrelic.agent.android:agent-gradle-plugin:4.244.0"
}
}

repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
maven { url 'http://download.crashlytics.com/maven' }
}

task wrapper(type: Wrapper) {
gradleVersion = '2.7'
}

apply plugin: 'com.android.application'

 android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

signingConfigs {
    release
}

testBuildType "debug"

defaultConfig {
    targetSdkVersion 23
    minSdkVersion 14

    multiDexEnabled true

    applicationId "com.freelancer.android.messenger"
    versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
    versionName "${versionMajor}.${versionMinor}.${versionPatch}"
    testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'

    renderscriptTargetApi 21
    renderscriptSupportModeEnabled true

}

sourceSets {
    production {
        java.srcDir 'src/common/java'
    }

    develop {
        java.srcDir 'src/common/java'
    }

    staging {
        java.srcDir 'src/common/java'
    }
}

buildTypes {
    release {
        signingConfig signingConfigs.release
        minifyEnabled true
        proguardFile 'proguard.cfg'
        buildConfigField "String", "CONTENT_PROVIDER_AUTHORITY",
                "\"com.freelancer.android.messenger.content_provider\""
    }
    debug {
        applicationIdSuffix '.debug'
        versionNameSuffix '.' + versionBuild + '-debug' + gitSha
        buildConfigField "String", "CONTENT_PROVIDER_AUTHORITY",
                "\"com.freelancer.android.messenger.content_provider.\" + BUILD_TYPE.toLowerCase()"
        debuggable true
        testCoverageEnabled = true
    }

    staging {
        applicationIdSuffix '.staging'
        versionNameSuffix '-staging-' + gitSha

        buildConfigField "String", "BASE_URL_API", "\"${rootProject.ext.stagingApiServer}\""
        buildConfigField "String", "BASE_URL_AUTH", "\"${rootProject.ext.stagingAuthServer}\""
        buildConfigField "String", "BASE_URL_WSS", "\"${rootProject.ext.stagingWsServer}\""
        buildConfigField "String", "BASE_URL_GAF", "\"${rootProject.ext.stagingGafUrl}\""
    }
}


productFlavors {
    // Define separate dev and prod product flavors.
    develop {
        // 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
    }
    production {
        // The actual minSdkVersion for the application.
    }
    mock {

    }

}

// from android 6 apache its been deprecated: refer to ->
// http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
 //    useLibrary 'org.apache.http.legacy'

}


dependencies {

compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:cardview-v7:22.2.0'
compile 'com.android.support:palette-v7:22.2.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:design:22.2.0'

compile 'com.google.android.gms:play-services-gcm:7.5.0'
compile 'com.google.android.gms:play-services-maps:7.5.0'
compile 'com.google.android.gms:play-services-location:7.5.0'
compile 'com.google.android.gms:play-services-analytics:7.5.0'

compile 'com.google.maps.android:android-maps-utils:0.3.4'
compile 'com.google.code.gson:gson:2.3'

// extension for popular dashclock app
compile 'com.google.android.apps.dashclock:dashclock-api:2.0.0'

compile 'com.jakewharton:butterknife:4.0.1'
compile 'com.squareup:otto:1.3.5'
compile 'com.squareup.dagger:dagger:1.2.2'
compile 'com.squareup.dagger:dagger-compiler:1.2.2'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
compile 'com.squareup.okhttp:okhttp:2.5.0'

compile ('com.facebook.android:facebook-android-sdk:3.23.0') {
    exclude group: 'com.google.android'
}

compile 'com.newrelic.agent.android:android-agent:4.244.0'
compile "com.mixpanel.android:mixpanel-android:4.5.3"
compile 'com.crashlytics.android:crashlytics:1.1.13'

compile 'com.github.kevinsawicki:http-request:6.0' // TODO deprecate and replace methods with okhttp

compile 'com.koushikdutta.async:androidasync:1.2.9@jar'
compile 'com.koushikdutta.ion:ion:1.2.9@jar'
compile 'com.path:android-priority-jobqueue:1.1.2'
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
compile 'com.soundcloud.android:android-crop:0.9.10@aar'

compile project(':libs:stickylistheaders:library')
compile project(':libs:android-core:android-core-library')
compile project(':libs:viewpagerindicator:library')
compile project(':libs:PagerSlidingTabStrip')

compile "org.mockito:mockito-core:1.10.19"
compile 'com.google.dexmaker:dexmaker:1.2'
compile 'com.google.dexmaker:dexmaker-mockito:1.2'

compile 'com.bartoszlipinski.flippablestackview:library:1.2.1'
compile 'com.edmodo:rangebar:1.0.0'
compile 'com.github.clans:fab:1.6.1'

compile fileTree(dir: '../libs', include: '*.jar')

testCompile('org.robolectric:robolectric:3.0') {
    exclude group: 'commons-logging', module: 'commons-logging'
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'recyclerview-v7'
}
testCompile "org.robolectric:shadows-multidex:3.0" // this is needed to avoid multidex

// Dependencies for local unit tests
//    testCompile "org.assertj:assertj-core:$rootProject.ext.assertjCore"
testCompile "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
testCompile "junit:junit:$rootProject.ext.junitVersion"
testCompile "org.mockito:mockito-all:$rootProject.ext.mockitoVersion"
testCompile "org.powermock:powermock-module-junit4:$rootProject.ext.powerMockito"
testCompile "org.powermock:powermock-api-mockito:$rootProject.ext.powerMockito"

// Android Testing Support Library's runner and rules
androidTestCompile "com.android.support.test:runner:$rootProject.ext.runnerVersion"
androidTestCompile "com.android.support.test:rules:$rootProject.ext.runnerVersion"

// Espresso UI Testing dependencies.
androidTestCompile "com.android.support.test.espresso:espresso-core:$rootProject.ext.espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:$rootProject.ext.espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-intents:$rootProject.ext.espressoVersion"

// dependencies added after dex split
//    compile 'com.jakewharton.timber:timber:3.1.0'
 }


 configurations.all {
     resolutionStrategy.force "com.android.support:support-   annotations:$rootProject.supportLibraryVersion"
     resolutionStrategy.force "org.hamcrest:hamcrest-core:$rootProject.hamcrestVersion"
     resolutionStrategy.force "com.squareup:javawriter:2.5.0"
}

 configurations.compile.dependencies.each { compileDependency ->
     println "Excluding compile dependency: ${compileDependency.getName()}"
     configurations.androidTestCompile.dependencies.each { androidTestCompileDependency ->
         configurations.androidTestCompile.exclude module:    "${compileDependency.getName()}"
    }
 }

我跟随https://codelabs.developers.google.com/codelabs/android-testing/让测试工作在我的项目上(例如mokito),但它不起作用。

2 个答案:

答案 0 :(得分:15)

也许这个问题与newrelic版本有关。我已经尝试安装最新版本的newrelic并成功。

  

classpath" com.newrelic.agent.android:agent-gradle-plugin:5.5.0"

     

compile' com.newrelic.agent.android:android-agent:5.5.0'

答案 1 :(得分:0)

要解决您的问题,请按以下步骤操作。 (Access to the Dex is now impossible, starting with 1.4.0

  1. 将所有库(从Google Play服务和支持库以及其他使用的SDK中的依赖项)更新为最大可用版本。如果您使用 NewRelic ,则会更新到最新版本
  2. 将multiDexEnabled true添加到/app/build.gradle中的defaultConfig。

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        multiDexEnabled true
    }
    
  3. 删除您之前编写的所有代码,以使MultiDexing正常工作。 例如,

    • 删除MultiDex的afterEvaluate

      afterEvaluate {
          tasks.matching {
              it.name.startsWith('dex')
          }.each { dx ->
              if (dx.additionalParameters == null) {
                  dx.additionalParameters = []
          }
          dx.additionalParameters += '--multi-dex'
      
          // this is optional
          //  dx.additionalParameters += "--main-dex-list=$projectDir/multidex-classes.txt".toString()
          }
      }
      
    • 删除multidex依赖

      dependencies {
          ...
          compile 'com.android.support:multidex:1.0.1'
      }
      
    • AndroidManifest.xml

      中删除代码
      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.example.pcsalt.multidex">
          <application
                  ...
              android:name="android.support.multidex.MultiDexApplication">
                  ...
          </application>
      </manifest>
      
    • 如果您通过扩展android.app.Application来使用自定义Application类,则删除MultiDex.install(base);

      @Override
      protected void attachBaseContext(Context base) {
              super.attachBaseContext(base);
              MultiDex.install(base);
      }
      
  4. 在/build.gradle中将构建工具版本更新为2.1.2(撰写本文时可用的最高稳定版本)

    classpath 'com.android.tools.build:gradle:2.1.2'
    
  5. Gradle通过单击立即同步

  6. 同步项目

    希望这会有所帮助。

    http://www.pcsalt.com/android/solution-access-dex-task-now-impossible-starting-1-4-0