签名的 apk 中的 ClassNotFoundException [英] ClassNotFoundException in signed apk

查看:29
本文介绍了签名的 apk 中的 ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在我的 android 设备上安装并运行一个签名的 APK,我就会收到这个错误.如果我只是编译一个应用程序并直接在设备上运行,则不会出现此错误.

似乎缺少的片段在我的项目代码中,而不是在任何外部库中.

我该如何调查该错误?我尝试重新构建、清理项目等.如果在 APK 中创建了缺失的类,我能以某种方式在 APK 中找到吗?

hier 是错误信息:

E/AndroidRuntime:致命异常:mainjava.lang.RuntimeException:无法启动活动组件信息{qua.com.bix/qua.com.bix.main.MainActivity}:android.support.v4.app.q:无法实例化片段qua.com.bix.main.MainLoadingFragment:确保类名存在,是公开的,并且在 Android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295) 有一个公开的空构造函数........引起:android.support.v4.app.q:无法实例化片段 qua.com.bix.main.MainLoadingFragment:确保类名存在,是公共的,并且有一个公共的空构造函数........引起:java.lang.ClassNotFoundException:在路径上没有找到类qua.com.bix.main.MainLoadingFragment":/data/app/qua.com.bix-1.apk在 dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)在 java.lang.ClassLoader.loadClass(ClassLoader.java:501)在 java.lang.ClassLoader.loadClass(ClassLoader.java:461)在 android.support.v4.app.o.a(来源不明)在 android.support.v4.app.o.a(来源不明)

这是我的build.gradle

应用插件:'com.android.application'安卓 {compileSdkVersion 23构建工具版本23.0.2"默认配置{applicationId "qua.com.bix"minSdk 版本 15目标SDK版本23版本代码 1版本名称1.0"multiDexEnabled true}构建类型{释放 {minifyEnabled true收缩资源真proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}包装选项{排除元信息/许可证"排除元信息/依赖项"排除元信息/通知"}dexOptions{增量真javaMaxHeapSize "4g"}useLibrary 'org.apache.http.legacy'}依赖{编译'com.android.support:multidex:1.0.1'编译文件树(目录:'libs',包括:['*.jar'])testCompile 'junit:junit:4.12'编译'com.android.support:appcompat-v7:23.1.1'编译'com.android.support:design:23.1.1'编译'com.quadriq.qlib:qlib@aar'编译'com.google.code.gson:gson:2.4'编译 'com.google.guava:guava:18.0'编译'com.google.code.findbugs:jsr305:2.0.2'编译 'com.google.android.gms:play-services:8.3.0'编译'com.nostra13.universalimageloader:universal-image-loader:1.9.5'编译'com.koushikdutta.ion:ion:2.+'编译'乔达时间:乔达时间:2.9.1'编译'com.github.michaelye.easydialog:easydialog:1.1'}

更新:现在有帮助的是禁用 proguard,还将 -dontobfuscate 添加到 proguard-rules.pro.但是,以这种方式执行此操作是一个好习惯吗?

proguard-rules.pro 现在是:

-dontobfuscate-不要警告 org.apache.lang.**-不要警告 com.google.common.**-不要警告 org.joda.**

更新 2:petey 回答了我的问题.

解决方案

Proguard 正在混淆您的重要应用程序类.

将以下行添加到您的 proguard 配置文件中,以便 proguard 不会与扩展 ActivityApplicationBroadcastReceiver 的应用程序类混淆, ContentProvider, Preference, &Fragment(支持和正常).

# 基本 Android 排除项,需要各种组件的正常功能- 保持公共类 * 扩展 android.app.Activity- 保持公共类 * 扩展 android.app.Application- 保持公共类 * 扩展 android.app.Service- 保持公共类 * 扩展 android.content.BroadcastReceiver- 保持公共类 * 扩展 android.content.ContentProvider- 保持公共类 * 扩展 android.preference.Preference- 保持公共类 * 扩展 android.support.v4.app.Fragment- 保持公共类 * 扩展 android.app.Fragment

(如果您不使用其中的一些,请随意删除这些行)

然后删除这一行

-dontobfuscate

<块引用>

现在有帮助的是禁用 proguard,同时添加 -dontobfuscate到 proguard-rules.pro.但是这样做是不是一个好习惯方式?

如果您不关心是否有人反编译您的应用程序,这才是最佳实践.所以不行.主要是因为您希望在您的生产/发布版本中尽可能地完成这样的任务.特别是如果您有应用内购买 (IAP) 或处理敏感数据.

I got this error if I install and run a signed APK at my android device. This error does not come up if I just compile an app and run directly at device.

The Fragment, that seems to be missing is in my code in the project, not in any external library.

How could I investigate that error? i tried to rebuld, clean project etc.. Could I somehow find in APK if missing classes are created there?

hier is the error message:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity

ComponentInfo{qua.com.bix/qua.com.bix.main.MainActivity}:
android.support.v4.app.q: Unable to instantiate fragment
qua.com.bix.main.MainLoadingFragment:
make sure class name exists, is public, and has an empty constructor that is public at Android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
    ....
    ....
    Caused by: android.support.v4.app.q: Unable to instantiate fragment qua.com.bix.main.MainLoadingFragment: make sure class name exists, is public, and has an empty constructor that is public
    ....
    ....
    Caused by: java.lang.ClassNotFoundException: Didn't find class "qua.com.bix.main.MainLoadingFragment" on path: /data/app/qua.com.bix-1.apk
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at android.support.v4.app.o.a(Unknown Source) 
    at android.support.v4.app.o.a(Unknown Source) 

here is my build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "qua.com.bix"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
    }
    dexOptions{
        incremental true
        javaMaxHeapSize "4g"
    }
    useLibrary 'org.apache.http.legacy'
}

dependencies {
    compile 'com.android.support:multidex:1.0.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.quadriq.qlib:qlib@aar'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.google.guava:guava:18.0'
    compile 'com.google.code.findbugs:jsr305:2.0.2'
    compile 'com.google.android.gms:play-services:8.3.0'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    compile 'com.koushikdutta.ion:ion:2.+'
    compile 'joda-time:joda-time:2.9.1'
    compile 'com.github.michaelye.easydialog:easydialog:1.1'
}

Update: What helped for now, is disabling proguard, also adding -dontobfuscate to proguard-rules.pro. But is it a good practice to do this in that way?

the proguard-rules.pro is now:

-dontobfuscate
-dontwarn org.apache.lang.**
-dontwarn com.google.common.**
-dontwarn org.joda.**

Update 2: petey answered my question.

解决方案

Proguard is obfuscating your vital app classes.

add the following lines to your proguard config file so that proguard will not mess with your application's classes that extend Activity, Application, BroadcastReceiver, ContentProvider, Preference, & Fragment (support and normal).

# Base Android exclusions, required for proper function of various components
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment

(if you arent using some of these feel free to remove those lines)

and then remove this line

-dontobfuscate

What helped for now, is disabling proguard, also adding -dontobfuscate to proguard-rules.pro. But is it a good practice to do this in that way?

It is only best practice if you do not care if someone decompiles your application. So, no. Mostly because you want to make a task like that as hard as possible on your production/release builds. Especially if you have In App Purchasing(IAP) or handle sensitive data.

这篇关于签名的 apk 中的 ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆