Angel Koh
Angel Koh

Reputation: 13535

Android Youtube API V3 cast exception when Release+Proguard ON

I have an Android app that utilises the youtube.search.list method. This application works fine when in "Debug" and when proguard is disabled in "Release".

When proguard is turned on (minifyEnabled true), I get the following exception when i iterate a list of the SearchResult class

com.google.api.client.util.ArrayMap cannot be cast to 
com.google.api.services.youtube.model.SearchResult

I set the following rules in proguard-rules (according to the advice in the comments in another question "android proguard makes the youtube api oauth disabled?")

-keep class com.google.api.** {
    *;
}

The following is the code segment that retrieves the List < SearchResult > . This part works fine (searchResultList.size() returns 25).

youtube = new Youtube.builder(new NetHttpTransport(),
                              new JacksonFactory(),
                              request->{})
                     .setApplicationName("youtube-search")
                     .build();

search = youtube.search()
                .list("id,snippet")
                .setKey(DeveloperKey.DEVELOPER_KEY)
                .setQ("test") 
                .setMaxResults(25);

 SearchListResponse searchResponse = search.execute();

List<SearchResult> searchResultList = searchResponse.getItems()

The following loop will cause the error to occur

for (SearchResult r: searchResultList){ 
   // program throws exception only in "RELEASE" with proguard on.
}

Any insights is appreciated.

Upvotes: 1

Views: 517

Answers (1)

bbrady
bbrady

Reputation: 36

I have the same problem as you do while building with proguard, I add this line in my proguard rule :

-keepattributes Signature

and it works for me.

From proguard official site :

For example, you should at least keep the Exceptions, InnerClasses, and Signature attributes when processing a library.

Upvotes: 1

Related Questions