//Interface クラス -> myinterface.jar にパッケージ化
package com.example.my_interface;
import com.example.my_interface.point;
pubic abstract class MyInterface{
publib Object obj;
public abstract Point newPoint(Point p);
}
//--- and other class ----
package com.example.my_interface.point;
public abstract class Point{
public int x;
public int y;
}
//プロジェクト 1 - myinterface.jar をインクルードし、classA.apk にパッケージ化
package com.testing.classa;
import com.example.my_interface.point;
public class ClsA extends MyInterface{
@Override
public Point newPoint(Point p){
Point newP;
newP.x = p.x + 1;
newP.y = p.y + 1;
return newP;
}
}
//メインプロジェクト // myinterface.jarを同梱 // このクラス(ClassB)から、classA.apkファイルの「ClsA」から「newPoint」メソッドを呼び出したい。
package com.testing;
import com.example.my_interface.point;
public class ClsB{
Point p1;
p1.x = 2;
p1.y = 3;
Point newP;
String apkfilePath = '/data/data/com.testing/files/apps/classA.apk';
final File optimizedDexOutputPath = cnt.getDir("outdex", Context.MODE_PRIVATE);
DexClassLoader dLoader = new DexClassLoader(apkfilePath,
optimizedDexOutputPath.getAbsolutePath(),
null, ClassLoader.getSystemClassLoader().getParent());
try{
Class<?> loadedClass = dLoader.loadClass("com.testing.classa.ClsA");
Object obj = (Object) loadedClass.newInstance(); // (*)
Method m = loadedClass.getDeclaredMethod("newPoint", Point.class);
m.setAccessible(true);
newP = (Point) m.invoke(obj, p1); //(**)
}catch(Exception ex){
ex.printStackTrace();
}
Log.d("NewPoint x",Integer.toString(newP.x));
Log.d("NewPoint y",Integer.toString(newP.xy));
}
質問 1. (*): インスタンスの後に返される "obj" を MyInterface クラスにキャストできません。
Object obj = (Object) loadedClass.newInstance();
MyInterface mi = (MyInterface) obj;
-> エラー:
Can not cast com.testing.classa.ClsA to com.example.my_interface.MyInterface`
なぜだめですか?com.testing.classa.ClsA では MyInterface を拡張していましたか?
質問 2. (**) で: エラー、
java.lang.IllegalArgumentException: argument 1 should have type com.example.my_interface.point, got com.example.my_interface.point
メインクラスにはインターフェース myinterface.jar があり、classA.apk にも myinterface.jar があり、それらは一緒に話すことができないと思います。この問題の解決策は何ですか?
p/s: Google で検索したところ、.apk (ここでは myinterface.jar) から一般的な lib を削除するよう提案されましたが、それを削除すると、エラーのためにビルドできなくなります。
助けて、ありがとう。