我有一个与 apk-plugin 链接的工作代码。在我在 android 模拟器上运行另一个应用程序后,此代码停止工作。
DexClassLoader不再找到 apk 文件。
loadClass开始下降到带有文本的异常:
没有找到类“com.example.testandlib.Testt”
请帮助了解在 android 模拟器上运行另一个应用程序可能会出现什么问题。模拟器重启没有效果。清洁项目也。
插件代码:
package com.example.testandlib
import android.util.Log
class Testt {
fun plus(a: Int,b: Int): Int {
return a + b - 2
}
fun minus(a: Int, b: Int): Int {
return a - b - 2
}
fun stringBoolHello(a: String, b: Boolean): String {
if (b)
return "Trully Hello, " + a
return "Hello, " + a
}
fun arrayHello(a: MutableList<String>): String {
var res = ""
var iter = a.iterator()
iter.forEach { res += it+";" }
return "Hello, " + res
}
fun originalArrayHello(a: Array<String>): String {
var res = ""
for ((index, value) in a.withIndex()) {
res += value+index.toString()+";"
}
return "Hello, " + res
}
}
主要应用程序代码的一部分:
val cont = getApplicationContext()
val apkPath = "/data/data/com.example.pozitivmobile/tmp/app-debug.apk"
val cachePath = getDir("dex",0).absolutePath
val classLoaderr = DexClassLoader(
apkPath,
cachePath,
null,
classLoader
)
try {
val curClass = classLoaderr.loadClass("com.example.testandlib.Testt")
val curObj = curClass.newInstance()
val methodParams = arrayOf(Int.javaClass)
val plusMethod = curClass.getMethod("plus", Int::class.java, Int::class.java)
val minusMethod = curClass.getMethod("minus", Int::class.java, Int::class.java)
val stringBoolHelloMethod = curClass.getMethod("stringBoolHello", String::class.java, Boolean::class.java )
val arrayHelloMethod = curClass.getMethod("arrayHello", MutableList::class.java )
val originalArrayHelloMethod = curClass.getMethod("originalArrayHello", Array<String>::class.java )
val plusResult = plusMethod.invoke(curObj,25,35)
val minusResult = minusMethod.invoke(curObj,35,45)
val stringBoolHelloResult1 = stringBoolHelloMethod.invoke(curObj,"Dmitry",true)
val stringBoolHelloResult2 = stringBoolHelloMethod.invoke(curObj,"Dmitry",false)
val paramHelloArray = mutableListOf<String>()
paramHelloArray.add("odin")
paramHelloArray.add("dva")
paramHelloArray.add("tri")
val arrayHelloResult = arrayHelloMethod.invoke(curObj,paramHelloArray)
val paramOriginalHelloArray = arrayOf("odinn","dvaa","trii")
val OriginalArrayHelloResult = originalArrayHelloMethod.invoke(curObj,paramOriginalHelloArray)
} catch (e: Exception){
Log.d("loadClass",e.message.toString() )
}
编辑:
我换线
val cachePath = getDir("dex",0).absolutePath
到
val cachePath = cont.cacheDir.absolutePath.toString()
现在我的代码正在运行。
然后我返回这条线
val cachePath = getDir("dex",0).absolutePath
我的代码仍然有效!海森堡?