Android Studio:包含Google地图时的编译器设置

Jas*_*ker 14 java android java-7 android-studio

我在Android Studio中创建了一个新项目,并添加了Google地图活动.

我收到这些警告:

warning: com/google/android/gms/maps/GoogleMap.class(com/google/android/gms/maps:GoogleMap.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: com/google/android/gms/maps/SupportMapFragment.class(com/google/android/gms/maps:SupportMapFragment.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: com/google/android/gms/maps/model/LatLng.class(com/google/android/gms/maps/model:LatLng.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: com/google/android/gms/maps/model/MarkerOptions.class(com/google/android/gms/maps/model:MarkerOptions.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: com/google/android/gms/maps/model/Marker.class(com/google/android/gms/maps/model:Marker.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
Run Code Online (Sandbox Code Playgroud)

我的猜测是我有一个JDK未命中赛或其他什么.我安装了JDK 7,当我进行javac -version时,我看到1.7.0_65.我在Android Studio的首选项中更改了Project字节码版本,但没有更改这些警告.

我的build.gradle有这个

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // You must install or update the Google Repository through the SDK manager to use this dependency.
    compile 'com.google.android.gms:play-services:5.0.77'
    compile 'com.android.support:support-v13:18.0.+'
}
Run Code Online (Sandbox Code Playgroud)

我需要做些什么来修复这些警告,还是应该在Android Studio中忽略它们?

小智 12

"主要版本"表示Java版本.Java 7 = 51,Java 6 = 50.代码是为Java 7编写的,这是Android的dex支持的.我不确定你所构建的是没有为Java 7设置的,但这就是问题所在.项目中的Maven构建工作正常.我没有看到你提到的错误,也可能与Java 6 vs 7有关.

  • 我添加了compileOptions {sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7},这使得错误消失了.这是最好的方法吗? (2认同)

Chr*_*lay 6

我能够根据Jason Hocker的提示和答案来解决这个问题.将此添加到您的androidgradle任务:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
Run Code Online (Sandbox Code Playgroud)

请注意,您必须安装Java 7 JDK.我还必须将此添加到我gradlew的允许gradle找到正确的JDK:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
Run Code Online (Sandbox Code Playgroud)