通过在 Android 中实施 Proguard,使用 JDBC 在 Asynctask 中应用程序崩溃
App crash in Asynctask with JDBC by Implementing Proguard in Android
我的应用程序之前工作正常,但在我的项目中启用 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.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.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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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 +"'\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'
}
}
已通过将此规则添加到我的混淆器中进行修复
-dontshrink
我的应用程序之前工作正常,但在我的项目中启用 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.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.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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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 +"'\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'
}
}
已通过将此规则添加到我的混淆器中进行修复
-dontshrink