首页 > 解决方案 > app-release 版本与 app-debug 版本有何不同?

问题描述

我的应用程序在发布模式下没有按预期工作,但在调试模式下可以无缝工作。例如,网络操作在发布模式下抛出异常。我在清单文件中添加了所有互联网权限。请参阅下面的代码提前致谢!

最初我认为这是 Internet Permissions 的问题。但我已经在清单文件中声明了互联网权限。

@Override
    protected ArrayList<NewsObject> doInBackground(Void... voids) {
        String url = "http://rvrjc.ac.in/";
        ArrayList<NewsObject> mList = new ArrayList<NewsObject>();
        Document document;
        try {
            Log.d(MainActivity.class.getSimpleName(), "Main Activity");

            document = Jsoup.connect(url).timeout(15000).get();

            Elements lnews = document.select("ul.newsticker li");
            Elements bnews = document.select("div#galleryimage p");
            //Get the title of the website
            for (Element news : lnews) {//latest news Section
                String s = "New";
                String descNews = news.select("p").text().trim();
                String newsUrl = news.select("a").attr("href");
                if (newsUrl.startsWith("http")) {
                    //do nothing
                    if (!(TextUtils.isEmpty(descNews)))
                        mList.add(new NewsObject(descNews, newsUrl, s));
                } else {
                    if (!(TextUtils.isEmpty(descNews)))
                        mList.add(new NewsObject(descNews, url, s));
                }
            }
            for (Element news : bnews) {

                String descNews = news.select("p").text();
                String newsGif = news.select("p img").attr("src");
                String newsStatus = "Old";
                if (newsGif.equals("new.gif")) {

                    newsStatus = "New";
                    //System.out.println("News Status changed");
                }

                String newsUrl = news.select("a").attr("href");
                if (newsUrl.startsWith("http")) {
                    if (!(TextUtils.isEmpty(descNews))) {
                        if(newsUrl.startsWith("http://rvrjcce.ac.in/examcell/results")&& newsStatus.equals("New"))
                            mList.add(0,new NewsObject(descNews, newsUrl, newsStatus));
                        else
                            mList.add(new NewsObject(descNews, newsUrl, newsStatus));
                    }
                }
                else if (!(TextUtils.isEmpty(descNews)))
                    mList.add(new NewsObject(descNews, url, newsStatus));
            }

        }
         catch (final java.net.SocketTimeoutException e) {
            e.printStackTrace();
           // showDialog();
             final View parentLayout = getActivity().findViewById(android.R.id.content);

             Snackbar nSnackbar = Snackbar.make(parentLayout, "Server Timeout", Snackbar.LENGTH_INDEFINITE)
                     .setAction("RETRY", new View.OnClickListener() {
                         @Override
                         public void onClick(View view) {
                             new TaskLoader().execute();


                         }
                     });

             nSnackbar.setActionTextColor(getResources().getColor(R.color.colorWhite));
             nSnackbar.getView().setBackgroundColor(ContextCompat.getColor(MainActivity.contextMain,R.color.colorPrimary));
             nSnackbar.show();
             Log.d("STEx","Socket Exception OCCURED");
        } catch (IOException e){
            e.printStackTrace();
            Log.e("Exception",e+"");
            Log.d("IOEx","IOEXCEPTION OCCURED");
            final View parentLayout = getActivity().findViewById(android.R.id.content);
            Snackbar nSnackbar = Snackbar.make(parentLayout, "Internet Connection is Slow!! ", Snackbar.LENGTH_INDEFINITE)
                    .setAction("RETRY", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            new TaskLoader().execute();


                        }
                    });

            nSnackbar.setActionTextColor(getResources().getColor(R.color.colorWhite));
            nSnackbar.getView().setBackgroundColor(ContextCompat.getColor(MainActivity.contextMain,R.color.colorPrimary));
            nSnackbar.show();
        }
        catch (Exception e)
        {
            e.printStackTrace();

            final View parentLayout = getActivity().findViewById(android.R.id.content);
            Snackbar nSnackbar = Snackbar.make(parentLayout, " Unable to get Data ! ", Snackbar.LENGTH_INDEFINITE)
                    .setAction("RETRY", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            new TaskLoader().execute();


                        }
                    });

            nSnackbar.setActionTextColor(getResources().getColor(R.color.colorWhite));
            nSnackbar.getView().setBackgroundColor(ContextCompat.getColor(MainActivity.contextMain,R.color.colorPrimary));
            nSnackbar.show();
        }

        catch (Error e)
        {
            e.printStackTrace();

            final View parentLayout = getActivity().findViewById(android.R.id.content);
            Snackbar nSnackbar = Snackbar.make(parentLayout, " Unable to get Data ! ", Snackbar.LENGTH_INDEFINITE)
                    .setAction("RETRY", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            new TaskLoader().execute();


                        }
                    });

            nSnackbar.setActionTextColor(getResources().getColor(R.color.colorWhite));
            nSnackbar.getView().setBackgroundColor(ContextCompat.getColor(MainActivity.contextMain,R.color.colorPrimary));
            nSnackbar.show();
        }
        Log.e("TAG", "" + mList);
        return mList;
    }

这是清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="********************"
    android:versionCode="1"
    android:versionName="Release">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/icon_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/icon_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">
        <activity android:name=".HomeActivity"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme">

        </activity>
        <activity
            android:name=".EditProfile"
            android:theme="@style/AppTheme"></activity>
        <activity
            android:name=".ShowProfileActivity"
            android:theme="@style/AppTheme"></activity>
        <activity
            android:name=".SettingsActivity"
            android:theme="@style/AppTheme"></activity>
        <service
            android:name=".MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/rvr" />


        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id"/>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />

    </application>


</manifest>

Blockquote 这是我的 build.gradle 应用文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "****************"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 3
        versionName "1.5.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        resConfigs "en"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.multidex:multidex:2.0.0'
    implementation 'com.google.firebase:firebase-core:16.0.9'
    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.google.firebase:firebase-database:17.0.0'
    implementation 'com.google.firebase:firebase-firestore:19.0.0'
    implementation 'com.google.firebase:firebase-messaging:19.0.1'
    implementation 'com.firebaseui:firebase-ui-auth:5.0.0'
    implementation 'com.firebaseui:firebase-ui-database:5.0.0'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'org.jsoup:jsoup:1.10.3'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

}
apply plugin: 'com.google.gms.google-services'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true

Proguard-optimise.txt

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# Starting with version 2.2 of the Android plugin for Gradle, this file is distributed together with
# the plugin and unpacked at build-time. The files in $ANDROID_HOME are no longer maintained and
# will be ignored by new version of the Android plugin for Gradle.

# Optimizations: If you don't want to optimize, use the proguard-android.txt configuration file
# instead of this one, which turns off the optimization flags.
# Adding optimization introduces certain risks, since for example not all optimizations performed by
# ProGuard works on all versions of Dalvik.  The following flags turn off various optimizations
# known to have issues, but the list may not be complete or up to date. (The "arithmetic"
# optimization can be used if you are only targeting Android 2.0 or later.)  Make sure you test
# thoroughly if you go this route.
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizationpasses 5
-allowaccessmodification

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Preserve some attributes that may be required for reflection.
-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod

-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
-keep public class com.google.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService
-dontnote com.google.vending.licensing.ILicensingService
-dontnote com.google.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# Keep setters in Views so that animations can still work.
-keepclassmembers public class * extends android.view.View {
    void set*(***);
    *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick.
-keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# Preserve annotated Javascript interface methods.
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

# The support libraries contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version. We know about them, and they are safe.
-dontnote android.support.**
-dontnote androidx.**
-dontwarn android.support.**
-dontwarn androidx.**

# This class is deprecated, but remains for backward compatibility.
-dontwarn android.util.FloatMath

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep
-keep class androidx.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}
-keep @androidx.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @androidx.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @androidx.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

-keepclasseswithmembers class * {
    @androidx.annotation.Keep <init>(...);
}

# These classes are duplicated between android.jar and org.apache.http.legacy.jar.
-dontnote org.apache.http.**
-dontnote android.net.http.**

# These classes are duplicated between android.jar and core-lambda-stubs.jar.
-dontnote java.lang.invoke.**

标签: androidandroid-studio

解决方案


要检查一切是否按预期工作,您可以通过在应用程序级 build.gradle 中更改此行来禁用 proguard

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

对此

    buildTypes {
        release {
            minifyEnabled false
            shrinkResources false
        }
    }

注意: 您应该始终混淆您的代码,以使逆向工程几乎不可能并减小您的应用程序 apk 大小。


推荐阅读