Bobbake4
Bobbake4

Reputation: 24857

Create API jar like Android does with stubbed methods?

I'm looking to distribute a custom API and know the classes will be available at run time. I would like my public and protected methods / classes to be included in a jar I can distribute but I don't want any other source code and I would like to throw an exception if that jars code is actually executed.

This is the exact behaviour the Android framework jar has when you attempt to execute the jar directly.

My question is how to I create the same jar from my source without manually going through and creating each stubbed method. I would like this to scale as my API grows.

Upvotes: 5

Views: 867

Answers (2)

Mixaz
Mixaz

Reputation: 4178

I believe you can use for that purpose the mkstubs tool: https://github.com/android/platform_development/tree/master/tools/mkstubs

As @CommonsWare mentioned stubs in AOSP are generated by javadoc DroidDoc script, read here: How are .java files in android_stubs_current_intermediates directory generated?

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83557

One possibility is to write a Java compiler which outputs a copy of your source code with empty method bodies or a body work a single statement which throws an exception. Then you can use normal dev tools to compile the generated classes.

Upvotes: 0

Related Questions