185

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?

kraftydevil
  • 5,144
  • 6
  • 43
  • 65
  • This might help you: http://stackoverflow.com/questions/32941733/android-studio-sdk-location-changes-for-same-project-using-mac-osx-and-window/32942758#32942758 – kraftydevil Oct 06 '15 at 09:46
  • Have you find a workaround or figured why it changes the language level ? – Falydoor Nov 24 '15 at 14:51
  • 2
    I did. Answer added. – kraftydevil Nov 24 '15 at 16:27
  • 4
    I just want to point out that https://intellij-support.jetbrains.com/hc/en-us/articles/206827587-How-to-manage-projects-under-Version-Control-Systems is the official answer to what should be in `.gitignore`, and this workaround goes against this. You lose some ability to share project properties to all developers, such as inspections/lint settings that we use to prevent some standard bad-practices before even getting to code review. You can just add `/.idea/misc.xml` to the `.gitignore` file to solve this. – Matt Quigley Nov 25 '15 at 00:06
  • 4
    I've noticed this issue myself and it wasn't even after a different team member committed work. I did my own work, pushed a commit, did some more work and notice it had switched on me again. That is what concerns me more. If its a different team member then I know why its changing, but to randomly change during personal local development is concerning and confusing. Any insight into this? – John Shelley May 05 '16 at 18:24
  • 3
    I have the same problem, the language level keeps changing between 1.7 and 1.8. – Han He Aug 02 '16 at 01:53
  • 1
    topic related to http://stackoverflow.com/questions/17637179/how-to-set-source-1-7-in-android-studio-and-gradle – CrandellWS Jan 04 '17 at 01:11
  • I have the same problem, and as @JohnShelley has indicated, it even happens when I am the only one making any modifications. It also seems like it changes whenever android studio gets updated, but that could be a coincidence... – David Apr 07 '17 at 05:44

4 Answers4

45

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.

Noel
  • 7,350
  • 1
  • 36
  • 26
  • 2
    Currently, do not use nor want to use the embed JDK as suggested in http://stackoverflow.com/a/40083824/1815624 using the gradle option does prevent the changing issue. Might want to note this though https://code.google.com/p/android/issues/detail?id=172115 – CrandellWS Jan 04 '17 at 01:16
  • Should I put this in the project or in the module file? – rraallvv Feb 04 '17 at 12:24
  • @rraallvv the module – Noel Feb 04 '17 at 15:55
  • This kind of "fixes" it for me. I have those options in my gradle file. If I open studio (it does a gradle sync and), it sets misc.xml to 1_8. If I build then, it will get set back to 1_7. If I then sync gradle, it will get set back to 1_8 and building will no longer set it back to 1_7. Doing gradle sync _never_ sets it to 1_7 for me, it is always 1_8 after a gradle sync. Any time I open studio, it gets set to 1_8. – David Apr 07 '17 at 06:13
  • If you want use JDK 1.8: android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } – Beatrice Lin Sep 18 '18 at 07:44
27

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

Pang
  • 9,564
  • 146
  • 81
  • 122
RED_
  • 2,997
  • 4
  • 40
  • 59
  • 7
    I did this (though in Win10) but as soon as i restarted AS i noticed the issue continues :( – CesarPim Nov 12 '16 at 15:24
  • 2
    This works to resolve the issue. As @CesarPim mentions, I see it does resurface when the build is out of sync. Running gradle sync then clears the change out. Overall a nice clean solution, much better than it was before - thanks! – Gene Bo Dec 25 '16 at 04:12
  • 5
    What do you mean @gnB ? With me it keeps going back and forth between 1.7 and 1.8... i wasn't able to find a stable solution. Were you? – CesarPim Jan 12 '17 at 21:02
  • 1
    @CesarPim - after making the update to Android Studio project settings, described in this answer - the *"going back and forth between 1.7 and 1.8"* happens when I first start the IDE and open the project. However, once I call "Run App" and the code/app is successfully launched to the device, then this *"going back and forth.."* goes away – Gene Bo Jan 12 '17 at 21:17
  • 3
    @gnB yes, the same with me, but it still bothers me that it happens every single time i launch AS... it shouldn't happen – CesarPim Jan 13 '17 at 21:29
  • 15
    Still happening in Android Studio 3.0, and this suggestion did not fix it. I already have "embedded JDK" selected, and yet it still keeps changing from 1_7 to 1_8 and back for no apparent reason. – Greg Ennis Sep 19 '17 at 14:05
  • Same here, on Android Studio 3.2.1 and the embedded JDK option is already enabled. – ArdenDev Jan 08 '19 at 06:50
  • Does this work for anyone at all? Can we downvote it in favor of solutions, please? – Andrew Koster Jul 09 '19 at 01:55
11

It seems the file should be stored under version control. 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.

Paweł Nadolski
  • 8,296
  • 2
  • 42
  • 32
  • This doesn't avoid issues when switching branches, if the IDE changes the file, the changes have to removed somehow before you can checkout a different branch. – ergosys Feb 17 '17 at 22:00
  • @ergosys, thanks for comment. Added script which I use in such cases. – Paweł Nadolski Feb 18 '17 at 07:28
  • 1
    Ignoring the file is an anti solution, and not even an beneficial workaround. It does not remedy the cause, it hides the symptomes, and by doing so creates and obscures simple problems so that they become hard to find and fix. – Barry Staes May 07 '18 at 09:35
  • @BarryStaes, thanks for feedback. I did not find perfect solution for this issue (other solutions didn't work) and this one works for me and few other people. Note that this is not completely ignoring the files only hiding the fact they have changed. Since these files can often and randomly change it allows to filter them up when doing git commands. You can still commit them whenever you want. – Paweł Nadolski May 07 '18 at 13:50
1

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.

Community
  • 1
  • 1
kraftydevil
  • 5,144
  • 6
  • 43
  • 65
  • 23
    According to https://intellij-support.jetbrains.com/hc/en-us/articles/206827587-How-to-manage-projects-under-Version-Control-Systems you're supposed to commit most of the `.idea` folder because they _aren't_ machine-specific, except for `workspace.xml` and `tasks.xml`. That being said, it's not very well thought-out in terms of version control, because of problems like these. I figure they'll eventually sort the mess out. – Matt Quigley Nov 24 '15 at 23:56
  • I understand there are discrepancies. At the same time, I'm not aware of any issues on my dev team from using this .gitignore. If I knew more I could advise further, but this is what's working for us right now. If an issue presents itself I'll change my answer. – kraftydevil Nov 25 '15 at 00:16
  • 31
    Yes, we all know how to force git to ignore this file. But the real question is _why_ it keeps changing from JDK_1_8 to JDK_1_7 (and sometimes back again)? – SMBiggs Oct 12 '16 at 07:05
  • Started happening to me with Android Studio 2.2. I think they bundled the JDK starting with AS 2.2, so it's possible that it keeps getting confused between the system one and the one within AS. – RED_ Oct 17 '16 at 10:08
  • 3
    This does not answer the question at all. Which is odd because you asked it. This is how to ignore the problem, which might be fine for you I guess, I would like to know why it keeps changing and how to stop it doing that. – David Jul 20 '17 at 01:52
  • @David I suppose this is a solution to the problem if and only if ignoring these files is acceptable in a particular context. It was acceptable in my use case but not for the majority of others, as evidenced in the voting, which is one reason why I have not selected this answer as correct – kraftydevil Aug 07 '17 at 06:56