我是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 - >偏好 - >路径变量 添加了“JAVA8_HOME”和“JAVA7_HOME”,与上面相同
3.Edited build.gradle(Project)
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
我无法解决这个问题。有人可以帮帮我吗?非常非常感谢你:))
答案 0 :(得分:0)
尝试将所有.java文件移动到根包中。这对我有用
答案 1 :(得分:0)
这就是我配置我的OSx环境的方式,
安装最新的java 8
编辑.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主目录。
将这些行添加到项目build.gradle文件中:
依赖{ ... classpath'me.tatarka:gradle-retrolambda:3.2.5' classpath'com.neenbedankt.gradle.plugins:android-apt:1.8'
将retrolambda插件应用到您的Android应用模块build.gradle:
申请插件:'me.tatarka.retrolambda'
*无需向此模块添加编译依赖项。