通过在Android中实现Proguard,使用JDBC在Asynctask中崩溃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过在Android中实现Proguard,使用JDBC在Asynctask中崩溃相关的知识,希望对你有一定的参考价值。

我的应用程序之前工作正常,但在我的项目中启用ProGuard后,应用程序立即在“release”和“debug”apk中崩溃。我也禁用proguard并再次尝试它,它的工作原理。

现在我的问题是如何在我的项目中使用jdbc以及proguard,这可能吗?

这是我的调试

    E/androidRuntime: FATAL EXCEPTION: AsyncTask #1
              Process: com.example.aileen.rotaractclub, PID: 6131
              java.lang.RuntimeException: An error occurred while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:309)
                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                  at java.lang.Thread.run(Thread.java:818)
               Caused by: java.lang.ExceptionInInitializerError
                  at com.b.a.bq.connect(Unknown Source)
                  at java.sql.DriverManager.getConnection(DriverManager.java:179)
                  at java.sql.DriverManager.getConnection(DriverManager.java:213)
                  at com.example.aileen.rotaractclub.b.a.a(Unknown Source)
                  at com.example.aileen.rotaractclub.b.a$c.a(Unknown Source)
                  at com.example.aileen.rotaractclub.b.a$c.doInBackground(Unknown Source)
                  at android.os.AsyncTask$2.call(AsyncTask.java:295)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                  at java.lang.Thread.run(Thread.java:818) 
               Caused by: java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class java.lang.String, int, class java.util.Properties, class java.lang.String, class java.lang.String]
                  at com.b.a.o.<clinit>(Unknown Source)
                  at com.b.a.bq.connect(Unknown Source) 
                  at java.sql.DriverManager.getConnection(DriverManager.java:179) 
                  at java.sql.DriverManager.getConnection(DriverManager.java:213) 
                  at com.example.aileen.rotaractclub.b.a.a(Unknown Source) 
                  at com.example.aileen.rotaractclub.b.a$c.a(Unknown Source) 
                  at com.example.aileen.rotaractclub.b.a$c.doInBackground(Unknown Source) 
                  at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                  at java.lang.Thread.run(Thread.java:818) 
               Caused by: java.lang.NoSuchMethodException: <init> [class java.lang.String, int, class java.util.Properties, class java.lang.String, class java.lang.String]
                  at java.lang.Class.getConstructor(Class.java:528)
                  at java.lang.Class.getConstructor(Class.java:492)
                  at com.b.a.o.<clinit>(Unknown Source) 
                  at com.b.a.bq.connect(Unknown Source) 
                  at java.sql.DriverManager.getConnection(DriverManager.java:179) 
                  at java.sql.DriverManager.getConnection(DriverManager.java:213) 
                  at com.example.aileen.rotaractclub.b.a.a(Unknown Source) 
                  at com.example.aileen.rotaractclub.b.a$c.a(Unknown Source) 
                  at com.example.aileen.rotaractclub.b.a$c.doInBackground(Unknown Source) 
                  at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                  at java.lang.Thread.run(Thread.java:818) 

这是我的AsyncTask

     //Deleting Announcement
public class deleting extends AsyncTask<String,String,String> {
    Boolean Connection = false;
    Boolean isSuccess = false;
    ProgressDialog progress;
    String title;
    String ids;

    public deleting(String aydi,String titulo)
    {

        ids=aydi;
        title =titulo;
       }


    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progress = new ProgressDialog(getActivity());
        progress.setTitle("Synchronizing");
        progress.setMessage("Removing '"+ title +"'
 announcement please wait...");
        progress.setIndeterminate(false);
        progress.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
        progress.show();
        progress.setCancelable(false);
    }


    @Override
    protected String doInBackground(String... params) {
        try {





            try {
                connection = connectionclass(secured_Pref.getString("server",""), secured_Pref.getString("user",""), secured_Pref.getString("pass",""));
                if (connection == null) {
                    Connection = false;
                }
                else
                {
                    Connection = true;

                    PreparedStatement statement = connection.prepareStatement("DELETE FROM Announcements WHERE ID='"+this.ids.toString()+"' AND Title='"+this.title.toString()+"'");
                    long i =statement.executeUpdate();
                    try {
                        if (i>0)
                        {
                            isSuccess = true;
                        } else {
                            isSuccess = false;
                        }
                        connection.close();
                    } catch (Exception x) {
                        Toast.makeText(getActivity(), "Error Occur.", Toast.LENGTH_SHORT).show();
                    }
                }

            } catch (Exception e)
            {
                e.printStackTrace();
                Toast.makeText(getActivity(), "Error Occur.", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception ex) {
            isSuccess = false;
        }
        return null;

    }


    @Override
    protected void onPostExecute(String r) {
        super.onPostExecute(r);
        if (Connection)
        {
            Toast.makeText(getActivity(),"Successfully Connected.",Toast.LENGTH_LONG).show();
        }
        else if(Connection==false)
        {
            Toast.makeText(getActivity(),"No Internet Connection.",Toast.LENGTH_LONG).show();
        }
        if (isSuccess)
        {
            Snackbar.make(getView(), "Announcement is Successfully Removed.", Snackbar.LENGTH_LONG).show();

            //Refresh my fragment
            FragmentTransaction refresh = getFragmentManager().beginTransaction();
            refresh.detach(ViewAnnounce.this).attach(ViewAnnounce.this).commit();
        }
        else
        {
            Snackbar.make(getView(), "Oops! Error Occur.", Snackbar.LENGTH_LONG).show();
        }
        progress.dismiss();

    }
}

ProGuard规则

    -ignorewarnings
# Basic ProGuard rules for Firebase Android SDK 2.0.0+
-keep class com.firebase.** { *; }
-keep class org.apache.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.apache.**
-dontwarn org.w3c.dom.**
-renamesourcefileattribute SourceFile
-keep public class * extends android.app.Application
-optimizationpasses 5
-keepattributes *Annotation*,EnclosingMethod

在我的Gradle中

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

已经通过将此规则包含在我的程序中来修复

    -dontshrink

以上是关于通过在Android中实现Proguard,使用JDBC在Asynctask中崩溃的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 上使用 OrmLite 进行 Proguard

使用 Android Studio 签署 APK 时出现 Proguard 错误

在 Android 上使用 R8 时,是不是需要卸载现有的 Proguard?

使用 ProGuard 导出 Android 应用程序会出现 ParseException 错误

签署 Android 应用程序时出现错误 Proguard

使用 android.support.v4.app 蜂窝兼容包导出签名应用时出现 Proguard 错误