【问题标题】:App is working fine in usb debug mode, but when i generate Signed APK and install it, its not working应用程序在 USB 调试模式下工作正常,但是当我生成签名 APK 并安装它时,它不工作
【发布时间】:2021-09-25 05:06:11
【问题描述】:

我尝试了不同的解决方案,例如在清单文件中提供 INTERNET PERMISION && 通过 useProguard false 关闭 PROGUARD

我的应用名称:SMS_VOICE_NOTIFICATION

我的应用描述:所以每当我的手机收到短信时,它会使用文本转语音读出短信正文,为此我使用前台服务,因为如果我使用后台服务,我的系统会在一段时间后将其杀死。

在调试模式下一切正常,但是当我开始生成签名 apk 时,广播接收器不工作,只是 UI 部分工作

我已经像这样注册了广播接收器。

public class MainActivity extends AppCompatActivity {
private MyReceiver receiver;

@Override
protected void onStart() {

    IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
    receiver = new MyReceiver();
    registerReceiver(receiver,intentFilter);
    super.onStart();
} 

build.gradle(app)

defaultConfig {
    applicationId "com.example.smsassistsigiri"
    minSdkVersion 23
    targetSdkVersion 30
    multiDexEnabled true
    versionCode 1
    versionName "1.0.1"
    resConfigs "en"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        useProguard false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

}

我的 build.gradle 文件

defaultConfig {
    applicationId "com.example.smsassistsigiri"
    minSdkVersion 23
    targetSdkVersion 30
    multiDexEnabled true
    versionCode 1
    versionName "1.0.1"
    resConfigs "en"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        useProguard false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

我的广播接收器类

public class MyReceiver extends BroadcastReceiver {

private static  final String SMS_RECEIVED =  "android.provider.Telephony.SMS_RECEIVED";
//TODO:Implement whats app text to speech
public static final String WHATS_APP_RECEIVED = "";
private static final String TAG = "SmsBroadcastReceiver";
String msg,PhoneNo = "";


@Override
public void onReceive(Context context, Intent intent) {

    Log.i(TAG,"INTENT RECEIVED:"+intent.getAction());
    //if(intent.getAction()==SMS_RECEIVED){
      if(intent.getAction().equals(SMS_RECEIVED)){
        //Retrieve  a map of extended data from the intent
        Bundle dataBundle = intent.getExtras();
        if(dataBundle!=null){
            //creating a PDU (PROTOCOL DATA UNIT)
            Object[]  mypdu = (Object[]) dataBundle.get("pdus");
            final SmsMessage[] message = new SmsMessage[mypdu.length];
            for(int i=0;i<mypdu.length;i++){
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
                    //API leve  > than 23
                    String format = dataBundle.getString("format");
                    message[i] = SmsMessage.createFromPdu((byte[])mypdu[i],format);
                }
                else{
                    message[i] = SmsMessage.createFromPdu((byte[])mypdu[i]);
                }
                msg = message[i].getMessageBody();
                PhoneNo = message[i].getOriginatingAddress();
            }

            SmsService instance = SmsService.getInstance();
            //if startservice button is not pressed then if we get a message then instance will be null so that's why we need to check for null
            if(instance!=null){
                instance.speak(msg);
            }
            Toast.makeText(context, "Message: "+msg+" \nNumber: "+PhoneNo, Toast.LENGTH_SHORT).show();


        }
        else{
            Toast.makeText(context, "Data bundle is null", Toast.LENGTH_SHORT).show();
        }
    }
      else {
          Log.e("SMSNOTRECEIVED","SMSNOTRECEIVED");
      }
}

}

主要文件

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<application
    android:name=".App"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.SMSAssist">
    <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" ></action>
    </intent-filter>
    </receiver>


    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".SmsService"/>
</application>

【问题讨论】:

    标签: java android android-studio broadcastreceiver apk


    【解决方案1】:

    在进行签名的 android 构建混淆时,有时会删除应用程序代码的必要部分。因此,如果您不使用模拟器,请连接测试设备并仔细观察 logcat,您可以在其中看到应用程序代码中断的问题。 现在您需要添加不想混淆代码的特殊情况。为此:

    • 打开 proguard-rules.pro 文件,该文件位于您的 app 目录下 build.gradle 文件的某个位置。

    • 现在通过添加导致崩溃的类来添加例外情况,如下所示:

      -keep public class com.app.YourActivtyOrFragment { public &lt;methods&gt;; public &lt;fields&gt;; }

    【讨论】:

    • 但我已经关闭了 Proguard 那么 apk 应该可以正常工作吧?如果它被关闭,它不应该破坏我的代码吗?
    • @sigri 是的,它不应该,那么请分享错误日志,您是直接从 android studio 中的构建变体安装它还是先制作一个 apk,然后将其发送到您的设备并在那里安装?
    • 我正在制作一个 apk,然后使用 what's app web 将其发送到我的手机并安装到我的手机上
    • 好的,所以现在当你安装它时它会在启动时崩溃,或者你甚至无法安装它吗?还有任何 logcat 痕迹?
    • 安装没有问题,几乎每个ui部分都工作正常,除了广播接收器无法工作
    【解决方案2】:

    您的应用是否已注册为设备的默认 SMS 或助手处理程序?除非您的应用是默认处理程序,否则您将无法再使用诸如 READ_SMS 之类的权限:

    https://support.google.com/googleplay/android-developer/answer/10208820?visit_id=637627208051926352-3034324628&rd=1#zippy=%2Cpermitted-uses-of-the-sms-and-call-log-permissions

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多