Java 反射在android版本apk上不起作用。即使禁用了proguard/minify

Java 反射在android版本apk上不起作用。即使禁用了proguard/minify,java,android,reflection,proguard,Java,Android,Reflection,Proguard,目前,我面临一个奇怪的问题,我的应用程序的apk发行版抛出了NoSuchFieldExceptions。它在调试apk上运行良好 我试图获取的字段是android.widget打包的。 我还努力启用proguard,并在proguard.pro文件中设置配置 这是一个例外 java.lang.IllegalAccessException:Class java.lang.Class无法访问类java.lang.Class的字段android.widget.ProgressBar com.trini

目前,我面临一个奇怪的问题,我的应用程序的apk发行版抛出了NoSuchFieldExceptions。它在调试apk上运行良好

我试图获取的字段是android.widget打包的。 我还努力启用proguard,并在proguard.pro文件中设置配置

这是一个例外

java.lang.IllegalAccessException:Class java.lang.Class无法访问类java.lang.Class的字段android.widget.ProgressBar com.trinitcore.localtrade.LoginActivity.o

梯度锉

    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'
}

IllegalAccessException意味着它可以看到变量/方法,但是它不能访问它,因为它是私有的(或者因为它是受保护的,并且它不是同一个包)。这不是反射不起作用的情况,这是一个没有反射就无法编译的常见错误


您可以在运行时使用反射更改变量/方法的可见性,然后访问它,但不建议这样做。最好添加一个满足您需要的公共方法,以防止以类编写时不接受的方式更改数据。

您的答案居然有效。设置反射想要访问的公共变量修复了这个问题。但我仍然不明白为什么这只发生在应用程序的发布版本中,而不发生在调试版本中?@user256272你有没有弄清楚为什么这发生在发布版本而不是调试版本中?