# Limit Methods to only when compiled to android

**URL:** <https://discourse.processing.org/t/limit-methods-to-only-when-compiled-to-android/14408>\
**Category:** Processing for Android\
**Created:** [October 3, 2019, 10:10pm UTC](https://discourse.processing.org/t/limit-methods-to-only-when-compiled-to-android/14408 "2019-10-03T22:10:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [October 3, 2019, 10:10pm UTC](https://discourse.processing.org/t/limit-methods-to-only-when-compiled-to-android/14408/1 "2019-10-03T22:10:29Z")

</div>

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

```auto
//if (android) {
  import android.content.SharedPreferences;
  import android.preference.PreferenceManager;
  import android.content.Context;
  import android.app.Activity;
//}

```

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

```auto
//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?

---

<div class="post-metadata">

**Author:** ![RockyHawk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rockyhawk/32/14384_2.png) [@RockyHawk](https://discourse.processing.org/u/RockyHawk)\
**Post date:** [October 4, 2019, 5:50am UTC](https://discourse.processing.org/t/limit-methods-to-only-when-compiled-to-android/14408/2 "2019-10-04T05:50:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [October 4, 2019, 12:41pm UTC](https://discourse.processing.org/t/limit-methods-to-only-when-compiled-to-android/14408/3 "2019-10-04T12:41:58Z")

</div>

[RockyHawk](https://discourse.processing.org/u/RockyHawk)’s solution above works great!

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

> <https://stackoverflow.com/questions/58229141/compile-source-code-only-when-compiled-for-android/58230324#58230324>
