Introduction to Huawei lightweight crash analytics SDK

RChancha
4 min readJul 24, 2020

--

Introduction:

Whether you are tracking down a weird behavior in your app or chasing a crash in app making the user frustrated, getting a precise and real time information is important. Huawei crash analytics is a primary crash reporting solution for mobile. It monitors and captures your crashes, intelligently analyzes them, and then groups them into manageable issues. And it does this through lightweight SDK that won’t bloat your app. You can integrate Huawei crash analytics SDK with a single line of code before you publish.

Prerequisite:

  1. Android Studio is recommended, which supports Android 4.2 (API level 17) and later versions.
  2. Google Chrome is recommended for accessing the console of AppGallery Connect.
  3. Before developing an app, you will need to register as a HUAWEI developer. Please refer to Register a HUAWEI ID.
  4. Integrating app gallery connect SDK. Please refer to AppGallery Connect Service Getting Started.
  5. Android device or simulator.

Integrating Crash SDK:

  1. Crash SDK requires Analytics kit to report crashes. Therefore we need to enable Analytics kit before integrating Crash SDK. Please refer to Service Enabling.
  2. Add AGC connect plugin in in app-level build.gradle.

apply plugin: 'com.huawei.agconnect'

3. Integrate Analytics kit by adding following code in app-level build.gradle.

implementation 'com.huawei.hms:hianalytics:4.0.3.300'

4. Integrate Crash SDK by adding following code in app-level build.gradle.

implementation 'com.huawei.agconnect:agconnect-crash:1.3.1.300'

5. Add following code in root-level build.gradle.

buildscript {
repositories {
maven { url 'http://developer.huawei.com/repo/' }
}
}
dependencies {
classpath "com.huawei.agconnect:agcp:$agcpVersion"
}
allprojects {
repositories {
maven { url 'http://developer.huawei.com/repo/' }
}
}

6. Declare the following permissions in Androidmanifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Application implementation:

Before Implementation, let’s see basic UI design which contains few UI elements.

1. Crash service can be enabled using android switch.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainmenu, menu);
MenuItem item = menu.findItem(R.id.myswitch);
item.setActionView(R.layout.switch_layout);
Switch mySwitch = item.getActionView().findViewById(R.id.switchActionBar);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
AGConnectCrash.getInstance().enableCrashCollection(isChecked);
}
});
return true;
}

2. MainActivity code.

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button bValidateString, bDivide, bMemoryleak;
private EditText etNum , etDen , etString;
private static final String TAG = "MainActivity";
private Random random = new Random();
public static final ArrayList<Double> list = new ArrayList<Double>(1000000);
@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
bValidateString = findViewById(R.id.bValidateString);
bDivide = findViewById(R.id.bdivide);
bMemoryleak = findViewById(R.id.bMemoryleak);
etDen = findViewById(R.id.etden);
etNum = findViewById(R.id.etnum);
etString = findViewById(R.id.et1);
bDivide.setOnClickListener(this);
bValidateString.setOnClickListener(this);
bMemoryleak.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainmenu, menu);
MenuItem item = menu.findItem(R.id.myswitch);
item.setActionView(R.layout.switch_layout);
Switch mySwitch = item.getActionView().findViewById(R.id.switchActionBar);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
AGConnectCrash.getInstance().enableCrashCollection(isChecked);
}
});
return true;
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.bValidateString){
String text = etString.getText().toString();
if(text.length() > 6 ) {
throw new IndexOutOfBoundsException();
} else {
Toast.makeText(MainActivity.this, "Text entered : "+ text, Toast.LENGTH_SHORT).show();
}
}
else if(v.getId() == R.id.bdivide) {
if(etDen.getText().toString().isEmpty()) {
return;
}
long numerator = Long.parseLong(etNum.getText().toString());
long denomiator = Long.parseLong(etDen.getText().toString());
long result = numerator/denomiator;
Toast.makeText(MainActivity.this, "result : " + String.valueOf(result), Toast.LENGTH_SHORT).show();
}
else if(v.getId() == R.id.bMemoryleak) {
for (int i = 0; i < 1000000; i++) {
list.add(random.nextDouble());
}
System.gc();
try {
Thread.sleep(100); // to allow GC do its job
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Monitoring crash Report in App gallery connect:

When app encounters crash , Crash service reports the crash on dashboard in App Gallery connect. To monitor the crash

  1. Sign in to App Gallery connect and select my project.
  2. Choose the app.
  3. Select Quality -> Crash on left panel of the screen.

4. Developer can add filer based on OS, appversion and device to narrow down the cause of error.

5. Select Problem tab to see list of errors.

6. Clicking on error will show the detailed log.

Crash Service notification:

Crash service monitors app in real time and notifies developer if any crash occurs. Developers will be reminded if a crash meets the following condition in last one hour:

1. The crash rate of a problem is greater than the threshold 1% (threshold cannot be modified during writing of article)

2. Number of app launches is greater than 500.

3. No crash notification has been triggered for this problem before. That is, the system does not repeatedly send notifications for the same problem.

To enable Crash service notifications:

  1. Sign in AppGallery and Select User and Permissions.
  2. Select User > Personal Information.
  3. Select the check box under Email for Crash notification under notification.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

RChancha
RChancha

No responses yet

Write a response