Search code examples
javaandroidintellij-ideacode-generation

Can I create own auto generated code in Android Studio IDE?


For example: I like use next code with library retrofit. I created abstract class and use it for simplify create retrofit API interface implementation. Can I add this code in Android Studio(or IDEA) and use autogenerate when I need?

public abstract class AnyApiBuilder<T> {

    private static final int TIMEOUT = 20;

    public abstract T build();

    protected T build(Class<T> api) {
        return build(api, ServerUri.MAIN_SERVER_URI);
    }

    protected T build(Class<T> api, String uri) {
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(TIMEOUT, TimeUnit.SECONDS);

        return new RestAdapter.Builder()
                .setEndpoint(uri)
                .setClient(new OkClient(okHttpClient))
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .build()
                .create(api);
    }

}

and

public interface GetAppsApi {

    @POST("/get_apps")
    void getApps(@Body GetAppsRequest request, Callback<GetAppsResponse> callback);

    class Builder extends AnyApiBuilder<GetAppsApi> {
        public GetAppsApi build() {
            return build(GetAppsApi.class);
        }
    }

}

Solution

  • Select the code and go to Tools->"Save as Live Template"

    You can then set abbreviation, description and variables.

    The abbreviation is what you type to generate the code followed by a Tab.

    enter image description here