JNI_CreateJavaVM() in Android - android-ndk

There are 2 ways to make Android native C/C++ to call Java code.
Java app calls the native functions first. The native functions save the JavaVM and JNIEnv, and then use them to call Java code, say Java class X.
The native functions call the same Java class X. In this case, we need a JVM to run the Java code. JNI_CreateJavaVM() can be used to do it.
I find the JVMs created by the above 2 ways are different.
Here is the Java class X.
public class X
{
public X()
{
Log.d("X", "X()");
}
}
The Log.d() can be run successfully in the way 1. However, in the way 2 I get the following error message from logcat:
"No implemention found for native Landroid/util/Log;.println_native(IILjava/lang/String;java/lang/String;)I"
I am thinking the JNIEnv got from JNI_CreateJavaVM() misses something. How could I solve the problem if I have to use the way 2?

Maybe the code in com.android.internal.util.WithFramework can help you.
See my similar question here

Related

Use of Method stubbed in Moles inside a Shims context

Recently while going through the automated test cases of a project I came across a piece of code which was something like this.
[TestMethod]
public void UpdateTtWebScResearchIdt()
{
using (ShimsContext.Create())
{
// Some code
SomeNamespace.Moles.MSubCom.StringFormatStringStringArray = (x, y) => "gLibErr";
//Assert
}
}
When I debug this test method, the compiler shows the following error
MolesInvalidOperationException.
At the line where Moles method is stubbed i.e.
SomeNamespace.Moles.MSubCom.StringFormatStringStringArray = (x, y) => "gLibErr";
The detailed message shows this.
"Moles requires tests to be IN an instrumented process.
In Visual Studio Unit Test, add the following attribute to your unit test method:
add this attribute
[TestMethod]
[HostType(Moles)]
public void Test()
{
...
}
Extensions are also available for most unit test frameworks. Please refer to the Moles manual.
But adding the aforementioned attribute does not solve my problem either.
I think that the use of moles inside a shimmed method is problematic.
I need an another opinion on this (or many for that matter).
And if someone can suggest a solution that'd be awesome.
Thanks.
The only problem with your initial code is the missing [HostType(Moles)]. Next you have to be sure you are running your tests with the Visual Studio test runner. If you have installed an other test runner, it could be that it is not loading the Moles host.
moles-requires-tests-to-be-in
I could not find why it was written like this so I rewrote the entire test method using fakes assembly. Now it is working fine.

Regression of IntelliJ IDEA 14 support for Spock Framework?

After upgrading from IDEA 13.1.x to 14.x (14.0.2 at the moment) I see the support for Spock Framework Mock() and Stub() methods got worse.
To be more specific, I mean in-line methods stubbing/mocking with closures like:
MyType stub = Stub {
myMethod() >> { /* do something */ }
}
IDEA 13 is aware of available methods for stubbed type, which is visible on the below screen shot.
size() method is not underlined. It can be navigated to, auto-completed, checked for possible argument types and so on - usual IDE stuff. The same is possible with any other List method inside of the 'stub closure'.
While IDEA 14 lacks this feature which really is a pity. The screen shot below shows it.
size() method is underlined and greyed out. IDE seems to not have a clue what's up.
The same applies to Mock { } method event if invoked with a type as an argument like Mock(MyType) { } (and Stub(MyType) { } respectively)
My question is - is it only me or that's a bug/regression? Or maybe I need to adjust some settings?
EDIT: seems it's a bug / regression. I raised a bug in youtrack. Up vote, please.
There is a bug in storage system, i.e. GDSL works itself, but state is inconsistent across IDE startups.
As a temporary solution:
Project View -> External Libraries -> spock-core
open org.spockframework.idea.spock.gdsl in Editor
wait until Notification about disabled GDSL comes out
use Activate link in the Notification
You should enable GDSL every time you start up your Idea.
This bug is fixed and the fix will be released asap.

Using MEF and Lazy<T,TMetadata> under VS2012

I'm in the process of taking some MEF code I wrote in VS2010, and writing it again in VS2012. Unfortunately, I got stuck on this simple interface:
public interface IModulesContainer
{
[ImportMany]
IEnumerable<Lazy<IModule, IModuleMetadata>> Container { get; }
}
VS2012 has a compile error on this:
Error Using the generic type 'System.Lazy<T>' requires 1 type arguments
I know there are two System.Lazy<> generic classes, one that takes one parameter type, and the other takes two (2nd being metadata).
I can't figure out how to get VS2012 to recognize the latter class. (Both are under the System namespace)
VS2010 sees it just fine. What am I missing?
Thanks in advance,
--Eric

How to force Monotouch AOT Compiler to see a nested generic method?

I've had to jump through hoops, but I've almost managed to get ServiceStack working on iOS with Monotouch in my project. One runtime JIT exception is holding out:
System.ExecutionEngineException: Attempting to JIT compile method 'ServiceStack.Text.Json.JsonTypeSerializer:GetWriteFn<int> ()' while running with --aot-only.
The offending code is quite simple:
internal WriteObjectDelegate GetWriteFn<T>()
{
return JsonWriter<T>.WriteFn();
}
As a test, I modified the SS code to make the internal methods and types public and included the following in the startup code of my project (to actually get called).
var ick = ServiceStack.Text.Json.JsonWriter<int>.WriteFn();
var erk = ServiceStack.Text.Json.JsonTypeSerializer.Instance.GetWriteFn<int>();
This still doesn't alert the AOT for some reason, I get the exception when the code above executes! Is this because the generic parameter is a value type? Or is it because these are static classes and methods? How can I force Monotouch to AOT the methods above?
The SS code in question is in JsonTypeSerializer.cs and JsonWriter.Generic.cs at:
https://github.com/ServiceStack/ServiceStack.Text/tree/master/src/ServiceStack.Text/Json
There are some generic limitations in monotouch now. I think you should check your code to one of them.

error calling managed code from unmanaged in C++/CLI

I'm writing a "driver" for a program, the driver communicates with some devices on network. I already have C# software working with the devices, so the plan is to reuse code.
So the driver dll is really an interop between program and and already availible assemblies, it's written in C++/CLI. The program calls methods described in interface, the interop dll calls C# code, that is how I see it.
I implement methods to be called by program using #pragma unmanaged
DeviceSearch::DeviceSearch(IDeviceSearchHandler* handler):m_handler(handler)
{
ManagedWrapper mw;
mw.Init();
}
ManagedWrapper is implemented in managed code, obviously
void ManagedWrapper::Init()
{
//some code
}
However, the problem rises here. If the Init() is empty or calls methods/classes defined in C++, it's working ok. However, if I try to call the C# code (which is referenced using #using , where Facade.dll is the C# dll which performs some functions), I get access violation exception right when mw.Init() is called, not even within it.
Am I missing something really obvious I should do to make the interop work? Most information in the net just tells that it should "just work"
See if this helps:
According to How can i use a C# dll in a Win32 C++ project?
"Define an abstract interface class in your native C++ code, then create a concrete subclass inside the managed C++ DLL. Call into your C# objects in the method implementations.
Finally, export a factory function that will instantiate the implementation class and return a base-class pointer that your native code can use."

Resources