0

I made a Contact App using Batch Operation like below

final public class BatchOperation {
private final String TAG = "BatchOperation";
private final ContentResolver mResolver;
// List for storing the batch mOperations
private final ArrayList<ContentProviderOperation> mOperations;

public BatchOperation(Context context, ContentResolver resolver) {
    mResolver = resolver;
    mOperations = new ArrayList<ContentProviderOperation>();
}

public int size() {
    return mOperations.size();
}

public void add(ContentProviderOperation cpo) {
    mOperations.add(cpo);
}

public Uri execute() {
    Uri result = null;

    if (mOperations.size() == 0) {
        return result;
    }
    // Apply the mOperations to the content provider
    try {
        ContentProviderResult[] results = mResolver.applyBatch(
                ContactsContract.AUTHORITY, mOperations);
        if ((results != null) && (results.length > 0))
            result = results[0].uri;
    } catch (final OperationApplicationException e1) {
        Log.e(TAG, "storing contact data failed", e1);
    } catch (final RemoteException e2) {
        Log.e(TAG, "storing contact data failed", e2);
    }
    mOperations.clear();
    return result;
}

Everything work fine in debug mode but after I signed my app in release mode I face this problem

E/BatchOperation﹕ storing contact data failed
java.lang.ClassCastException: a cannot be cast to android.content.ContentProviderOperation
        at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:479)
        at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:227)
        at android.content.ContentResolver.applyBatch(ContentResolver.java:951)

in this line

ContentProviderResult[] results = mResolver.applyBatch(ContactsContract.AUTHORITY,
                mOperations);

Here are my proguard file

-optimizationpasses 5 
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''


-dontwarn com.actionbarsherlock.**
-dontwarn org.apache.**
-dontwarn com.commonsware.**
-dontwarn freemarker.**
-dontwarn ezvcard.**

-keep class org.apache.** { *; }
-keep class android.support.v4.app.** { *; } 
-keep class com.actionbarsherlock.** { *; }
-keep class ezvcard.** { *; }
-keep class com.fasterxml.jackson.** { *; }

Thank for your reading,

Tai Tran
  • 1,406
  • 3
  • 15
  • 27

2 Answers2

1

When this kind of things happen I always ask a question to my fellow developers... Are you using Proguard? Try disabling Proguard and compiling into release mode for testing. Also remove optimization by using

-dontoptimize

Also check that your Content Provider is being kept with something like

-keep public class * extends android.content.ContentProvider
Junior Buckeridge
  • 2,075
  • 2
  • 21
  • 27
  • Yes, after disable proguard everything is fine again , – Tai Tran Sep 06 '14 at 02:53
  • Try without the optimization option. I don't use it anymore 'cause its a bit of a trouble maker. – Junior Buckeridge Sep 06 '14 at 02:56
  • I recommend you to use the default proguard script that comes with the sdk. It will save you a lot of time. – Junior Buckeridge Sep 06 '14 at 03:10
  • I experience the same problem. I need to import contacts and calendar events from android. It works in debug mode but the app crash in release mode. -dontoptimize -keep public class * extends android.content.ContentProvider I added those lines in project.properties but the app still crashes. Able to share with me the solutions? – Antoine Murion Nov 15 '16 at 12:31
0

Ah , I find out that Proguard obfuscator android.content.ContentProviderOperation; class. Just add this -keep class android.content.** { *; } everything work fine =)

Tai Tran
  • 1,406
  • 3
  • 15
  • 27