反射不在android发布apk上。即使禁用了proguard / minify

时间:2017-01-07 19:59:38

标签: java android reflection proguard

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

我想要获得的字段是android.widget打包的。 我也努力启用proguard并在proguard.pro文件中设置配置

无论如何都是例外。

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

Gradle文件

    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 个答案:

答案 0 :(得分:2)

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

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