Limit Methods to only when compiled to android

I am making a game for both android and pc, and have to import stuff exclusive for android.

I want to be able to do something like this, so in the case of me compiling for a non-android version, it wont give compile errors

//if (android) {
  import android.content.SharedPreferences;
  import android.preference.PreferenceManager;
  import android.content.Context;

Along with a way to have methods that use said exclusive imports to be ignored when not compiling for android

//only compile when android
void saveInt(int n, String name) {
  SharedPreferences sharedPreferences;
  SharedPreferences.Editor editor;
  //~~stuff not necessary for example~~

Is this possible or will I just have to comment out all of the code that does not apply manually every time?

It would be best to have the code you want to combine with the two types (android & pc) on it’s own, and then stitch that code together using a separate Android and PC class. So the main draw classes and input classes would be separate between them, but make all the game play, menu renders and everything in a separate combined class.

In a game for example you would combine the game play and then have individual ways to interact in the game.

Essentially you would export to android using your Android class + Core class
and to PC using PC class + Core class.

1 Like

RockyHawk’s solution above works great!

the answer to the same question on stackoverflow is a different neat solution