Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

android - Obfuscation (minifyEnabled true) not working in Debug and Release

I cannot able to run apk using minifyEnabled true in Debug and Release mode.I have referred below links but none worked for me.

1.Android- Error:Execution failed for task ':app:transformClassesWithDexForRelease'

2.Error converting bytecode to dex: Cause: java.lang.RuntimeException: Exception parsing classes - Android studio 2.0 beta 6

Below is application build.gradle file

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId 'XX.XX.XXX'
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 9
        versionName "1.0"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true



    }



    sourceSets.main {
        jni.srcDirs = [] //disable automatic ndk-build call
        jniLibs.srcDir 'src/main/libs' //integrate your libs from libs instead of jniLibs
    }
    dexOptions {
        javaMaxHeapSize "4g" //specify the heap size for the dex process
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

        }
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }


    }
    productFlavors {
    }
    lintOptions {
        checkReleaseBuilds false
    }
    packagingOptions {
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'

    }
}



dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    //    compile files('libs/maflogonimo-1.210.3.jar')
    compile project(':android-logging-log4j-1.0.3')
    compile project(':log4j-1.2.17')
    compile files('libs/gson-2.6.2.jar')
    //compile files('libs/javarosa-libraries-2015-09-02.jar')
    compile 'com.android.support:design:23.4.0'
    //compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.google.android.gms:play-services-maps:10.0.1'
    compile 'com.google.android.gms:play-services-location:10.0.1'
    compile 'com.google.android.gms:play-services-auth:10.0.1'
    //    compile 'com.google.android.gms:play-services:10.0.1'

    //    compile 'com.google.android.gms:play-services-fcm:9.6.1'
    compile 'com.google.firebase:firebase-messaging:10.0.1'
    //    compile 'com.viewpagerindicator:library:2.4.1@aar'
    //compile files('libs/hellocharts-library-1.5.8.jar')
    compile 'com.esri.arcgis.android:arcgis-android:10.2.8-1'
    //GIS

    compile(name:'ClientHubSLL-3.16.1', ext:'aar')
    compile(name:'ClientLog-3.16.1', ext:'aar')
    compile(name:'Common-3.16.1', ext:'aar')
    compile(name:'Connectivity-3.16.1', ext:'aar')
    compile(name:'CoreServices-3.16.1', ext:'aar')
    compile(name:'DataVaultLib-3.16.1', ext:'aar')
    compile(name:'E2ETrace-3.16.1', ext:'aar')
    compile(name:'HttpConvAuthFlows-3.16.1', ext:'aar')
    compile(name:'HttpConversation-3.16.1', ext:'aar')
    compile(name:'MAFLogger-3.16.1', ext:'aar')
    compile(name:'MAFLogonCore-3.16.1', ext:'aar')
    compile(name:'ODataAPI-3.16.1', ext:'aar')
    compile(name:'ODataOnline-3.16.1', ext:'aar')
    compile(name:'Request-3.16.1', ext:'aar')
    compile(name:'SupportabilityFacade-3.16.1', ext:'aar')

    compile(name:'MAFCalendar-3.16.1', ext:'aar')
    compile(name:'MAFLocaleAwareControls-3.16.1', ext:'aar')
    compile(name:'MAFLogViewer-3.16.1', ext:'aar')
    compile(name:'MAFSettingScreen-3.16.1', ext:'aar')
    compile(name:'MAFTreeView-3.16.1', ext:'aar')
    compile(name:'MAFUIComponents-3.16.1', ext:'aar')
    compile(name:'XscriptParser-3.16.1', ext:'aar')
    compile(name:'MAFLogonUI-3.16.1', ext:'aar')
    compile(name:'MobilePlace-3.16.1', ext:'aar')

}

apply plugin: 'com.google.gms.google-services'

Below is proguard-rules.pro file

-renamesourcefileattribute SourceFile
-keepattributes SourceFile, LineNumberTable
-keepattributes Exceptions, Signature

-keepattributes EnclosingMethod
-keepattributes InnerClasses

-keep class org.javarosa.** { *; }
-keep class com.sap.** { *; }

-keep class jcifs.** { *; }


-dontwarn com.google.**
-dontwarn org.apache.**
-dontwarn com.sap.**
-dontwarn au.com.bytecode.**
-dontwarn org.joda.**
-dontwarn android.content.**
-dontwarn android.graphics.**
-dontwarn android.util.**
-dontwarn android.view.**

-dontwarn javax.servlet.**
-dontwarn jcifs.http.**
-dontwarn org.codehaus.**

Below exception while running from command line

cause: java.lang.RuntimeException: Exception parsing classes
    UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Exception parsing classes
        at com.android.dx.command.dexer.Main.processClass(Main.java:752)
        at com.android.dx.command.dexer.Main.processFileBytes(Main.java:718)
        at com.android.dx.command.dexer.Main.access$1200(Main.java:85)
        at com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1645)
        at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
        at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
        at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
        at com.android.dx.command.dexer.Main.processOne(Main.java:672)
        at com.android.dx.command.dexer.Main.processAllFiles(Main.java:574)
        at com.android.dx.command.dexer.Main.runMultiDex(Main.java:366)
        at com.android.dx.command.dexer.Main.run(Main.java:275)
        at com.android.dx.command.dexer.Main.main(Main.java:245)
        at com.android.dx.command.Main.main(Main.java:106)
Caused by: com.android.dx.cf.iface.ParseException: bad descriptor: charOffset
        at com.android.dx.cf.direct.AttributeListParser.parse(AttributeListParser.java:156)
        at com.android.dx.cf.direct.AttributeListParser.parseIfNecessary(AttributeListParser.java:115)
        at com.android.dx.cf.direct.AttributeListParser.getEndOffset(AttributeListParser.java:96)
        at com.android.dx.cf.direct.MemberListParser.parse(MemberListParser.java:213)
        at com.android.dx.cf.direct.MemberListParser.parseIfNecessary(MemberListParser.java:108)
        at com.android.dx.cf.direct.MethodListParser.getList(MethodListParser.java:54)
        at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:542)
        at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
        at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
        at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
        at com.android.dx.command.dexer.Main.parseClass(Main.java:764)
        at com.android.dx.command.dexer.Main.access$1500(Main.java:85)
        at com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684)
        at com.android.dx.command.dexer.Main.processClass(Main.java:749)
        ... 12 more
Caused by: java.lang.IllegalArgumentException: bad descriptor: charOffset
        at com.android.dx.rop.type.Type.intern(Type.java:330)
        at com.android.dx.cf.direct.AnnotationParser.parseAnnotation(AnnotationParser.java:248)
        at com.android.dx.cf.direct.AnnotationParser.parseAnnotations(AnnotationParser.java:223)
        at com.android.dx.cf.direct.AnnotationParser.parseAnnotationsList(AnnotationParser.java:188)
        at com.android.dx.cf.direct.AnnotationParser.parseParameterAttribute(AnnotationParser.java:127)
        at com.android.dx.cf.direct.StdAttributeFactory.runtimeVisibleParameterAnnotations(StdAttributeFactory.java:666)
        at com.android.dx.cf.direct.StdAttributeFactory.parse0(StdAttributeFactory.java:162)
        at com.android.dx.cf.direct.AttributeFactory.parse(AttributeFactory.java:96)
        at com.android.dx.cf.direct.AttributeListParser.parse(AttributeListParser.java:141)
        ... 25 more

16:35:57.798 [ERROR] [org.gradle.api.Project] 1 error; aborting
16:35:57.800 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':app:transformClassesWithDexForRelease'
16:35:57.801 [LIFECYCLE] [class org.gradle.TaskExecutionLogger] :app:transformClassesWithDexForRelease FAILED
16:35:57.804 [INFO] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] :app:transformClassesWithDexForRelease (Thread[Daemon worker,5,main]) completed. Took 1 mins 1.425 secs.
16:35:57.807 [DEBUG] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] Task worker [Thread[Daemon worker,5,main]] finished, busy: 14 mins 11.51 secs, idle: 0.078 secs
16:35:57.870 [ERROR] [org.gradle.BuildExceptionReporter]
16:35:57.871 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception.
16:35:57.872 [ERROR] [org.gradle.BuildExceptionReporter]
16:35:57.872 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
16:35:57.872 [ERROR] [org.gradle.BuildExceptionReporter] Execution failed for task ':app:transformClassesWithDexForRelease'.
16:35:57.873 [ERROR] [org.gradle.BuildExceptionReporter] > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:Program FilesJavajdk1.8.0_161injava.exe'' finished with non-zero exit value 1
16:35:57.874 [ERROR] [org.gradle.BuildExceptionReporter]
16:35:57.875 [ERROR] [org.gradle.BuildExceptionReporter] * Try:
16:35:57.877 [ERROR] [org.gradle.BuildExceptionReporter] Run with --stacktrace option to get the stack trace.
16:35:57.878 [LIFECYCLE] [org.gradle.BuildResultLogger]
16:35:57.878 [LIFECYCLE] [org.gradle.BuildResultLogger] BUILD FAILED
16:35:57.880 [LIFECYCLE] [org.gradle.BuildResultLogger]
16:35:57.880 [LIFECYCLE] [org.gradle.BuildResultLogger] Total time: 14 mins 46.244 secs
16:35:57.930 [DEBUG] [org.gradle.cache.internal.btree.BTreePersistentIndexedCache] Closing cache fileHashes.bin (D:myJobCard_SourceCode.gradle2.10askArtifactsfileHashes.bin)
16:35:57.931 [DEBUG] [org.gradle.cache.internal.btree.BTreePersistentIndexedCache] Closing cache fileSnapshots.bin (D:myJobCard_SourceCode.gradle2.10askArtifactsfileSnapshots.bin)
16:35:57.932 [DEBUG] [org.gradle.cache.internal.btree.BTreePersistentIndexedCache] Closing cache taskArtifacts.bin (D:myJobCard_SourceCode.gradle2.10askArtifactsaskArtifacts.bin)
16:35:57.933 [DEBUG] [org.gradle.cache.internal.btree.BTreePersistentIndexedCache] Closing cache outputFileStates.bin (D:myJobCard_SourceCode.gradle2.10askArtifactsoutputFileStates.bin)
16:35:57.933 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on task history cache (D:myJobCard_SourceCode.gradle2.10askArtifacts).
16:35:57.935 [DEBUG] [org.gradle.cache.internal.btree.BTreePersistentIndexedCache] Closing cache jarSnapshots.bin (C:UsersMY HOME.gradlecaches2.10jarSnapshotsjarSnapshots.bin)
16:35:57.935 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well... As you haven't mentioned yet, to the ant, to pro-guard your app, That's why it is not able to obfuscate it. Use explicitly useProguard true


Need a simple example of app gradle build file...? Find it below as it is of my project :

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.xxxxxxxxxxcccccccvvvvvv.apps.firebase"
    minSdkVersion 16
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        shrinkResources true
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Now what it means in wording : It will only obfuscate your code in release of your project; which means you have to build your release apk.

How to test...? obfuscation happened or not?

Here we go...

  1. Unzip apk file from linux or mac os terminal as: unzip apk_name
  2. You will get your project files extracted... find dex file
  3. Use dex2jar to convert it into jar
  4. Read that jar file with JD-UI app
  5. You will see all your java classes and core code files
  6. Yes.... look neat.... Its a code.... But with obfuscation

Hmmmm... Hope it helps

Edit : 2

In the pro-guard rules files... Do not try to program it like code, just keep some classes which are not so important as : -keep public class OpenSource

Thats it.

Edit : 3

Most importantly you should and must as a beginner use default pro-guard file set by android studio or whichever your IDE; while creating the project. like :

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

Edit : 4

It is a good habit to create a new sample project, learn how to implement any concept, once we are hands on it and cleared the concept..., Open the main project and implement it within..., As ... As of now , we are mastered the concept.

Edit : 5

It is always recommended to use Android Studio than any other IDE as it is officially by google as android is.. And it must be latest and updated IDE with minimum api 26 enabled. as google made compulsory last week that to upload apps on google play apps must target api level 26.

Also it helps to your project to get it build error free as all the latest dependencies are there along with patches.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...