Intermediate: Integration of Safety Detect App using Huawei Safety Detect Kit

Ashish Kumar
4 min readMar 12, 2021

Overview

This article provides how to build a secure app using Huawei Safety Detect Kit. With this kit, we can secure our app in terms of malicious app on device, malicious url check, fake user detection or secure wifi connection etc. This kit provides below API’s to secure our app.

  • SysIntegrity: This API is getting used to check if device is secure or not (checks whether device is rooted).
  • AppsCheck: Checks malicious apps on device.
  • UrlCheck: Checks malicious url.
  • UserDetect: Checks if app is interaction with a fake user or not.
  • WifiDetect: Checks whether device using wifi connection is secure or not.

Let us start with the project configuration part:

Step 1: Create an app on App Gallery Connect.

Step 2: Enable the Safety Detect in Manage APIs menu.

Step 3: Create Android Binding Library for Xamarin Project.

Step 4: Integrate Xamarin Safety Detect library.

Step 5: Change your app package name same as AppGallery app’s package name.

a) Right click on your app in Solution Explorer and select properties.

b) Select Android Manifest on lest side menu.

c) Change your Package name as shown in below image.

Step 6: Generate SHA 256 key.

a) Select Build Type as Release.

b) Right click on your app in Solution Explorer and select Archive.

c) If Archive is successful, click on Distribute button as shown in below image.

d) Select Ad Hoc.

e) Click Add Icon.

f) Enter the details in Create Android Keystore and click on Create button.

g) Double click on your created keystore and you will get your SHA 256 key. Save it.

h) Add the SHA 256 key to App Gallery.

Step 7: Sign the .APK file using the keystore for both Release and Debug configuration.

a) Right click on your app in Solution Explorer and select properties.

b) Select Android Packaging Signing and add the keystore file path and enter details as shown in image.

Step 8: Download agconnect-services.json and add it to project Assets folder.

Step 9: Now, choose Build > Build Solution.

Let us start with the implementation part:

SysIntegrity Check:

Step 1: Create a new class for reading agconnect-services.json file.

Step 2: Override the AttachBaseContext method in MainActivity.cs to read the configuration file.

Step 3: Create BottomNavigation inside activity_main.xml.

Step 4: Define navigation.xml inside menu folder.

Step 5: Create SysIntegrityFragment, AppsCheckFragment, UrlCheckFragment and OthersFragment class for bottom navigation.

Step 6: On navigation item selection, remove all fragment and add the specific fragment in MainActivity.cs.

Step 7: create the fg_sysintegrity.xml.

Step 8: After clicking on “Run Detection” button, process the view and call SysIntegrity() method.

Now Implementation done for SysIntegrity check.

Malicious Apps Check Implementation:

Step 1: Create the fg_appscheck.xml.

Step 2: After clicking on AppCheck tab, enable and verify the app checks and get the malicious app list.

Now Implementation done for Malicious Apps check.

Malicious Url Check Implementation:

Step 1: Define fg_urlcheck.xml.

Step 2: Define string array in string.xml.

Step 3: Load UrlCheckFragment.cs after clicking on UrlCheck tab.

Now Implementation done for Malicious Url check.

User Detect implementation:

Step 1: Create fg_others.xml which contains Wifi Detect and User Detect buttons.

Step 2: Load the OthersFragment.cs after clicking on Others tab and detect the wifi and user after button click.

Now Implementation done for User Detect.

Result

Tips and Tricks

Please enable the app check first for getting the malicious app list.

Conclusion

This article helps us to make our app secure with Huawei Safety Detect kit API’s. We can prevent our app from malicious url attack. If our app uses wifi, then it will let us know whether it is secure or not.

References

https://developer.huawei.com/consumer/en/doc/HMS-Plugin-Guides-V1/sysintegrity-0000001058620520-V1

https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Examples-V1/xamarin-sample-code-0000001059247728-V1

--

--