无法为AndroidViewModel的子类创建类的实例

时间:2018-09-20 08:48:10

标签: java android android-studio android-proguard android-r8

使用我的应用程序发行包时出现以下错误:

  

E / Android运行时:致命异常:主要       进程:my.package.free,PID:14179       java.lang.RuntimeException:无法创建类c.a.f.E的实例           在b.p.y $ a.a(:9)           在c.a.f.C.onCreate(:14)           在androidx.fragment.app.Fragment.performCreate(:5)           在b.m.a.s.a(:243)           在b.m.a.D.a(:260)           在b.m.a.D.a(:11)           在b.m.a.s.a(:510)           在b.m.a.s.b(:31)           在b.m.a.s.g(:8)           在b.m.a.s.d(:11)           在b.m.a.s.m(:3)           在androidx.fragment.app.FragmentActivity.onStart(:6)           在androidx.appcompat.app.AppCompatActivity.onStart(:1)           在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)           在android.app.Activity.performStart(Activity.java:7157)           在android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937)           在android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)           在android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)           在android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)        造成原因:java.lang.NoSuchMethodException:[class android.app.Application]           在java.lang.Class.getConstructor0(Class.java:2327)           在java.lang.Class.getConstructor(Class.java:1725)           在b.p.y $ a.a(:5)           在c.a.f.C.onCreate(:14)           在androidx.fragment.app.Fragment.performCreate(:5)           在b.m.a.s.a(:243)           在b.m.a.D.a(:260)           在b.m.a.D.a(:11)           在b.m.a.s.a(:510)           在b.m.a.s.b(:31)           在b.m.a.s.g(:8)           在b.m.a.s.d(:11)           在b.m.a.s.m(:3)           在androidx.fragment.app.FragmentActivity.onStart(:6)           在androidx.appcompat.app.AppCompatActivity.onStart(:1)           在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)           在android.app.Activity.performStart(Activity.java:7157)           在android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937)           在android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)           在android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)           在android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

如果我查看映射文件,则c.a.f.E被映射到my.package.fragments.ProfileViewModel。这是该类的定义:

public class ProfileViewModel extends AndroidViewModel {

    private final ContactSpanRepository contactSpanRepository;

    private final ProfileRepository profileRepository;

    public ProfileViewModel(Application application) {
        super(application);
        Context context = getApplication().getApplicationContext();
        this.contactSpanRepository = ContactSpanRepository.getInstance(context);
        this.profileRepository = ProfileRepository.getInstance(context);
    }

    public LiveData<List<ContactSpan>> getContactSpans(Long profileId) {
        return contactSpanRepository.getContactSpan(profileId);
    }

    public LiveData<List<Profile>> getProfiles() {
        return profileRepository.getProfiles();
    }

}

当我从Android Studio(3.3 Canary 11)运行该应用程序时,不会发生此问题。该问题仅发生在已发布的软件包中(当我安装gradle assembleRelease生成的APK时)。我认为问题与创建发行包时的优化有关。

我尝试禁用R8(android.enableR8=false),但仍然遇到相同的问题。强制将类定义保留在Proguard配置文件中也无济于事。

使用以下Gradle属性:

  

android.enableJetifier = true   android.useAndroidX = true

以下是所使用的androidx依赖项:

annotationProcessor 'androidx.annotation:annotation:1.0.0-rc02'

implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01'
implementation "android.arch.work:work-runtime:1.0.0-alpha08"

implementation 'androidx.appcompat:appcompat:1.0.0-rc02'
implementation 'androidx.browser:browser:1.0.0-rc02'
implementation 'androidx.cardview:cardview:1.0.0-rc02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.exifinterface:exifinterface:1.0.0-rc02'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-rc02'
implementation 'androidx.recyclerview:recyclerview:1.0.0-rc02'

0 个答案:

没有答案