3

Is it possible to create an universal Android App for both TV and Mobile but with different main Activities, Themes, Styles and so on?

I tried adding activity to the Mainfest file:

<application
        ... >
    <!-- Mobile -->
    <activity
        ...>

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <!-- TV -->
    <activity
        ...
        android:theme="@style/Theme.Leanback">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
        </intent-filter>

    </activity>
</application>

But as I understand I cant have 2 "android.intent.action.MAIN" activities?

When starting new project in Android Studio and selecting both Mobile and TV as supported "form factors" it creates project with 2 separate modules, so actually these are 2 separate apps, right? But I want to be able to share 90% of code between them. How do I do that?

Thanks in advance.

oleynikd
  • 896
  • 13
  • 33
  • "But as I understand I cant have 2 "android.intent.action.MAIN" activities?" -- in general, you can. You can have 1,000 of them, if you wanted. The app that we think of as Google Maps usually has three: Maps, Street View, and Navigation. You can certainly create [a single app that works on Android TV and on mobile devices](https://github.com/commonsguy/cw-omnibus/tree/master/Presentation/Decktastic). In your case, one set of file templates for one IDE code generated two modules for you. That does not mean that this is the only possible solution. – CommonsWare Feb 03 '15 at 00:18
  • Now, in my case, I used one launcher activity for all form factors, and I was not distributing the app through the Play Store. – CommonsWare Feb 03 '15 at 00:20
  • 1
    @CommonsWare OK, let's say I have 2 .MAIN activities like in my question. How do you think, what activity will startup if I press on App icon on Android TV (Lollipop). I bet you think that one with "android.intent.category.LEANBACK_LAUNCHER", but NO! The first one with "android.intent.action.MAIN" comes up! Why? – oleynikd Feb 03 '15 at 14:12
  • 2
    I would consider that to be a bug in the Android TV home screen implementation. The *point* behind `LEANBACK_LAUNCHER` was to indicate the activity that should appear in the Android TV home screen launcher. What you may need to do is have one `MAIN` activity, with both categories (`LAUNCHER`, `LEANBACK_LAUNCHER`), set up with `Theme.NoDisplay`. That activity can use `hasSystemFeature()` to detect whether it is on a TV or not, then start the appropriate activity. BTW, what Android TV are you testing this on? ADT-1? Nexus Player? Something else? – CommonsWare Feb 03 '15 at 14:16
  • @CommonsWare Thanks for help! I'm using Nexus Player. I'll make more tests with Manifest file and let you know results. Thanks. – oleynikd Feb 03 '15 at 14:24
  • @oleynikd Did you found solution? – Waldmann Jun 29 '22 at 05:18

1 Answers1

3

You can make a new module like a library for both project, and then import inside each gradle and you can share common code between both modules.

f3rn4nd000
  • 434
  • 3
  • 7
  • Thanks for fast reply. This is what I also thought about, but how can this help me to make 1 .apk? If I have 2 separate modules for Mobile and TV - I'll have 2 separate .apk files, right? – oleynikd Feb 03 '15 at 14:10
  • I don't know exactly when you build for release in Android Studio generate 2 separated apk or packing both in only one apk (In AndroidWear if you build an Android Wear App, in the release apk its packed the 2 apk in one). And if you export your common code, layouts and other tings in a library you could make a clean, maintanable, modular and no repeat code in each module. – f3rn4nd000 Feb 03 '15 at 15:36
  • @oleynikd have you found any solution for it ? – nitin tyagi Jun 22 '17 at 09:37