【问题标题】:app:transformClassesWithDexForDebug with multiple modulesapp:transformClassesWithDexForDebug 与多个模块
【发布时间】:2017-08-18 16:23:15
【问题描述】:

我正在尝试创建一个依赖于 java 库模块中的 java 代码的 android 应用程序,但是每当我尝试使用该 java 库中的任何对象时,我总是得到以下结果

任务 ':app:transformClassesWithDexForDebug' 执行失败。

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1 .8.0_121\bin\java.exe'' 以非零退出值 1 结束

我查看了以下Execution failed for task ':app:transformClassesWithDexForDebug' while implementing Google sign in for Android 并尝试了几乎所有建议,但仍然无法构建。我的 java 库确实使用了 android 使用的 json 库,我收到了一些警告,但它说它将被忽略

这是我的 build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "pwils33.familymap"
        minSdkVersion 23
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"
        preDexLibraries false
        incremental true
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile project(':shared')
    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support:support-v4:25.3.0'
    compile 'com.android.support:multidex:1.0.1'
}

我正在将该模块用于我构建的另一个 java 应用程序,因此我无法将其转换为 android 库。任何帮助将不胜感激

编辑我也尝试让我的应用程序成为一个多 dex 应用程序,但这也不起作用,这是我的应用程序清单

<?xml version="1.0" encoding="utf-8"?>
<manifest package="pwils33.familymap"
        xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="android.support.multidex.MultiDexApplication">
        <activity android:name=".activities.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

    标签: java android


    【解决方案1】:

    尝试以下代码来解决您的问题:

    android {
        defaultConfig {
            minSdkVersion 15
            targetSdkVersion 25
            multiDexEnabled true
        }
    }
    
    dependencies {
        compile 'com.android.support:multidex:1.0.1'
    }
    

    【讨论】:

      【解决方案2】:

      在您应用的模块build.gradle 文件中包含multidex 库:

      compile 'com.android.support:multidex:1.0.1'
      

      然后在defaultConfig 部分添加多索引:

      multiDexEnabled true 
      

      如果您不覆盖Application,请编辑清单的&lt;Application&gt; 标签,并添加:

      <application
              android:name="android.support.multidex.MultiDexApplication">
      

      如果您确实覆盖了Application 类,请从MultiDexApplication 类扩展它(来自上面提到的multidex 库:

      public class AndroidApplication extends MultiDexApplication { ... }
      

      如果您确实覆盖了Application 类并且它实际上扩展了其他类(例如因为使用经常需要从其类扩展的ORM 库),请通过从attachBaseContext 调用来安装multidex:

      MultiDex.install(this);
      

      更多详情请访问Google's documentation

      【讨论】:

      • 我试过了,还是不行,我会更新帖子
      • 这如何应用于具有多个模块的项目?
      【解决方案3】:

      我终于明白了。事实证明,多 dex 库不支持 1.8,因为我的计算机上有 java 8,当它从我的 java 模块中找到编译后的代码时,它被破坏了。我将以下几行添加到我的 build.gradle 到我的 java 模块中,最终让它工作

      sourceCompatibility = 1.7
      targetCompatibility = 1.7
      
      dependencies {
      ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-05
        • 2020-10-19
        • 2014-04-29
        • 2020-07-04
        • 1970-01-01
        • 1970-01-01
        • 2019-12-11
        • 1970-01-01
        相关资源
        最近更新 更多