0

Android Studio 0.8.2 で、Spring を使用して Twitter と統合するアプリを作成しようとしています。Gradleビルドのテストを使用するだけの空のアクティビティを作成するまで、ビルドの問題は無数にありました。

spring-social-twitter に接続しようとするたびに、ビルドが失敗します。Twitter の依存関係をコメント アウトすると、アプリはコンパイルされます。空のアプリでテストしているので、これはアプリ自体とは何の関係もないと確信しています。

私は何を間違っていますか?

私のbuild.gradleファイル:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.example.testapplication"
        minSdkVersion 8
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/notice.txt'
    }
}

configurations.compile {
    exclude module: 'spring-core'
    exclude module: 'spring-web'
    exclude module: 'commons-logging'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:20.+'
    compile 'org.springframework.android:spring-android-rest-template:1.+'
    compile 'org.springframework.android:spring-android-auth:1.+'
    compile 'org.springframework.android:spring-android-core:1.+'
    compile 'org.springframework.security:spring-security-crypto:3.+'
    compile 'org.springframework.social:spring-social-core:1.+'
    compile 'org.springframework.social:spring-social-twitter:1.+' // <- The offending line
    compile 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
}

エラー:

Error:Execution failed for task ':app:dexDebug'.
    > com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\Android\android-studio\sdk\build-tools\android-4.4W\dx.bat --dex --num-threads=4 --output C:\Workspace\TestApplication\app\build\intermediates\dex\debug C:\Workspace\TestApplication\app\build\intermediates\classes\debug C:\Workspace\TestApplication\app\build\intermediates\dependency-cache\debug C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\aopalliance-1.0-b64c80122d373c2ee241b55db78eac4c4c550a82.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\classes-528f16ed29eae5d85d8c6f1ee7d32d83803e6e6d.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\classes-fa1cd323aa3b243f3f4157cb5e43b3768ee49e9b.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\internal_impl-20.0.0-d4c263ffd0c244f7b9af8079e5d62faa86242454.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\jackson-annotations-2.3.0-c8c500c4e2e271888f48319f1d0f4ee141245e21.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\jackson-core-2.3.2-5a10979c76ddf0300365195ed3dcdf3bbf65dba0.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\jackson-databind-2.3.2-a027503e6b72a7b1487e8e935334766d26725e1b.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\javax.inject-1-e86c6ad046940b581fcf8f69e7965f8f5d1cfaf6.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-android-auth-1.0.1.RELEASE-d09921850d19080abfdeefe1fde904485c1b37c8.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-android-core-1.0.1.RELEASE-d006eb074ec6bcaf01f1fcb65dcc840f72c538d1.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-android-rest-template-1.0.1.RELEASE-f254c94632ca185a2fee85575fea1e4e9e68766f.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-aop-4.0.2.RELEASE-87af05a1d636417a8a1c074a0d93dbc6c5902550.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-beans-4.0.2.RELEASE-5afb5560550de06352f4871e0c5351c296555245.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-context-4.0.2.RELEASE-ae3ec0e430000b49e6dabc34fb702e4db3ab96ad.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-expression-4.0.2.RELEASE-1cfe445710559e2affa6317c573db04b9638c918.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-security-crypto-3.2.4.RELEASE-e5c641be2ba64d1b5bda6b6e9778a6043726fc96.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-social-config-1.1.0.RC1-0646da07e81cc32f0ff7a641a47b589c6ee71ed6.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-social-core-1.1.0.RC1-f762fce97f3b62664de8e04d427fea101bc74da1.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-social-twitter-1.1.0.RC1-66dcadc997a346a9caac49e5e66d0d6d60d6e684.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-social-web-1.1.0.RC1-753970492ed13f57671662ceb587dd9aa4f2c882.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\spring-webmvc-4.0.2.RELEASE-a6c8053426ad57c5e0b1eb2de9405ef3c0e118b1.jar C:\Workspace\TestApplication\app\build\intermediates\pre-dexed\debug\support-annotations-20.0.0-bab26a85db10fdf51a5bd499a182514bd4080ed4.jar
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lorg/springframework/beans/BeansException;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
4

2 に答える 2

3

先に進めますが、次の問題で行き詰まる可能性があります。

ビルド ファイルを読み込み (ビルド エラーを再現)、IDE に移動してBeansExceptionクラスに移動するように依頼すると、次のようになります。

のスクリーンショット

が 2 つの異なる場所で定義されていることがわかりますがBeansException、これは Android ビルドでは許可されていないため、表示されているエラーです。各クラスが 1 か所で定義されるようにビルドをセットアップする必要があります。

もう少し調べてみると、このコマンドを使用して依存関係ツリーを表示できます。アプリケーションのモジュール ディレクトリから、次のコマンドを実行します。

../gradlew dependencies

出力の関連部分は次のとおりです。

_debugApk - ## Internal use, do not manually configure ##
+--- com.android.support:appcompat-v7:20.+ -> 20.0.0
|    \--- com.android.support:support-v4:20.0.0
|         \--- com.android.support:support-annotations:20.0.0
+--- org.springframework.android:spring-android-rest-template:1.+ -> 1.0.1.RELEASE
|    \--- org.springframework.android:spring-android-core:1.0.1.RELEASE
+--- org.springframework.android:spring-android-auth:1.+ -> 1.0.1.RELEASE
|    +--- org.springframework.android:spring-android-core:1.0.1.RELEASE
|    +--- org.springframework.android:spring-android-rest-template:1.0.1.RELEASE (*)
|    +--- org.springframework.security:spring-security-crypto:3.1.3.RELEASE -> 3.2.4.RELEASE
|    \--- org.springframework.social:spring-social-core:1.0.2.RELEASE -> 1.1.0.RC1
+--- org.springframework.android:spring-android-core:1.+ -> 1.0.1.RELEASE
+--- org.springframework.security:spring-security-crypto:3.+ -> 3.2.4.RELEASE
+--- org.springframework.social:spring-social-core:1.+ -> 1.1.0.RC1
+--- org.springframework.social:spring-social-twitter:1.+ -> 1.1.0.RC1
|    +--- com.fasterxml.jackson.core:jackson-databind:2.3.0 -> 2.3.2
|    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.3.0
|    |    \--- com.fasterxml.jackson.core:jackson-core:2.3.2
|    +--- org.springframework.security:spring-security-crypto:3.2.0.RELEASE -> 3.2.4.RELEASE
|    +--- org.springframework.social:spring-social-core:1.1.0.RC1
|    +--- org.springframework.social:spring-social-config:1.1.0.RC1
|    |    +--- org.springframework.social:spring-social-web:1.1.0.RC1
|    |    |    +--- javax.inject:javax.inject:1
|    |    |    +--- org.springframework:spring-webmvc:4.0.2.RELEASE
|    |    |    |    +--- org.springframework:spring-beans:4.0.2.RELEASE
|    |    |    |    +--- org.springframework:spring-context:4.0.2.RELEASE
|    |    |    |    |    +--- org.springframework:spring-aop:4.0.2.RELEASE
|    |    |    |    |    |    +--- aopalliance:aopalliance:1.0
|    |    |    |    |    |    \--- org.springframework:spring-beans:4.0.2.RELEASE
|    |    |    |    |    +--- org.springframework:spring-beans:4.0.2.RELEASE
|    |    |    |    |    \--- org.springframework:spring-expression:4.0.2.RELEASE
|    |    |    |    \--- org.springframework:spring-expression:4.0.2.RELEASE
|    |    |    \--- org.springframework.social:spring-social-core:1.1.0.RC1
|    |    \--- org.springframework.social:spring-social-core:1.1.0.RC1
|    +--- com.fasterxml.jackson.core:jackson-core:2.3.0 -> 2.3.2
|    \--- com.fasterxml.jackson.core:jackson-annotations:2.3.0
\--- com.fasterxml.jackson.core:jackson-databind:2.3.2 (*)

このことから、org.springframework.social:spring-social-twitter:1.+最終的にはorg.springframework:spring-beans:4.0.2.RELEASE推移的な依存関係 (ビルドの問題を引き起こしている行としてこれを分離したためにわかった) を介して含まれていることがわかりますが、それは、既に spring-android-core jar にバンドルされている場合は冗長であることを意味します。spring-android-core からの spring-beans への別の依存関係は見当たりません。おそらく、そのクラスは jar にパッケージ化されているだけで、依存関係さえありません。

とにかく、依存関係でこの構文を使用して、Gradle に推移的な依存関係を取得しないように指示できます。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:20.+'
    compile 'org.springframework.android:spring-android-rest-template:1.+'
    compile 'org.springframework.android:spring-android-auth:1.+'
    compile 'org.springframework.android:spring-android-core:1.+'
    compile 'org.springframework.security:spring-security-crypto:3.+'
    compile 'org.springframework.social:spring-social-core:1.+'
    compile('org.springframework.social:spring-social-twitter:1.+') {
        exclude module: 'spring-beans'
    }
    compile 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
}

これを行うと、dex multiple-files-define-a-class エラーは発生しなくなります。ただし、この同様の、しかし異なる重複ファイル エラーが表示されるようになりました。これについては少し考えることができますが、Spring をインスタンス化するテスト アプリを作成せずに修正できるかどうかはわかりません。 SOの回答で行います;)

:app:packageDebug
Error: duplicate files during packaging of APK /Users/sbarta/AndroidStudioProjects/MyApplication/app/build/outputs/apk/app-debug-unaligned.apk
    Path in archive: META-INF/spring.schemas
    Origin 1: /Users/sbarta/.gradle/caches/modules-2/files-2.1/org.springframework.social/spring-social-config/1.1.0.RC1/7afae4a4f83c64c4fd382c77708866e82900e799/spring-social-config-1.1.0.RC1.jar
    Origin 2: /Users/sbarta/.gradle/caches/modules-2/files-2.1/org.springframework/spring-context/4.0.2.RELEASE/6d6a781d99215990da8b398e1cdf09594a683209/spring-context-4.0.2.RELEASE.jar
You can ignore those files in your build.gradle:
    android {
      packagingOptions {
        exclude 'META-INF/spring.schemas'
      }
    }
:app:packageDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK META-INF/spring.schemas
    File 1: /Users/sbarta/.gradle/caches/modules-2/files-2.1/org.springframework.social/spring-social-config/1.1.0.RC1/7afae4a4f83c64c4fd382c77708866e82900e799/spring-social-config-1.1.0.RC1.jar
    File 2: /Users/sbarta/.gradle/caches/modules-2/files-2.1/org.springframework.social/spring-social-config/1.1.0.RC1/7afae4a4f83c64c4fd382c77708866e82900e799/spring-social-config-1.1.0.RC1.jar

これは、2 つの異なる場所から 2 つのファイルが含まれていることを意味しMETA-INF/spring.schemasますが、これも許可されていません。ただし、ここでは、それらの 1 つだけを無視するように dex に指示する柔軟性はありません。次の方法で、それらすべてを無視するように指示できます。

android {
  packagingOptions {
    exclude 'META-INF/spring.schemas'
  }
}

ビルドファイルで。Spring にはそのスキーマ ファイルが必要であると感じているため (どのスキーマファイルですか?)、これを行うと、アプリがまだ機能しない可能性があります。それでも問題が解決しない場合は、別の質問を投稿して、より専門的なヘルプを得ることができます。

この問題の根本が何であるかはわかりません。スキーマファイルが複数あるのは、Spring ライブラリのパッケージングエラーですか? それとも、これを解決することが期待されていますか? それとも、他のビルドシステムは正しいことをするだけですか? わからない。

于 2014-07-16T18:19:35.127 に答える
0

わかりました、これは満足のいく解決策とは言えませんが、コンパイルは完了したので、私は「満足しています」。

問題の根本が何であれ、それは spring-social-twitter バージョン 1.1.0 に固有のようです。build.gradle 行を次のように変更しました。

compile 'org.springframework.social:spring-social-twitter:1.0.5.RELEASE'

...spring-social-twitter:1.+' ではなく、以前に持っていたもので、今では正常にコンパイルされます。動作面はまだテストしていませんが、それはまったく異なる一連の質問になります。

これを見つける方法は、実際の「解決策」と同じくらいばかげていると断言できます。

于 2014-07-18T01:33:46.903 に答える