运行签名的 apk 时出现 Dexguard 错误

Posted

技术标签:

【中文标题】运行签名的 apk 时出现 Dexguard 错误【英文标题】:Dexguard error while running signed apk 【发布时间】:2015-08-27 06:32:57 【问题描述】:

我正在使用 android Studio,我的项目有 5 个模块。

运行签名的混淆 apk 时出现以下错误 无法实例化应用程序 com.....FTApplication:java.lang.ClassNotFoundException:在路径上找不到类“com....FTApplication”:DexPathList [[zip 文件“/data/app/app.apk”] .

这是我的配置

DexGuard6.1.03 com.android.tools.build:gradle:1.0.0 - 依赖 应用程序从 MultiDexApplication 扩展而来。

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.project.package">
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:name="com.project.package.FTApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />



        <activity
            android:name=".ui.activity.FTSplashActivity"
            android:label="@string/app_name"
            android:noHistory="true"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest>

*** build.gradle

buildscript 
    repositories 
        jcenter()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:1.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    


allprojects 
    repositories 
        jcenter()
    

模块构建.grade

apply plugin: 'com.android.application'
apply plugin: 'dexguard'

repositories 
    maven  url 'https://maven.fabric.io/public' 


buildscript 
    repositories 
        mavenCentral()
        flatDir  dirs '../../DexGuard6.1.03/lib' 
    
    dependencies 
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath ':dexguard:'
    


android 
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig 
        applicationId "com.project.package"
        minSdkVersion 17
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        multiDexEnabled = true
    
    buildTypes 

        debug 
            proguardFile getDefaultDexGuardFile('dexguard-debug.pro')
            proguardFile 'dexguard-project.txt'
        
        release 
            proguardFile getDefaultDexGuardFile('dexguard-release.pro')
            proguardFile 'dexguard-project.txt'
        

//        release 
//            minifyEnabled false
//            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
//        
    


dependencies 
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':materialdialogs')
    compile project(':AndroidKit')
    compile 'com.joanzapata.pdfview:android-pdfview:1.0.4@aar'
    compile files('libs/disklrucache-2.0.1.jar')
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.jakewharton:disklrucache:2.0.2'
    compile files('libs/commons-httpclient-3.0.jar')
    compile files('libs/httpclientandroidlib-1.2.1.jar')

【问题讨论】:

发布您的manifest.xml 文件和gradle.build 反编译后发现我的应用类文件。 问题是因为 MultidexApplication。但我仍然无法解决问题。我必须使用 multidex 支持,因为我正在使用一些库并且方法索引超出范围异常 Raji,请尝试通过在 dexguard-project.txt 中添加 -multidex 来开启,而不是在 Gradle 中设置 multiDexEnabled = true。那是使用 Dexguard 的反射调用而不是 Android 的 mulitdex 支持。请参阅 Dexguard 的 doc/index.html。它谈到了 -multidex 选项。 【参考方案1】:

感谢@John 的评论。

这是 MultiDex 与 Dexguard 的解决方案:

1) 正确应用默认的 MultiDex 规则。有关正确选项,请参阅 my another post。

2)修改build.gradledexguard-project.txt文件。

build.gradle : 仅为调试版本设置 multiDexEnabled true。 Dexguard 将在发布构建时处理它。

defaultConfig 
    applicationId "com.sample"
    minSdkVersion 14
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"

    // NOTE 1 : Remove multiDexEnabled setting from here 


buildTypes 
    debug 
        // NOTE 2 : By default, debug builds don't need Dexguard. So set multiDexEnabled here.

        multiDexEnabled true
    
    release 
        // NOTE 3 : Multidex is handled at dexguard-project.txt. So remove multiDexEnabled setting if exists.

        proguardFiles getDefaultDexGuardFile('dexguard-release.pro'),'dexguard-project.txt'
    

dexguard-project.txt : 不要忘记在此处添加 multidex 选项!

-multidex 

【讨论】:

@shadygoneinsane 你最好联系 Dexguard 团队。 sdk 可能会有更新。

以上是关于运行签名的 apk 时出现 Dexguard 错误的主要内容,如果未能解决你的问题,请参考以下文章