为了重现我面临的问题,我使用了 react-native
示例项目 demo-react-native来自“排毒”。
使用 yarn
安装软件包后,我执行了 2 个命令来运行测试:
1) cd android && ./gradlew assembleRelease assembleAndroidTest -DtestBuildType=release && cd ..
2) npx detox test -l error --configuration android.emu.release
到目前为止,一切都按预期进行,但一旦我像这样在 android/app/build.gradle
文件中添加 Kotlin 标准库的依赖项
dependencies {
...
androidTestImplementation('com.wix:detox:+')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.71" // <-- line added
}
然后再次运行这两个步骤,我收到以下错误
04-05 20:21:06.709 16552 16615 E AndroidRuntime: FATAL EXCEPTION: com.wix.detox.manager
04-05 20:21:06.709 16552 16615 E AndroidRuntime: Process: com.test.app, PID: 16552
04-05 20:21:06.709 16552 16615 E AndroidRuntime: java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/functions/Function1;
04-05 20:21:06.709 16552 16615 E AndroidRuntime: at com.wix.detox.Detox$1.run(Detox.java:134)
04-05 20:21:06.709 16552 16615 E AndroidRuntime: at java.lang.Thread.run(Thread.java:764)
04-05 20:21:06.709 16552 16615 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.jvm.functions.Function1" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.test.app.test-xbA2GW9WwS_BxzRNPa2waQ==/base.apk", zip file "/data/app/com.test.app-g_-tD026mxddTh82TmdYGg==/base.apk"],nativeLibraryDirectories=[/data/app/com.test.app.test-xbA2GW9WwS_BxzRNPa2waQ==/lib/x86, /data/app/com.test.app-g_-tD026mxddTh82TmdYGg==/lib/x86, /data/app/com.test.app.test-xbA2GW9WwS_BxzRNPa2waQ==/base.apk!/lib/x86, /data/app/com.test.app-g_-tD026mxddTh82TmdYGg==/base.apk!/lib/x86, /system/lib]]
04-05 20:21:06.709 16552 16615 E AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
04-05 20:21:06.709 16552 16615 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
04-05 20:21:06.709 16552 16615 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
我尝试在 android/build.gradle
文件中加载 JVM
插件,如所述 here但这没有帮助。
我还能做些什么来让 org.jetbrains.kotlin.jvm
在该项目中工作?
提前谢谢您。
编辑 1
我将 kotlin.jvm.functions.Function1
添加到 MainApplication.java
中,以测试 Kotlin JVM
是否确实可以被项目访问添加以下代码
...
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
public class MainApplication extends Application implements ReactApplication {
private Function1<Throwable, String> errorParseFn = new Function1<Throwable, String>() {
@Override
public String invoke(Throwable t) {
return Log.getStackTraceString(t);
}
};
...
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
Log.d("@@@@@@", "onCreate");
Log.d("@@@@@@", errorParseFn.invoke(new Exception("Test Exception message")));
}
}
再次运行npx detox test -l error --configuration android.emu.release
后,我可以在控制台中看到日志。
04-06 21:24:06.715 23836 23836 D @@@@@@ : onCreate
04-06 21:24:06.716 23836 23836 D @@@@@@ : java.lang.Exception: Test Exception message
这意味着应用本身可以访问 kotlin.jvm
,这意味着 gradle
设置是正确的。
有人知道 DetoxManager.java
如何在运行时访问 Kotlin JVM
吗?
最佳答案
我可以通过在 proguard 文件中添加以下配置来缓解相同的错误。
-dontobfuscate
-keep class kotlin.** { *; }
-keep class kotlin.Metadata { *; }
-dontwarn kotlin.**
关于android - Detox - DetoxManager 在运行时找不到 kotlin/jvm/functions/Function1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61054745/