Reflection not working on android release apk. Eve

2019-07-29 05:53发布

问题:

Currently I'm facing an odd issue that the release apk of my app throws NoSuchFieldExceptions. It works fine on a debug apk.

The fields I am trying to get are android.widget packaged. I have also made efforts with enabling proguard too and setting configurations in the proguard.pro file

Here's the exception regardless.

java.lang.IllegalAccessException: Class java.lang.Class cannot access field android.widget.ProgressBar com.trinitcore.localtrade.LoginActivity.o of class java.lang.Class

Gradle file

    apply plugin: 'com.android.application'

android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.trinitcore.localtrade"
        minSdkVersion  15 //15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.7
        sourceCompatibility 1.7
    }
}

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.mikepenz:materialdrawer:5.4.0@aar') {
        transitive = true
    }
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:design:+'
    compile 'com.quinny898.library.persistentsearch:library:1.1.0-SNAPSHOT'
    compile 'com.github.paolorotolo:appintro:4.0.0'
    compile 'com.mikepenz:materialize:0.9.0@aar'
    compile 'com.mikepenz:iconics-core:2.7.2@aar'
    compile 'com.mikepenz:fastadapter:1.6.1@aar'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.google.android.gms:play-services:9.6.0'
    compile 'com.android.support:support-v4:+'
    compile 'com.android.support:cardview-v7:+'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile "com.mikepenz:iconics-core:2.8.1@aar"
    compile 'com.mikepenz:google-material-typeface:2.2.0.3.original@aar'
    compile 'com.mikepenz:material-design-iconic-typeface:2.2.0.2@aar'
    compile 'com.mikepenz:fontawesome-typeface:4.7.0.0@aar'
    compile 'com.mikepenz:octicons-typeface:3.2.0.2@aar'
    compile 'com.mikepenz:meteocons-typeface:1.1.0.2@aar'
    compile 'com.mikepenz:community-material-typeface:1.7.22.1@aar'
    compile 'com.mikepenz:weather-icons-typeface:2.0.10.2@aar'
    compile 'com.mikepenz:typeicons-typeface:2.0.7.2@aar'
    compile 'com.mikepenz:entypo-typeface:1.0.0.2@aar'
    compile 'com.mikepenz:devicon-typeface:2.0.0.2@aar'
    compile 'com.mikepenz:foundation-icons-typeface:3.0.0.2@aar'
    compile 'com.mikepenz:ionicons-typeface:2.0.1.2@aar'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.lusfold.spinnerloading:library:1.0.0'
    compile 'com.github.medyo:fancybuttons:1.8.3'
    compile 'jp.wasabeef:blurry:2.1.0'
    compile ('org.apache.httpcomponents:httpmime:4.3'){
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    compile ('org.apache.httpcomponents:httpcore:4.4.1'){
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'

    }
    testCompile 'junit:junit:4.12'
}

回答1:

An IllegalAccessException means it can see the variable/method, but that it can't access it because its private (or because its protected and this isn't the same package). This isn't a case of reflection not working, its a common bug that without reflection wouldn't compile.

You can change the visibility of the variable/method at runtime using reflection and then access it, but its not recommended. Its better to add a public method that does what you need, to prevent the possibility of changing data in ways the class wasn't written to accept.