使用 jack android 重新部署后生成 class 的 ClassNotFoundException

ClassNotFoundException with generated class after redeploy with jack android

我正在使用 schematic 库创建一个应用程序,为我生成 SQL 数据库和内容提供程序。我还使用了很多其他库,我将在底部包含我的 gradle 文件。

当我首先部署我的应用程序时,它 运行 是正确的,但是如果我更改代码中的某些内容(即使我只是插入一个日志)并重新部署该应用程序,它会在启动时崩溃并出现以下异常:

02-08 20:53:40.279 29400-29400/hu.bendaf.udacity.popularmovies.popularmoviesapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: hu.bendaf.udacity.popularmovies.popularmoviesapp, PID: 29400
    java.lang.RuntimeException: Unable to get provider hu.bendaf.udacity.popularmovies.popularmoviesapp.data.generated.MovieProvider: java.lang.ClassNotFoundException: Didn't find class "hu.bendaf.udacity.popularmovies.popularmoviesapp.data.generated.MovieProvider" on path: DexPathList[[zip file "/data/app/hu.bendaf.udacity.popularmovies.popularmoviesapp-1/base.apk"],nativeLibraryDirectories=[/data/app/hu.bendaf.udacity.popularmovies.popularmoviesapp-1/lib/arm64, /system/lib64, /vendor/lib64]]
    at android.app.ActivityThread.installProvider(ActivityThread.java:5856)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:5445)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5384)
    at android.app.ActivityThread.-wrap2(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassNotFoundException: Didn't find class "hu.bendaf.udacity.popularmovies.popularmoviesapp.data.generated.MovieProvider" on path: DexPathList[[zip file "/data/app/hu.bendaf.udacity.popularmovies.popularmoviesapp-1/base.apk"],nativeLibraryDirectories=[/data/app/hu.bendaf.udacity.popularmovies.popularmoviesapp-1/lib/arm64, /system/lib64, /vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.ActivityThread.installProvider(ActivityThread.java:5841)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:5445) 
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5384) 
    at android.app.ActivityThread.-wrap2(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

MovieProvider class 应该是用原理图库生成的(在第一次部署时,但由于某种原因后它消失了)。

如果我清理项目,我可以部署并 运行 我的应用程序再次正确,但每次修改后清理项目非常烦人。 :)

你能帮我看看是什么导致了这个问题吗?

这是我的 build.gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "hu.bendaf.udacity.popularmovies.popularmoviesapp"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 2
        versionName "1.1"
        vectorDrawables.useSupportLibrary = true
        jackOptions {
            enabled true
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support:design:25.1.1'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
    compile 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    compile 'com.android.support:recyclerview-v7:25.1.1'
    compile 'com.android.support:cardview-v7:25.1.1'
    annotationProcessor 'net.simonvt.schematic:schematic-compiler:0.7.0'
    compile 'net.simonvt.schematic:schematic:0.7.0'
}

你可以在 github

上找到我的整个项目

这可能是因为 ProGuard 进行了缩小。您必须手动指定以忽略您的外部库以防止缩小。

您可以像这样在 proguard-rules.pro 文件中指定它..

-keep class com.package.** { *; }

阅读更多here..

我已经解决了这个问题,因为我使用了jack和java 1.8。在我从我的 gradle 中删除这些行(以及我的应用程序中的 lambda 函数)后,崩溃消失了:

jackOptions {
    enabled true
}
...
compileOptions {
    targetCompatibility 1.8
    sourceCompatibility 1.8
}

感谢您的帮助!