How can I configure Android studio to add new line at end of file?
Asked Answered
D

4

51

I use Android Studio and when I commit to a repo on GitHub and view git diff I see a red marker at end of line for few files with a warning 'No newline at end of file'. How can I configure Android Studio to ensure that it does add a newline at file of file for every file. Thank you.

Deflection answered 7/6, 2014 at 4:19 Comment(0)
F
108

Yes, you can configure Android Studio to automatically add a new line at the end of a file.

File | Settings... menu, then Editor under IDE Settings, then check the Ensure line feed at file end on Save option box.

This will only take effect on freshly edited files after the setting has been enabled.


UPDATE

On newer versions of Android Studio, the preference location has changed to

File > Preferences > Editor > General > Ensure line feed at file end on Save

Farthing answered 22/2, 2015 at 16:35 Comment(4)
It is File → Settings → ...Enchantress
Thanks for the answer. I was looking for justification of why to include a newline at the end of a file but couldn't find it. Why do we do that and why does Github call it out? Is it something with *nix? Edit: Sorry here's the discussion I'm looking forFulks
@Enchantress He's probably referring to Mac. Though on Mac it would be Android Studio > Preferences > ...Shoran
Option label now is Ensure every saved file ends with a line break. Also, for it to work properly, you have to uncheck Keep trailing spaces on caret line.Prognosis
N
19

In the latest Android Studio (2.3.3), the setting has moved.

Android Studio -> Preferences -> Editor -> General -> Ensure line feed at file end on Save

Northumberland answered 16/8, 2017 at 20:38 Comment(1)
In Android Studio 3.5 File -> Settings -> Editor -> General -> Ensure line feed at file end on SaveMobilize
I
10

enter image description here

On Android Studio Bumblbee open Preferences -> General -> enable option "Ensure every saved file ends with a line break"

Inoue answered 10/3, 2022 at 16:21 Comment(0)
I
0

The previous changes for Android Studio (53630 - 52407 and 54132 - 52407: should add a newline at the end of autogenerated xml files) seem to suggest there is no global setting to add a newline for every file.

It may have changed depending on the Android Studio you are using (eclipse-based or the more recent IntellJ IDEA-based), but you can check the formatters associated with the different types of files: they usually have that option "add newline at the end of a (certain type of) file".

Ikey answered 7/6, 2014 at 4:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.