反射不适用于android版本apk.即使禁用了proguard/minify, [英] Reflection not working on android release apk. Even with proguard/minify disabled
问题描述
当前,我面临一个奇怪的问题,我的应用的发布APK抛出NoSuchFieldExceptions.在调试apk上工作正常.
Currently I'm facing an odd issue that the release apk of my app throws NoSuchFieldExceptions. It works fine on a debug apk.
我尝试获取的字段已打包为android.widget. 我也已经努力启用proguard并在proguard.pro文件中设置配置
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
这是个例外.
java.lang.IllegalAccessException:类java.lang.Class无法访问类java.lang.Class的字段android.widget.ProgressBar com.trinitcore.localtrade.LoginActivity.o
java.lang.IllegalAccessException: Class java.lang.Class cannot access field android.widget.ProgressBar com.trinitcore.localtrade.LoginActivity.o of class java.lang.Class
成绩文件
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意味着它可以看到变量/方法,但是由于它是私有的(或者因为它是受保护的,并且不是相同的包装).这不是反射不起作用的情况,它是一个常见的错误,没有反射就无法编译.
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.
这篇关于反射不适用于android版本apk.即使禁用了proguard/minify,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!