javaandroidandroid-youtube-api

declare Google api key in manifest (For youtube API)


Using the maps API i can declare my key in manifest:

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AlOtOfNumBersAndLetters123" />

But in the Youtube API i have to do it programatically (AFAIK):

String API_KEY = "AlOtOfNumBersAndLetters123";
youTubePlayerFragment.initialize(API_KEY, myOnInitializedListener);

How can I use the key declared in the manifest for Youtube API as well?


Solution

  • You can use the existing meta-data or define a custom one in the AndroidManifest:

    <meta-data android:name="com.my.app.myYoutubeID" android:value="AlOtOfNumBersAndLetters123" />
    

    And use it at runtime (both for the dedicated or the existing meta-data):

    String API_KEY = context.getPackageManager().getApplicationInfo(context.getPackageName(),PackageManager.GET_META_DATA).metaData.getString("com.my.app.myYoutubeID");
    //String API_KEY = context.getPackageManager().getApplicationInfo(context.getPackageName(),PackageManager.GET_META_DATA).metaData.getString("com.google.android.maps.v2.API_KEY"); //this will use the Maps meta-data
    youTubePlayerFragment.initialize(API_KEY, myOnInitializedListener);