Reputation: 48218
I am trying to write a java program that uses some functions in an Android project. I don't need an Android device or emulator, I just need to use some of the functions of the Android project.
The problem I'm running into is this: I'm trying to run something like this FROM JAVA:
AndroidProject testService = new AndroidProject(_context);
testService.start();
But "Context" is an Android thing, and I just can't figure out how to create one, or mock one, or get one from my Java class.
I'm not actually interested in the context, I'm really just interested in the testService.start()
, but there is no () constructor for AndroidProject, just the AndroidProject(context)
constructor is available.
I've tried
Context _context;
_context = null;
AndroidProject testService = new AndroidProject(_context);
testService.start();
But I get a Exception in thread "main" java.lang.RuntimeException: Stub!
error
I've also tried
MainActivity myMainActivity = new MainActivity();
Context _context;
_context = myMainActivity.getBaseContext(); // also tried getApplicationContext()
I've started looking into Mocking, but don't know if I'm making it too complicated or not...
MainActivity
class, MainActivity myMockObject =
AndroidMock.createMock(MainActivity.class);
I just want to create some sort of Android dummy context and use it in my java application, to get to the other functions of that Android class... is this possible?
Upvotes: 4
Views: 1202
Reputation: 69208
android.jar
contains only stub implementation of the classes. It provides the means for you app to build, once you have your APK you must run it on an android device or emulator.
Even if you mock the components you need (i.e. Context
) then you will reach the stubs somewhere. If you don't, then the code you are trying to run is not android dependant and you can probably factor it into an external jar
that can be shared between your Java and Android projects.
Upvotes: 2