dex文件不包含MainActivity类 [英] dex file does not contain MainActivity class
问题描述
运行我的应用程序时,出现以下错误:
When running my app I get the following error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.name.app/com.name.app.MainActivity}:
java.lang.ClassNotFoundException: Didn't find class "com.name.app.MainActivity" on path:
DexPathList[[zip file "/data/app/com.name.app-2/base.apk"],
nativeLibraryDirectories=[/vendor/lib, /system/lib]]
我已经使用Android Studio apk分析器(构建">分析APK")分析了我的apk(app/build/outputs/apk/debug/app-debug.apk),classes.dex
的结构为
I have used the Android Studio apk analyzer (Build > Analyze APK) to analyse my apk (app/build/outputs/apk/debug/app-debug.apk) and the structure of the classes.dex
is
java/
lang/*
util/*
io/*
com/google/devtools/build/...
我注意到我自己的类以及MainActivity都丢失了. 就我所了解的dex文件(来自这个 SO问题)我自己课程也应该在那里. 因为他们不在,所以我明白了为什么我得到找不到我的班级的错误.
I noticed that my own classes, and thus the MainActivity, are missing. For as far as I understand dex files (from this SO question) my own classes should be there, too. As they are not there, I see why I get the error that my class is not found.
我在自己的设备:Samsung,Lollipop 5.0.2,API 21和虚拟设备:Google Pixel,Oreo 8.0.0,API 26上收到此错误.
I get this error on my own device: Samsung, Lollipop 5.0.2, API 21, and on a virtual device: Google Pixel, Oreo 8.0.0, API 26.
我尝试过的事情
我在Google上搜索并找到了多个建议,但没有一个对我有用. 这些建议是:
I have googled and found multiple suggestions, but none of them worked for me. These suggestions were:
-
禁用即时运行. 文件">设置">构建,执行,部署">即时运行">取消选中即时运行.
Disable instant run. File > Settings > Build, Execution, Deployment > Instant Run > uncheck instant run.
清理项目.生成>清理.然后重新构建项目,依次选择构建">重新构建".
Clean the project. Build > Clean. Then rebuild the project, Build > Rebuild.
使缓存无效/重启.
从设备上卸载应用程序,然后重试.
Uninstall the application from the device and try again.
在Manifest
中使用不同的路径命名.在activity
标签中将android:name=".MainActivity"
更改为android:name="com.name.app.MainActivity"
.
Use different path naming in the Manifest
. Change android:name=".MainActivity"
into android:name="com.name.app.MainActivity"
in the activity
tag.
禁用预删除功能,例如此答案.
Disable pre-dexing, as in this answer.
在build.gradle
dexOptions {
preDexLibraries false
}
- 启用multidex,然后点击此链接.首先在
build.gradle
中启用multidex,然后将MultiDexApplication
添加到清单中.我的MainActivity
扩展了AppCompatActivity
(因此它没有扩展Application
). - Enable multidex, followed this link. First enabled multidex in
build.gradle
, and then added theMultiDexApplication
to my manifest. MyMainActivity
extendsAppCompatActivity
(and thus it does not extendApplication
).
启用multidex会产生类似的错误:
Enabling multidex gives a similar error:
java.lang.RuntimeException: Unable to instantiate application android.support.multidex.MultiDexApplication:
java.lang.ClassNotFoundException: Didn't find class "android.support.multidex.MultiDexApplication" on path:
DexPathList[...]
以下是启用multidex的新增功能.
Below are the additions to enable multidex.
build.gradle
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 26
multiDexEnabled true
}
...
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
清单(只是相关部分)
<application
android:name="android.support.multidex.MultiDexApplication" >
...
</application>
备注 还有与图书馆相关的建议,请参见此答案. 但是,这是针对Eclipse的,我不确定是否可以解决我的问题. Gradle应该处理所有依赖关系,对吗? 以防万一,这是我在Gradle(Kotlin版本1.2.51)中列出所有依赖项的方式:
Remark There was another suggestion related to libraries, see this answer. However, this was targeted at Eclipse and I'm not sure if it could be a solution to my problem. Gradle should deal with all the dependencies, right? Just in case, here is how I listed all my dependencies in Gradle (Kotlin version 1.2.51):
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-simplexml:2.3.0'
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testImplementation 'org.junit.platform:junit-platform-engine:1.2.0'
testImplementation 'org.jetbrains.spek:spek-api:1.1.5'
testImplementation 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5'
}
更新
我正在使用Android Studio 3.1.3.我尝试使用另一台笔记本电脑(通过版本控制,使用Android Studio 3.1.3)运行我的应用程序,但未出现错误.由于文件完全相同,我怀疑我的Android Studio设置有问题.
I'm using Android Studio 3.1.3. I have tried to run my app using another laptop (via version control, using Android Studio 3.1.3) and there the error does not appear. As the files are exactly the same, I suspect there is something wrong with my Android Studio setup.
删除Android Studio及其所有设置,然后从此处并没有解决问题.
Removing Android Studio and all its settings, and then downloading and installing Android Studio from here did not solve the problem.
推荐答案
我的文件夹的结构是
.gradle/
.idea/
app/
build/
src/main
gen/
java/
res/
build/
gradle/
gen
文件夹不应在此处.
此文件夹包含生成的文件,例如R.java
.
这些文件也(但正确)在app/build/generated/source/*
中.
只需删除gen
文件夹并清理项目即可解决问题.
The gen
folder should not be here.
This folder contained generated files, like R.java
.
These files were also (however correctly) in app/build/generated/source/*
.
Simply deleting the gen
folder and cleaning the project solved the problem.
我不知道那个gen
文件夹如何在那里结束,因为我从磁盘上删除了项目并克隆了git存储库(没有该文件夹)了好几次.
一旦找到答案,便会更新此答案.
I have no idea how that gen
folder ended up there, as I removed the project from my disk and cloned the git repository (that does not have this folder) several times.
As soon as I find out how it got there, I will update this answer.
这篇关于dex文件不包含MainActivity类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!