通过在 Android 中实现 Proguard 在使用 JDBC 的 Asynctask 中应用程序崩溃

Posted

技术标签:

【中文标题】通过在 Android 中实现 Proguard 在使用 JDBC 的 Asynctask 中应用程序崩溃【英文标题】:App crash in Asynctask with JDBC by Implementing Proguard in Android 【发布时间】:2017-12-18 12:56:17 【问题描述】:

我的应用程序之前运行良好,但在我的项目中启用 ProGuard 后,应用程序在“发布”和“调试”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) 

这是我的异步任务

     //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 +"'\n 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'
    

【问题讨论】:

这是我的连接尝试 Class.forName("com.mysql.jdbc.Driver");连接 = DriverManager.getConnection(url, 用户, 密码); catch (SQLException se) Log.e("error here 1 : ", se.getMessage()); catch (ClassNotFoundException e) Log.e("error here 2 : ", e.getMessage()); catch (Exception e) Log.e("error here 3 : ", e.getMessage()); 忘记包含抱歉,Connection connection = null; 【参考方案1】:

已通过将此规则包含在我的 proguard 中修复

    -dontshrink

【讨论】:

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

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

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

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

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

评估指令时出现 Proguard 错误

如何通过正确的视图 z 顺序在 android 中实现这个基本布局