Groovy的android系统 - 发射活动不能找到 [英] Groovy in android - launcher activity cant be found
问题描述
我试图运行一个简单的Android程序。问题是发射活动是一个常规的文件,这就是所谓的MainActivity.groovy,它驻留在java源文件夹中。这是我的依赖的gradle和项目结构的照片:
i'm trying to run a simple android program. The issue is the launcher activity is a groovy file, it's called MainActivity.groovy and it resides in the java source folder. Here is a photo of my gradle dependencies and project structure:
和这里是Android清单文件:
and here is the android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
和最后这里是错误,当我运行程序:
and lastly here is the error when i run the program:
07-08 17:11:49.297 20173-20173/com.example.uen229.myrxjavaandroidproject2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.uen229.myrxjavaandroidproject2, PID: 20173
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.uen229.myrxjavaandroidproject2/com.example.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.uen229.myrxjavaandroidproject2-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.uen229.myrxjavaandroidproject2-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Suppressed: java.lang.ClassNotFoundException: com.example.MainActivity
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
我试图重新启动我的电脑和IDE。请记住,在MainActivity已更名为MainActivity.groovy。我究竟做错了什么?
i have tried restarting my pc and IDE. Keep in mind the MainActivity has been renamed to MainActivity.groovy. What am i doing wrong ?
更新:下面的说明后这里
我做了我的目录结构看起来像它解决了问题如下:
i made my directory structure looked like the following which resolved the issue:
推荐答案
在您的code你有很多问题:
In your code you have many problems:
- Groovy文件应该是在
常规
文件夹,而不是的Java
- Android不支持常规作为母语,只是Java,所以你需要pre-编译Groovy文件为了让在Dalvik / ART执行它。这样做的方式是在申请做这个工作对你的build.gradle插件。
- Groovy files should be under
groovy
folder, notjava
- Android does not support groovy as a native language, just java, so you need to pre-compile the groovy files in order to let the Dalvik/ART execute it. The way to do so is applying a plugin in the build.gradle that does this work for you.
这是在github插件一个伟大的教程。
This is a great tutorial for the plugin on github.
这篇关于Groovy的android系统 - 发射活动不能找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!