Android ShareActionProvider work in debugging mode but doesn't in real device
Asked Answered
B

2

5

When I debug my app on device, using Eclipse, this is the result, that is what I want:

App image from debug mode

The 'share icon' works fine!

HERE, THE PROBLEM!!!! WHEN I ADD THE APP ON GOOGLE PLAY, AND THEN I INSTALL THE APP FROM IT, THIS IS THE RESULT:

App image after install from Google Play

The 'share icon' doesn't work. It's clickable, but it does nothing. The icon doesn't appear, just the title: "share"

This is the code:

private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    // Set up ShareActionProvider's default share intent
    MenuItem shareItem = menu.findItem(R.id.action_share);
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
    mShareActionProvider.setShareIntent(getDefaultShareIntent());  
    return super.onCreateOptionsMenu(menu);
}

private Intent getDefaultShareIntent() {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.app_name));
    intent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.app_play_address));
    return intent;
}

And this is main.xml file:

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:yourapp="http://schemas.android.com/apk/res-auto">

<item 
    android:id="@+id/action_share"
    android:title="@string/share"
    yourapp:showAsAction="ifRoom"
    yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />

</menu>

In styles.xml file:

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

styles.xml file in v-11:

<resources>

<!--
    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat">
    <!-- API 11 theme customizations can go here. -->
</style>

styles.xml file in v-14:

<resources>

<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat">
    <!-- API 14 theme customizations can go here. -->
</style>

and, finally, on manifest.xml:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
Brandy answered 21/10, 2013 at 15:21 Comment(0)
B
16

Are you using Proguard for your releases?

I do, and had the same problem until I realised the class ShareActionProvider gets mangled by Proguard and thus I had to take it out of Proguard-mangling.

So, in your proguard-project.txt, include the following line:

-keep class android.support.v7.widget.ShareActionProvider { *; }
Bottoms answered 16/5, 2014 at 14:59 Comment(2)
Now, with AndroidX replacing Support, it would be: -keep class androidx.appcompat.widget.ShareActionProvider { *; }Pardoner
Thank you for expanding to my answer for AndroidX Jetpack support!Bottoms
A
1

Do you have the right permissions in your manifest?

http://developer.android.com/guide/topics/security/permissions.html

Almond answered 21/10, 2013 at 15:33 Comment(4)
Do I need permissions to use "ShareActionProvider"?Brandy
Your link is for Api 14 and higher. My app in for Api 7 and higher. I use android.support.v7.widget.ShareActionProvider!Brandy
Here's a great blog post that explains the usage of it pre-honeycomb. wptrafficanalyzer.in/blog/…Almond
I just noticed this, for future readers: you don't need permissions to use the ShareActionProvider (as it only queries share-target apps and builds a specific SHARE-Intent when the user clicks on its button-representation). Also, the Sherlock-library as mentioned in the blog-post from 2012 is discontinued and surpassed by the aforementioned android.support library.Bottoms

© 2022 - 2024 — McMap. All rights reserved.