Integration of Huawei Crash Service in Quiz Android app (Kotlin) – Part 5
Tags: crash, kotlin, android, xml
Published: 2022-06-13 09:58:43


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ch7q5oultu10u7wnomg.png)
**Introduction**
In this article, we can learn about the basic questions on different country flags of **Quiz application**. Also, we can learn to integrate **Huawei Crash service** of **AppGallery Connect** in Quiz application.

If you are new to this application, follow my previous articles.

[https://forums.developer.huawei.com/forumPortal/en/topic/0202877278014350004](https://forums.developer.huawei.com/forumPortal/en/topic/0202877278014350004)

[https://forums.developer.huawei.com/forumPortal/en/topic/0201884103719030016?fid=0101187876626530001](https://forums.developer.huawei.com/forumPortal/en/topic/0201884103719030016?fid=0101187876626530001)

[https://forums.developer.huawei.com/forumPortal/en/topic/0202890333711770040](https://forums.developer.huawei.com/forumPortal/en/topic/0202890333711770040)

[https://forums.developer.huawei.com/forumPortal/en/topic/0201892776213940037](https://forums.developer.huawei.com/forumPortal/en/topic/0201892776213940037)

**What is Crash service?**
The AppGallery Connect Crash service provides a powerful **lightweight** solution to app crash problems. With the service, you can quickly **detect**, **locate**, and **resolve** app crashes (unexpected exits of apps), and have access to highly readable crash reports in real-time, without the need to write any code.

Huawei Crash is a real-time crash reporting tool that helps in **tracking**, **prioritizing**, and **fixing** stability issues that compromise the quality of your app.

To ensure stable running of your app and prevent user experience deterioration caused by crashes, you are advised to monitor the running status of your app on each device, which is the key. The Crash service provides real-time reports, revealing any crash of your app on any device. In addition, the Crash service can intelligently aggregate crashes, providing context data when a crash occurs, such as environment information and stack, for you to prioritize the crash easily for rapid resolution.

**Requirements**
1. Any operating system (MacOS, Linux, and Windows).
2. Must have a Huawei phone with HMS 4.0.0.300 or later.
3. Must have a laptop or desktop with Android Studio, JDK 1.8, SDK platform 26, and Gradle 4.6 and above installed.
4. Minimum API Level 24 is required.
5. Required EMUI 9.0.0 and later version devices.

**How to integrate HMS Dependencies**

- First register as Huawei developer and complete identity verification on the Huawei developers website, refer to register a [Huawei ID](https://developer.huawei.com/consumer/en/doc/start/registration-and-verification-0000001053628148).

- Create a project in android studio, refer [Creating an Android Studio Project](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#1).

- Generate a **SHA-256 certificate fingerprint**.

- To generate **SHA-256 certificate fingerprint**. On the right-upper corner of the android project click **Gradle**, choose **Project Name > Tasks > android**, and then click **signingReport**, as follows.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpabizv3t6e686bqef7m.png)

Note: Project Name depends on the user created name.

- [Create an App in AppGallery Connect](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#0).

- Download the **agconnect-services.json** file from **App information**, copy and paste in android **Project** under **app** directory, as follows.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m70fi4lcis0cp7jxn0dn.png)

- Enter **SHA-256 certificate fingerprint** and click **Save** button, as follows.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhx04jub1aum9n3sl85y.png)


- Click **Manage APIs** tab and enable **HUAWEI Analytics**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5fzrun1ur3w8loud3uf.jpg)

- Add the below maven URL in **build.gradle(Project)** file under the repositories of **buildscript**, **dependencies** and **allprojects**, refer [Add Configuration](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#6).

```
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
```

- Add the below plugin and dependencies in **build.gradle(Module)** file.

```
apply plugin: id 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
// Huawei Analytics Kit
implementation 'com.huawei.hms:hianalytics:6.4.0.300'
// Huawei Crash Service
implementation 'com.huawei.agconnect:agconnect-crash:1.6.0.300'
```

- Now Sync the gradle.

- Add the required permission to the **AndroidManifest.xml** file.

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

**Configuration in AppGallery Connect**

Find the Crash using AppGallery connect dashboard.

Choose **My Projects > Quality > Crash**, and click **Enable now**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvabqyiw0f2lmhgsu2za.jpeg)

**Let us move to development**

I have created a project on Android studio with empty activity let us start coding.

- Initialize an AGConnectCrash before other methods and Enable crash.

```
AGConnectCrash.getInstance().enableCrashCollection(true)
```

- Test the below method in your code.

```
AGConnectCrash.getInstance().testIt(this@Home)
```

- Customizing User IDs: Analyzing crashes by a user to resolve crashes using User ID. The user can be uniquely identified after a crash occurs.

```
AGConnectCrash.getInstance().setUserId("123456789")
```

- Recording Custom Logs: we can record custom logs, Recorded logs will be reported together with the crash data.

```
AGConnectCrash.getInstance().log("catch exception")
```

- Adding a Custom key-value pair: we can add a custom key-value pair to record the status. The recorded status will be reported together with the crash data.

```
AGConnectCrash.getInstance().setCustomKey("keyInt", 1)
```

In the **Home.kt** we can find the business logic for crash service.

```
class Home : AppCompatActivity(), HomeAdapter.ItemListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        // Crash Service 
        AGConnectCrash.getInstance().enableCrashCollection(true)
        // Record Custom Status
        AGConnectCrash.getInstance().setCustomKey("keyInt", 1)
        // Set the user ID
        AGConnectCrash.getInstance().setUserId("123456789")
        try {
            throw NullPointerException()
        } catch (ex: NullPointerException) {
            AGConnectCrash.getInstance().log("catch exception")
            // Record non-fatal exception
            AGConnectCrash.getInstance().recordException(ex)
        }

    }

    override fun onItemClick(item: Int) {
        when(item ) {
            0 -> {val intent = Intent(this@Home, AndroidActivity::class.java)
                startActivity(intent)
            }
            1 ->  {val intent = Intent(this@Home, HMSActivity::class.java)
                startActivity(intent)
            }
            2 ->  {val intent = Intent(this@Home, SportsActivity::class.java)
                startActivity(intent)
            }
            3 ->  {val intent = Intent(this@Home, QuizActivity::class.java)
                startActivity(intent)
                 // Test
                 AGConnectCrash.getInstance().testIt(this@Home)
            }
        }
    }

}
```


In the **QuizActivity.kt** we can find the business logic for country flags questions.

```
class QuizActivity : AppCompatActivity(), View.OnClickListener {

    private var nCurrentPosition: Int = 1
    private var nQuestionsList: ArrayList<Question>? = null
    private var nSelectedOptionPosition: Int = 0
    private var nCorrectAnswers: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_quiz)

        nQuestionsList = Constants.getQuestions()
        setQuestion()

        txt_option_one.setOnClickListener(this)
        txt_option_two.setOnClickListener(this)
        txt_option_three.setOnClickListener(this)
        txt_option_four.setOnClickListener(this)
        btn_submit.setOnClickListener(this)

    }

    private fun setQuestion() {
        val question = nQuestionsList!![nCurrentPosition -1]
        defaultOptionsView()
        if(nCurrentPosition == nQuestionsList!!.size){
            btn_submit.text = "FINISH"
        } else {
            btn_submit.text = "SUBMIT"
        }
        progressBar.progress = nCurrentPosition
        txt_progress.text = "$nCurrentPosition" + "/" + progressBar.max
        txt_question.text = question!!.question
        iv_image.setImageResource(question.image)
        txt_option_one.text = question.optionOne
        txt_option_two.text = question.optionTwo
        txt_option_three.text = question.optionThree
        txt_option_four.text = question.optionFour
    }

    private fun defaultOptionsView() {
        val options = ArrayList<TextView>()
        options.add(0, txt_option_one)
        options.add(1, txt_option_two)
        options.add(2, txt_option_three)
        options.add(3, txt_option_four)
        for(option in options){
            option.setTextColor(Color.parseColor("#7A8089"))
            option.typeface = Typeface.DEFAULT
            option.background = ContextCompat.getDrawable(this, R.drawable.border_bg)
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.txt_option_one -> {
                selectedOptionView(txt_option_one, 1)
            }
            R.id.txt_option_two -> {
                selectedOptionView(txt_option_two, 2)
            }
            R.id.txt_option_three -> {
                selectedOptionView(txt_option_three, 3)
            }
            R.id.txt_option_four -> {
                selectedOptionView(txt_option_four, 4)
            }
            R.id.btn_submit -> {
                if(nSelectedOptionPosition == 0){
                    nCurrentPosition++
                    when{
                        nCurrentPosition <= nQuestionsList!!.size -> {
                            setQuestion()
                        } else  -> {
                        val intent = Intent(this, ResultActivity::class.java)
                        intent.putExtra(Constants.CORRECT_ANSWERS, nCorrectAnswers)
                        intent.putExtra(Constants.TOTAL_QUESTIONS, nQuestionsList!!.size)
                        startActivity(intent)
                        }
                    }
                } else {
                    val question = nQuestionsList?.get(nCurrentPosition -1)
                    if(question!!.correctAnswer != nSelectedOptionPosition){
                        answerView(nSelectedOptionPosition, R.drawable.wrong_bg)
                    } else {
                        nCorrectAnswers ++
                    }
                    answerView(question!!.correctAnswer, R.drawable.correct_bg)
                    if(nCurrentPosition == nQuestionsList!!.size){
                       btn_submit.text = "FINISH"
                    } else {
                        btn_submit.text = "Next Question"
                    }
                    nSelectedOptionPosition = 0
                }
            }
        }
    }

    private fun answerView(answer:Int, drawableView:Int){
        when(answer) {
            1 -> {
                txt_option_one.background = ContextCompat.getDrawable(this, drawableView)
            }
            2 -> {
                txt_option_two.background = ContextCompat.getDrawable(this, drawableView)
            }
            3 -> {
                txt_option_three.background = ContextCompat.getDrawable(this, drawableView)
            }
            4 -> {
                txt_option_four.background = ContextCompat.getDrawable(this, drawableView)
            }
        }
    }

    private fun selectedOptionView(tv: TextView, selectedOptionNum: Int){
        defaultOptionsView()
        nSelectedOptionPosition = selectedOptionNum
        tv.setTextColor(Color.parseColor("#363A43"))
        tv.setTypeface(tv.typeface, Typeface.BOLD)
        tv.background = ContextCompat.getDrawable(this, R.drawable.select_bg)
    }

}
```

In the **ResultActivity.kt** we can find the business logic for country flags result view.

```
class ResultActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_result)

        val totalQuestions = intent.getIntExtra(Constants.TOTAL_QUESTIONS, 0)
        val correctAnswers = intent.getIntExtra(Constants.CORRECT_ANSWERS, 0)
        txt_score.text = "Your Score is $correctAnswers out of $totalQuestions"

        btn_finish.setOnClickListener {
            startActivity(Intent(this, Home::class.java))
        }

    }

}
```

Create a data class **Question.kt** to access variables.

```
data class Question(
    val id:Int,
    val question:String,
    val image:Int,
    val optionOne:String,
    val optionTwo:String,
    val optionThree:String,
    val optionFour:String,
    val correctAnswer:Int
)
```

Create object class **Constants.kt** to show questions.

```
object Constants {

    const val TOTAL_QUESTIONS: String = "total_question"
    const val CORRECT_ANSWERS: String = "correct_answers"

    fun getQuestions():ArrayList<Question>{
        val questionsList = ArrayList<Question>()

        // 1
        val que1 = Question(1, "What Country does this flag belong to?",
            R.drawable.angola_flag, "Zambia",
            "Botswana", "Angola", "Namibia", 3)
        questionsList.add(que1)

        // 2
        val que2 = Question(2, "What Country does this flag belong to?",
            R.drawable.barmuda_flag, "Bermuda",
            "Havana", "Merida", "Miami", 1)
        questionsList.add(que2)

        // 3
        val que3 = Question(3, "What Country does this flag belong to?",
            R.drawable.bolivia_flag, "Brazil",
            "Bolivia", "Paraguay", "Uruguay", 2)
        questionsList.add(que3)

        // 4
        val que4 = Question(4, "What Country does this flag belong to?",
            R.drawable.cambodia_flag, "Vietnam",
            "Laos", "Cambodia", "Jakarta", 3)
        questionsList.add(que4)

        // 5
        val que5 = Question(5, "What Country does this flag belong to?",
            R.drawable.chile_flag, "Peru",
            "Panama", "Honduras", "Chile", 4)
        questionsList.add(que5)

        // 6
        val que6 = Question(6, "What Country does this flag belong to?",
            R.drawable.cuba_flag, "Guyana",
            "Cuba", "Ecuador", "Guatemala", 2)
        questionsList.add(que6)

        // 7
        val que7 = Question(7, "What Country does this flag belong to?",
            R.drawable.honduras_flag, "Honduras",
            "Costa Rica", "Puerto Rico", "Dominican Republic", 1)
        questionsList.add(que7)

        // 8
        val que8 = Question(8, "What Country does this flag belong to?",
            R.drawable.mali_flag, "Algeria",
            "Libya", "Mali", "Tunisia", 3)
        questionsList.add(que8)

        // 9
        val que9 = Question(9, "What Country does this flag belong to?",
            R.drawable.norway_flag, "Sweden",
            "Poland", "Austria", "Norway", 4)
        questionsList.add(que9)

        // 10
        val que10 = Question(10, "What Country does this flag belong to?",
            R.drawable.suriname_flag, "Barbados",
            "Haiti", "Suriname", "Belize", 3)
        questionsList.add(que10)
        return questionsList

    }

}
```

In the **activity_quiz.xml** we can create the UI screen for questions.

```
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".flags.QuizActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:gravity="center" >

        <TextView
            android:id="@+id/txt_question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center"
            android:text="What Country does this flag belong to?"
            android:textColor="#363A43"
            android:textSize="18sp">
        </TextView>

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="wrap_content"
            android:layout_height="120dp"
            android:layout_marginTop="16dp"
            android:src="@drawable/android_image"/>

        <LinearLayout
            android:id="@+id/progress_details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:layout_marginTop="16dp" >

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:minHeight="50dp"
                android:progress="0"
                android:indeterminate="false"
                android:max="10">
            </ProgressBar>

            <TextView
                android:id="@+id/txt_progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:gravity="center"
                android:textColorHint="#7A8089"
                android:textSize="14sp"
                tools:text="0/10">
            </TextView>

        </LinearLayout>

        <TextView
            android:id="@+id/txt_option_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="India">
        </TextView>

        <TextView
            android:id="@+id/txt_option_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="Sri Lanka">
        </TextView>

        <TextView
            android:id="@+id/txt_option_three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="Burma">
        </TextView>

        <TextView
            android:id="@+id/txt_option_four"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/border_bg"
            android:padding="15dp"
            android:gravity="center"
            android:textColor="#7A8089"
            android:textSize="18sp"
            tools:text="USA">
        </TextView>

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/design_default_color_primary"
            android:text="Submit"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</ScrollView>
```

In the **activity_result.xml** we can create the UI screen for result.

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/result_bg"
    android:padding="20dp"
    tools:context=".flags.ResultActivity">

    <TextView
        android:id="@+id/txt_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:text="Result"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="22sp">
    </TextView>

    <ImageView
        android:id="@+id/img_trophy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:src="@drawable/trophy"/>

    <TextView
        android:id="@+id/txt_congratulations"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Hi, Congratulations!!"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="25sp">
    </TextView>

    <TextView
        android:id="@+id/txt_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Your Score is 9 out of 10"
        android:textColor="@android:color/secondary_text_dark"
        android:textSize="20sp">
    </TextView>

    <Button
        android:id="@+id/btn_finish"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@android:color/white"
        android:text="Finish"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:textStyle="bold" />

</LinearLayout>
```

**Demo**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8n56hbovuazbqhktwwd.gif)

**Crash Service**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqp3oj6ppbjqco967gyl.JPG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icm7nrmb4xdnhbzquvta.JPG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7wsk7z13jmzq1uibp31.JPG)

**Tips and Tricks**
1. Make sure you are already registered as Huawei developer.
2. Set minSDK version to **24** or later, otherwise you will get **AndriodManifest** merge issue.
3. Make sure you have added the **agconnect-services.json** file to app folder.
4. Make sure you have added **SHA-256 fingerprint** without fail.
5. Make sure all the dependencies are added properly.

**Conclusion**
In this article, we have learned about the basic questions on different country flags of **Quiz application**. Also, we have learned to integrate **Huawei Crash service** of **AppGallery Connect** in Quiz application. Crash service provides a powerful **lightweight** solution to app crash problems. With the service, you can quickly **detect**, **locate**, and **resolve** app crashes (unexpected exits of apps), and have access to highly readable crash reports in real time, without the need to write any code.

I hope you have read this article. If you found it helpful, please provide likes and comments.

**Reference**
Crash Service – [Documentation](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-crash-introduction-0000001055732708)
Crash Service – [Training Video](https://developer.huawei.com/consumer/en/training/course/video/101624680490355847)