I have a class name String and a String which containing the class code. For example, "Example" is the name of the class, and
public class Example {
public void example () {System.out.println ("Hello world!"); }
}
The class code.
I looked at the Dexmaker library, but I did not understand if it's possible to compile the generated code into it. And the question is just how to compile the code string under Android?
Not sure if possible at all the compilation within the embedded system but definitelly you can parse and run the code using beanshell:
http://www.beanshell.org/
it is lightweight and easily to embed in your app. Then you can instance the generated class and run whatever you put inside.
There is only one true way: using DexMaker. All examples you can find on DexMaker wiki and especially for current problem (runtime generation code on android).
Related
So I'm trying to follow the simple read.me for Kotshi and get it set up for my project but I seem to be hitting a little snag.
I'm currently at this portion of the read.me
#KotshiJsonAdapterFactory
object ApplicationJsonAdapterFactory: KotshiApplicationJsonAdapterFactory()
but KotshiApplicationJsonAdapterFactory seems to give me an unresolved reference error. Now this sounds like an absolutely silly question but is KotshiApplicationJsonAdapterFactory supposed to be a custom class that I set up? If so I don't see anywhere in the documentation regarding it. My gradle has the two dependencies added so I'm absolutely baffled.
#KotshiJsonAdapterFactory makes Kotshi generate a JsonAdapter factory.
Should be placed on an abstract class that implements
JsonAdapter.Factory.
So yes, KotshiApplicationJsonAdapterFactory is a custom class that you've to setup and which fulfills the above condition.
KotshiJsonAdapterFactory is an annotation that you apply to a class that you write. This will make Kotshi generate a class with the same name as your class + the Kotshi prefix. That class will implement the actual factory.
When you write your class it will look like you have a compile error, but it will not prevent compilation.
The follows was the code which I want to test.
public class Demo {
private static final List<Pair<String, String>> mList;
static {
mList = new ArrayList<>();
mList.add(new Pair<>("F0", "T1"));
mList.add(new Pair<>("F1", "T2"));
mList.add(new Pair<>("F2", "T3"));
}
public String getStr(int pos) {
return mList.get(pos).first;
}
}
I was an android developer. I have get some trouble in test and mock the code.I have use mockito.
I have try some code to test it,but the result was not my expect.
1.First try
#Test
public void test(){
Demo demo=new Demo();
assertEquals(demo.getStr(0),"F0");
/**
* java.lang.AssertionError:
* Expected :null
* Actual :F0
*/
}
2.Second try
#Test
public void test() {
Demo demo = mock(Demo.class);
doCallRealMethod().when(demo).getStr(0);
assertEquals(demo.getStr(0), "F0");
/**
* java.lang.AssertionError:
* Expected :null
* Actual :F0
*/
}
Anyone tell me how can I resolve this problem to make demo.getStr(0) == "F0" by call the real method? Thanks!
===========================
Another question relate to it
I have try an another test to test android.util.Pair class, and the result is that "pair.first" was null,.(There are androidTest and test directory,I put it into test package.Did it impact the result?)
import android.util.Pair;
import org.junit.Test;
import org.mockito.Mockito;
import static org.junit.Assert.assertEquals;
public class DemoTest {
#Test
public void test1(){
Pair<String,String> pair=new Pair("First","Second");
assertEquals("First",pair.first);
//pair.first was null,why?
}
#Test
public void test2(){
Pair<String,String> pair= Mockito.spy(Pair.class);
assertEquals("First",pair.first);
//pair.first was null also,why?
}
}
Why the simple code is correct in real android environment,but failure in test?
I had the same problem too. month ago I have problem with TextUtils class too.
I report this to jUnit but they told me the problem is with android package because in unit test environment you don't have access to platform specific classes
for that pair case you can use this package. this works for me
import android.support.v4.util.Pair;
The problem in your first try is, that the public field "first" is actually null.
Is the Pair class the one from the "javafx.util" package or a custom implementation?
Did you forget "this.first = first" or something similar in the constructor of the "Pair" class?
I would also recommend to change the following line:
assertEquals(demo.getStr(0),"F0");
to
assertEquals("F0", demo.getStr(0));
so that the error is printed correctly.
Your second try does not make any sense. What is the point in mocking the class you want to test?
I think the second example has the same problem as the first one. Pair.first is never set. If you fix that, it should also work (untested).
From Google's Android tools website:
"Method ... not mocked."
The android.jar file that is used to run unit tests does not contain any actual code - that is provided by the Android system image on real devices. Instead, all methods throw exceptions (by default). This is to make sure your unit tests only test your code and do not depend on any particular behaviour of the Android platform (that you have not explicitly mocked e.g. using Mockito).
So how can we solve this?
In other words. If you need a default android class to work properly you either have to include it from a separate repository, or implement it yourself.
In the case of Android's Pair class. You can use android.support.v4.util.Pair instead.
To get access to this class, you can include com.android.support:support-compat:27.0.0 in your build.gradle or dependencies file.
If you are not using Gradle, you can copy the implementation of this file and use it in place of the official one. Or you can try and download the .jar file from this older version https://mvnrepository.com/artifact/com.google.android/support-v4/r7 (I have not tested whether it works)
Another approach (based on this) is to create the class in app/src/test/java/android/util/Pair.java and copy the code from the Android implementation.
This way you don't need extra dependencies. (There may be issues related to the implementation changing after you make the copy, but the dependencies may become stale as well.)
I am new to android. If my question is wrong please forgive me,
My Question is:
Can I write a method in android which can be accessed from anywhere inside my application?
I've studied VB for all these years and now I am trying to program in android, I couldn't stop comparing them when I write code.
In VB we can create modules and access it from anywhere. Is there anything I can do in Android...??
Answers and advises are needed!
You have to create a class with a static method:
public class MyClass {
public static void myMethod() {
// Your code here...
}
}
And you can call it like this: MyClass.myMethod();
You can try extending Application and put your common functions there.
In all activities you can access this via context.
For reference follow this:
http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/
It is a long topic to discuss , but as you have simply ask whether method is accessible or not , then answer is yes but with some respect of java rules.
Android is again like java coding, You can do same thing what we can do with java.
Same Data type
Method Format
Class structure
Inheritance, Public , private , protected, etc.
So while you write your code you should care for that all things and these all thing you know because you are working in VB.
VB module equivalent is not there in java(android),
but some how you can mock them to some extend by using final classes with static methods.
Anyway you need to import the package containing these type of classes wherever you use it.
I'm taking my first foray into Android services and having trouble with compile errors in AIDL files. I am using Eclipse (with Android Development Tools) and Android 4.1. I have the following AIDL files:
Weather.aidl
package ws.hamacher.weatherservice.service;
parcelable ws.hamacher.weatherservice.dto.Weather;
In this file, I get "interface ws.hamacher.weatherservice.dto.Weather should be declared in a file called ws\hamacher\weatherservice\service\ws.aidl." on the parcelable line, but this refers to my Java class!
IWeatherService.aidl
package ws.hamacher.weatherservice.service;
import ws.hamacher.weatherservice.service.Weather;
interface IWeatherService {
void addToWeatherService(in Weather weather);
void deleteFromWeatherService(in Weather weather);
List<Weather> getLocations();
}
Here again, the import statement gives a similar error "interface ws.hamacher.weatherservice.dto.Weather should be declared in a file called ws\hamacher\weatherservice\service\ws.aidl." This should be referring to the first file above right?
Along with that, the method declarations all have errors, to the tune of "unknown type Weather".
Any help would be appreciated.
If you want to send custom object such as Weather class, you should creat a package:
ws.hamacher.weatherservice.dto
write :
Weather.java
in package: ws.hamacher.weatherservice.dto like this:
public class Weather implements Parcelable {
....
}
Then, write Weather.aidl:
package ws.hamacher.weatherservice.dto;
parcelable Weather;
Please see AndroidMusicPlayer and AndroidMusicPlayerClient for real code.
Solved it. My Weather.aidl had to be in the same directory as my parcelable Weather class.
I have the follow scenario to work on. I was given a shared library (libeffect.so) to use in a Android project i am working for a client. I dont have the shared library source code, i have just the .so file with me. The library is pre-compiled to work on android devices. Along with the shared library I have the method signature
public static native void doEffect(int param1, IntBuffer intBuffer);
So now I have some questiosn on how to make the call to this native method, of source, if this is possible having just the .so file, so there they are:
Do I need to place the native method signature in the same package/class as those defined when the .so was or I can use this signature in any package/class in my project that during runtime the jvm will be able to find the method in the shared library? For example, if this shared library was firstly used in a class mypackage.MyClass, do I need to create the same package, class and then put the method signature there?
Where do I need to place this .so file inside my eclipse android project to get this file deployed inside my apk file?
These question might sound noob, but I have never worked with jndi before so I am a bit concerned if calling the method doEffect without any error can be achieve. Any answer that can guide me is very welcome.
Many Thanks
Thiago
Do I need to place the native method signature in the same package/class as
those defined when the .so was or I
can use this signature in any
package/class in my project that
during runtime the jvm will be able to
find the method in the shared library?
For example, if this shared library
was firstly used in a class
mypackage.MyClass, do I need to create
the same package, class and then put
the method signature there?
No need to create same package/class. You can put the method signature in any package.
public class NativeLib {
static {
System.loadLibrary("so_file");
}
public static native void doEffect(int param1, IntBuffer intBuffer);
}
2.Where do I need to place this .so file inside my eclipse android project
to get this file deployed inside my apk file?
You have put this .so file in lib folder of your application . IF lib folder is not there then you can create a lib folder and put the .so file. you can call it by using System.loadLibrary("so_ file");
1 Do I need to place the native method signature in the same
package/class as those defined when the .so was or I can use this
signature in any package/class in my project that during runtime the
jvm will be able to find the method in the shared library?
According to http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/design.html you have to use a matching package and class name.
I've only observed JNI methods where the C side functions are called things like Java_com_company_whatever_SomeClass_someMethod, which means that you have to put the 'native' declarations in a similarly-named Java class.
Use the tool 'nm' or 'nm++' (they're in the precompiled folders in the NDK) to look at the .so file and see what the functions defined in it are called. If you see any starting Java_, those're what you want.
I'm sceptical of the preceding claim that you can call functions which aren't named in the Java_PACKAGE_CLASS_METHOD format; it may be a legacy behaviour if it actually works, but even if you can, it seems dangerous - you might get the wrong one.
2 Where do I need to place this .so file inside my eclipse android
project to get this file deployed inside my apk file?
Your .so lives in libs/armeabi, libs/armeabi-v7a, libs/x86, and/or libs/mips depending on how many platforms you're working with, where 'libs' is a peer of 'src' and 'res'. I don't know whether Android looks in libs/ without the platform qualifier, but there's no evident benefit in that. The situation is slightly complicated by most/all Intel devices including fancy technology allowing them to execute most ARM libraries on x86 hardware.
Further, I like to declare an interface of a JNI class and provide a factory (it's a method here for brevity, but I prefer a factory class) that supplies a no-op implementation of the interface if things go wrong: it facilitates unit testing and also avoids having to mess about testing for null values before calling its methods (assuming you're comfortable that your shipped library will never have missing or changed method signatures - your integration tests should check that):
public interface YourLibI {
#Override
public native yourMethod();
public static final NO_OP = new YourLibI() {
#Override
public void yourMethod(){}
}
}
public class YourLib extends YourLibI {
public newYourLibI() {
try {
return new YourLib();
}
catch (UnsatisfiedLinkError e) {
Log.e("YourLibJNI", "Load failed, returning NO-OP dummy", e);
return YourLibI.NO_OP;
}
}
static {
System.loadLibrary("arbitronSDK");
}
private YourLib() {
}
#Override
public native void yourMethod();
}
I don't normally call interfaces 'xxxI' but I'm assuming your library's JNI class isn't called something nice like UtilityJNI (whereupon I'd call the interface 'Utility').