2

我是 OSX 的菜鸟。今天尝试在Android Studio中使用RxJava和Retrolambda,Retrolambda需要gradle--retrolambda。

所以我开始配置:

1.我在 ~/.bash_profile 中粘贴了一些代码来设置 JAVA7_HOME 和 JAVA8_HOME

export JAVA7_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home   
export PATH=$JAVA_HOME/bin:$PATH 
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export JAVA8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home 
export PATH=$JAVA_HOME/bin:$PATH 
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH

看起来工作正常,我使用终端检查它

echo $JAVA8_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home
echo $JAVA7_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

然后我关注其他文章

2.Edited Android Studio -> Preference -> Path Variables 添加“JAVA8_HOME”和“JAVA7_HOME”,同上

3.Edited build.gradle(项目)

dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'me.tatarka:gradle-retrolambda:3.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

4.Edited build.gradle(app)

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'  #Added here!


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.ezioshiki.demoproject"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    #Added here!
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

#Added here!
String java8 = getJavaVersion(8)
String java7 = getJavaVersion(7)
retrolambda {
    jdk java8 //have tried with JAVA8_HOME
    oldJdk java7 //have tried with JAVA6_HOME
    javaVersion JavaVersion.VERSION_1_7 //have matched the above with VERSION_1_6
}

String getJavaVersion(Integer v) {
    def sout = new StringBuffer()
    def proc = "/usr/libexec/java_home -v 1.$v".execute()
    proc.consumeProcessOutput(sout, new StringBuffer())
    proc.waitForOrKill(1000)
    return sout.toString().replace("\n", "").replace("\r", "")
}



dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'io.reactivex:rxjava:1.0.14'
    compile 'net.orfjackal.retrolambda:retrolambda:2.0.6'
    compile 'me.tatarka:gradle-retrolambda:3.2.2'
}

我关注了https://github.com/evant/gradle-retrolambda/issues/61,因为 gradle-retrolambda 官方建议找不到 Java8,这个问题有效。

但它给了我:

Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileRetrolambdaDebug
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:preDexDebug

意外的顶级异常:

java.lang.RuntimeException: Exception parsing classes

at com.android.dx.command.dexer.Main.processClass(Main.java:752)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:718)
at com.android.dx.command.dexer.Main.access$1200(Main.java:85)
at com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1645)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:672)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:574)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:311)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)

Caused by: com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)

at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
at com.android.dx.command.dexer.Main.parseClass(Main.java:764)
at com.android.dx.command.dexer.Main.access$1500(Main.java:85)
at com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684)
at com.android.dx.command.dexer.Main.processClass(Main.java:749)
... 12 more
1 error; aborting
Error:Execution failed for task ':app:preDexDebug'.


com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1


Information:BUILD FAILED
Information:Total time: 2.143 secs
Information:1 error
Information:0 warnings
Information:See complete output in console

我不能处理这个。任何人都可以帮助我吗?非常非常感谢:)

4

2 回答 2

0

这就是我配置我的 OSx 环境的方式,

  1. 安装最新的 java 8

  2. 编辑您的 .bash_profile (位于您的主目录中)并添加以下行:

    导出 JAVA_HOME=/usr/libexec/java_home 导出 JAVA7_HOME=/usr/libexec/java_home -v 1.7

*现在“java -version”命令必须显示java 1.8,但retrolambda也可以访问java 7主目录。

  1. 将这些行添加到您的项目 build.gradle 文件中:

    依赖项 { ... 类路径 'me.tatarka:gradle-retrolambda:3.2.5' 类路径 'com.neenbedankt.gradle.plugins:android-apt:1.8'

  2. 将 retrolambda 插件应用于您的 android 应用程序模块 build.gradle:

    应用插件:'me.tatarka.retrolambda'

*无需向该模块添加编译依赖项。

于 2016-05-26T06:05:01.500 回答
0

尝试将所有 .java 文件移动到根包。这对我有用

于 2015-10-04T12:49:16.960 回答