Android .idea/misc.xml's languageLevel tag keeps changing JDKs

JavaAndroidAndroid Studio

Java Problem Overview


The languageLevel key gets changed from JDK_1_8 to JDK_1_7 for reasons I am not aware.

What could be going on?

Does this have something to do with the IDE of other developers working on the project? Maybe they have another Android Studio setting?

Here is what pops up after I notice files under source control have changed:

$ git diff
diff --git a/.idea/misc.xml b/.idea/misc.xml
index fbb6828..5d19981 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -37,7 +37,7 @@
     <ConfirmationsSetting value="0" id="Add" />
     <ConfirmationsSetting value="0" id="Remove" />
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/build/classes" />
   </component>
   <component name="ProjectType">

This is my gitignore in case it matters.

.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

How do I proceed so that it just stays one way or the other?

Java Solutions


Solution 1 - Java

This was driving me nuts for a while. I was able to fix it by explicitly setting the java version in my build.gradle:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

Note that if you're using VERSION_1_7, when you cold launch Android Studio or switch to another project that uses VERSION_1_8, it will modify .idea/misc.xml to use JDK_1_8. Doing a gradle sync will revert it back to using JDK_1_7. If you're using VERSION_1_8, you won't have this issue.

It's not perfect but I found this to be good enough for now.

Solution 2 - Java

Came here from Google after updating to Android Studio 2.2. This might be helpful to others.

Since Android Studio 2.2, the JDK has been bundled with it, instead of you having to download and install it on your system. My project JDK started switching when I updated to 2.2, possibly because of the confusion between the two versions available now - system and embedded.

If you go into File > Project Structure (Mac OS), on the SDK Location tab, there is JDK location. There's now a new setting to use the embedded JDK. Once I switched to it, it solved my issue.

enter image description here

Solution 3 - Java

It seems the file [should be stored under version control][1]. I would propose to keep it in git, but ignore all local changes:

git update-index --assume-unchanged .idea/misc.xml

When switching branches there might be conflict in these files. Then you could use following imlreset script to reset the files:

#!/bin/bash                                                                     
while read f                                                                    
do                                                                              
  [ -f $f ] && git checkout $f                                                    
done <<!                                                                        
app/app.iml                                                           
wear/wear.iml                                                                   
!

Create similar script for ignoring these files if you do it often. [1]: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839-How-to-manage-projects-under-Version-Control-Systems

Solution 4 - Java

I solved this problem when I removed and stopped committing the .idea folder to source control.

The problem is that some of these files are machine specific configurations so sharing them could be an issue.

Removing it and other offending files was a two step git process:

  1. Add this .gitignore (from https://stackoverflow.com/a/32942758/869936):

    #built application files *.apk *.ap_

    files for the dex VM

    *.dex

    Java class files

    *.class

    generated files

    bin/ gen/

    Local configuration file (sdk path, etc)

    local.properties

    Windows thumbnail db

    Thumbs.db

    OSX files

    .DS_Store

    Eclipse project files

    .classpath .project

    Android Studio

    *.iws *.iml .idea .gradle build/ */build/

  2. For each line of the .gitignore, run git rm line from the command line.

Example:

$ git rm *.iws
$ git rm *.iml
$ git rm .idea
$ git rm .gradle
$ git rm build/
$ git rm */build/

Add and commit the changes

Now these files will be generated when you open the Android Studio project and they won't be added to git.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkraftydevilView Question on Stackoverflow
Solution 1 - JavaNoelView Answer on Stackoverflow
Solution 2 - JavaRED_View Answer on Stackoverflow
Solution 3 - JavaPaweł NadolskiView Answer on Stackoverflow
Solution 4 - JavakraftydevilView Answer on Stackoverflow