Reputation: 573
I'm having pretty much the same problem as Android: Cast SDK v3 Crashing in Release build only. The key difference is that my project does it while i'm just debugging and does it on this line
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), mMediaRouteButton);
I have tried making all my variables public but that doesn't do anything. The full code is
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), mMediaRouteButton);
mCastContext = CastContext.getSharedInstance(this);
mSelector = new MediaRouteSelector.Builder()
// These are the framework-supported intents
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build();
mMediaRouter = MediaRouter.getInstance(this);
}
Also I find it worth mentioning that this code worked and I can not for the life of me figure out what caused it to stop working. As far as I can tell it stopped working when I invalidated the cache in Android Studio. This is the error I'm getting
java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage.package/mypackage.package.MainActivity}: java.lang.IllegalStateException: Failed to initialize CastContext.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalStateException: Failed to initialize CastContext.
at com.google.android.gms.cast.framework.CastContext.zzbb(Unknown Source)
at com.google.android.gms.cast.framework.CastContext.getSharedInstance(Unknown Source)
at com.google.android.gms.cast.framework.CastButtonFactory.setUpMediaRouteButton(Unknown Source)
at mypackage.package.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalAccessException: java.lang.Class<mypackage.package.CastOptionsProvider> is not accessible from java.lang.Class<com.google.android.gms.cast.framework.CastContext>
at java.lang.Class.newInstance(Native Method)
at com.google.android.gms.cast.framework.CastContext.zzbb(Unknown Source)
at com.google.android.gms.cast.framework.CastContext.getSharedInstance(Unknown Source)
at com.google.android.gms.cast.framework.CastButtonFactory.setUpMediaRouteButton(Unknown Source)
at mypackage.package.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Upvotes: 3
Views: 6971
Reputation: 4266
If this is happening only in release buildtype then it is related to proguard. Please add the CastOptionsProvider class to the proguard file and recheck
-keep class abc.xyz.videocast.CastOptionsProvider { *; }
For more safety these classes can also be added
-keep class android.support.** { *; }
-keep class com.google.** { *; }
Upvotes: 5
Reputation: 573
This was fixed by making the CastOptionsProvider public. I don't know why this problem occurred to begin with, because it was working fine when it wasn't public until it didn't. I didn't touch the CastOptionsProvider on the build where it started failing. My CastOptionsProvider now looks like this
public class CastOptionsProvider implements OptionsProvider {
@Override
public CastOptions getCastOptions(Context appContext) {
CastOptions castOptions = new CastOptions.Builder()
.setReceiverApplicationId(appContext.getString(R.string.app_id))
.build();
return castOptions;
}
@Override
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}
}
Upvotes: 6
Reputation: 4950
There might be an issue on how you initialize media CastContext, to properly initialize CastContext
, the application must have the implements OptionsProvider
interface:
package com.example.app;
public class CastOptionsProvider implements OptionsProvider {
@Override
public CastOptions getCastOptions(Context appContext) {
...
}
}
and specify its fully qualified class name in the AndroidManifest.xml with the key OPTIONS_PROVIDER_CLASS_NAME_KEY:
...
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="com.example.app.CastOptionsProvider" />
...
Note: All public methods must be called from the main thread.
I did some research and found this related SO ticket which discuss how to initialize CastContext: How to initialize CastContext outside of onCreate method
Upvotes: 6