0

dev env is below:
androidStudio :4.2.2:
wrap gradle:https://services.gradle.org/distributions/gradle-4.10.1-bin.zip:
android Gradle plugin:'com.android.tools.build:gradle:3.2.0':
and i simplely create test code:

package com.example.myapplication;


import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testAssert();
    }

    private void testAssert() {
        assert Looper.myLooper()!=Looper.getMainLooper();
        Log.i("yich","pass!!!!!!!!!");
    }
}

android gradle config is below:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 29
//    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
}

when I run this code by android studio, app work fine, the 'assert' keywrod code not work,so i decompile apk i found no 'assert' code in my apk,pic below:decompile code screenshot ,
i guess it may be AGP's bug when package code into apk,when compile MainActivity.class to dex file ,the assert code is in class file,but i found that assert code lose in dex file,so what i do wrong to make assert code lose when packaging APk? need some help!!!

yuqi
  • 1
  • 1
  • This may help you. https://stackoverflow.com/questions/6176441/how-to-use-assert-in-android – ocos Mar 23 '22 at 09:54
  • @ocos i really want to kown why "assert" keyword code miss in my apk when I decompile apk,and I update android gradle plugin to 4.2.0 and repackage,I finally found 'assert' code in decompile apk, and finally this "assert" code work in apk,so different APG version ,why android gradle plugin 3.2.0 dex processing will lose "assert" code? – yuqi Mar 23 '22 at 11:23
  • I tested it, and only debug releases had assert instructions in bytecode. I think, Assert instructions are automatically removed automatically from release builds. You may test (I really do not recommend it in production release) by putting `debuggable true` in release buildType. It may possible having asserts without debuggable true but I am not sure. https://jakewharton.com/d8-optimization-assertions/ – ocos Mar 23 '22 at 12:43
  • @ocos thks bro very much,I know that different is caused by D8 ,below APG 4.1,the assert will not package in apk, over AGP 4.1 it will package in apk。 – yuqi Mar 24 '22 at 03:29

0 Answers0