Apps-android-commons: Decide preferred ordering of import statements

Created on 20 May 2017  路  3Comments  路  Source: commons-app/apps-android-commons

A trivial issue, but I think this has mildly annoyed (via Codacy pull request messages) many people and deserves a discussion.

Personally I'd go with Android Studio's default ordering (performed with Ctrl+Alt+O), at least something close to and is not contradicting to it. This would be easy to live with for developers because it will just work if you install Android Studio (and most do so anyway) - other choices would force them to change rather obscure settings of Android Studio.

To give an example the style will look like this:

// android comes first
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;

// and some other standard prefixes - org., java. etc
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Map;

// non-standard prefixes go next
import fr.free.nrw.commons.settings.SettingsActivity;

// static will come last, for all packages
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;

Changes from the current checkstyle configuration is that:

  • Static imports come first -> static imports come last
  • No special ordering for android., java. etc. (so the ordering would be android.* < fr.* < java.*, for instance.) -> some prefixes including android, java, com, are placed first.

For reference, there does not seem to be an existing consensus on this in the larger Android community, I think we can choose whatever suitable for us. In particular, what appear to be standard styles disagrees with the treatment of static imports:

  1. Google's Java coding style says that static imports should go before non-static imports.
  2. Android Studio's default is non-static first.
  3. Code Style for Contributors to Android (core) says nothing about the ordering of static vs normal imports. (I unfortunately don't know how to implement this flexible rule with checkstyle.)
continuous integration question

Most helpful comment

Just noting that now that it is merged, most files' imports need to be reordered.

Considering flipping it at once could cause edit conflicts, it might be better to wait until a less busy time.

All 3 comments

I agree, I'd go with Android Studio's default ordering as well.

Just noting that now that it is merged, most files' imports need to be reordered.

Considering flipping it at once could cause edit conflicts, it might be better to wait until a less busy time.

I have checked the results of checkstyle and there is no warnings about the ordering of imports now, so closing this.

Was this page helpful?
0 / 5 - 0 ratings