6

我有一个 Android Maven 项目,想在其中使用Google Guice 3.0

Guice 有一个“No-AOP”版本,它与 Android 兼容。

我如何告诉使用的“No-AOP”版本?

更新 1 (03.05.2013 10:46 MSK):

当我添加对 Google Guice 的依赖项时,在构建期间出现以下异常(mvn clean install):

[INFO] UNEXPECTED TOP-LEVEL EXCEPTION:
[INFO] java.lang.IllegalArgumentException: already added: Lcom/google/inject/Abs
tractModule;
[INFO]  at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:12
3)
[INFO]  at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
[INFO]  at com.android.dx.command.dexer.Main.processClass(Main.java:490)
[INFO]  at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
[INFO]  at com.android.dx.command.dexer.Main.access$400(Main.java:67)
[INFO]  at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
[INFO]  at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpen
er.java:245)
[INFO]  at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.j
ava:131)
[INFO]  at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java
:109)
[INFO]  at com.android.dx.command.dexer.Main.processOne(Main.java:422)
[INFO]  at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
[INFO]  at com.android.dx.command.dexer.Main.run(Main.java:209)
[INFO]  at com.android.dx.command.dexer.Main.main(Main.java:174)
[INFO]  at com.android.dx.command.Main.main(Main.java:91)
[INFO] 1 error; aborting

这是我的依赖项:

<dependencies>
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>4.1.1.4</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>3.0</version>
        <classifier>no_aop</classifier>
    </dependency>

    <!-- Make sure this (robolectric dependency) is below the android dependencies -->
    <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <version>1.0-RC4</version>
    </dependency>
    <dependency>
        <groupId>org.achartengine</groupId>
        <artifactId>achartengine</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.easytesting</groupId>
        <artifactId>fest-assert-core</artifactId>
        <version>2.0M8</version>
    </dependency>
    <dependency>
        <groupId>ru.altruix</groupId>
        <artifactId>ccp-commons</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>ru.altruix</groupId>
        <artifactId>commons</artifactId>
        <version>1.11</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.1.4</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.1.4</version>
    </dependency>
</dependencies>
4

3 回答 3

10

您需要no_aop像这样指定为分类器:

<dependency>
   <groupId>com.google.inject</groupId>
   <artifactId>guice</artifactId>
   <version>3.0</version>
   <classifier>no_aop</classifier>
</dependency>
于 2013-05-03T06:23:57.140 回答
2

如果您还想摆脱 aopalliance 依赖项,请使用以下命令:

<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>3.0</version>
    <classifier>no_aop</classifier>
    <exclusions>
        <exclusion>
            <artifactId>aopalliance</artifactId>
            <groupId>aopalliance</groupId>
        </exclusion>
    </exclusions>
</dependency>
于 2013-06-27T06:55:18.913 回答
1

由于反射性能,Google不鼓励在 Android 上使用 Guice。您可以使用的最后一个no_aop版本是:

compile 'com.google.inject:guice:2.0-no_aop'

'com.google.inject:guice:3.0-no_aop'不再在 Maven 存储库中,以及'com.google.inject:guice:4.1.0-no_aop'.


'com.google.inject:guice:4.1.0在 Android 上崩溃: com.google.common.util.concurrent.ExecutionError: java.lang.ExceptionInInitializerError

compile ('com.google.inject:guice:4.1.0') {
   exclude group: 'aopalliance', module: 'aopalliance'
}

与崩溃NoClassDefFoundError: org.aopalliance.intercept.MethodInterceptor

于 2017-02-17T15:11:38.257 回答