private KeysetManager generateKeysetAndWriteToPrefs()
        throws GeneralSecurityException, IOException {
      if (keyTemplate == null) {
        throw new GeneralSecurityException("cannot read or generate keyset");
      }

      KeysetManager manager = KeysetManager.withEmptyKeyset().add(keyTemplate);
      int keyId = manager.getKeysetHandle().getKeysetInfo().getKeyInfo(0).getKeyId();
      manager = manager.setPrimary(keyId);
      KeysetWriter writer = new SharedPrefKeysetWriter(context, keysetName, prefFileName);
      write(manager.getKeysetHandle(), writer, masterAead);
      return manager;
    }
Builds and returns a new {@link AndroidKeysetManager} with the specified options.

@throws IOException If a keyset is found but unusable.
@throws KeystoreException If a master key is found but unusable.
@throws GeneralSecurityException If cannot read an existing keyset or generate a new one.
java_src/src/main/java/com/google/crypto/tink/integration/android/AndroidKeysetManager.java