BlogsEducationFeatured

How to implement a Monero Miner in an Android app

Monetizing your Android app with cryptocurrency mining

 

How to implement a Monero miner in an Android App


Background Information

Monero is interesting, due to the fact it can utilize both GPUs and CPUs to mine. Therefore, many websites have Monero miners embedded in them to utilize user’s CPUs as a monetization method. Let’s implement the miner inside an Android app.

CoinHive is an easy way to setup a Monero miner, for the reason that we only have to embed JavaScript in our website.

A user on GitHub under the name theapache64 created an Android library utilizing CoinHive to embed the miner in an Android application. Before I go on and show you how to implement the miner, go check the library on Github out!

Also, you will need to make an account on CoinHive to access the SITE-KEY  that we will use when initializing it in the app.

Android Implementation

Now is time for our implementation! First, you need to add the dependency to the build.gradle.

implementation ‘com.theah64.coinhive:coinhive:1.2.2’

Next, we will need to create a class which, we will initialize CoinHive in. This is where the SITE-KEY will be used.

public class App extends Application {
     
     @Override
     public void onCreate() {
         super.onCreate();

         /* Start Miner */
         CoinHive.getInstance()
                 .init("SITE-KEY") // needed
                 .setNumberofThreads(4) // optional
                 .setIsAutoThread(true) // optional
                 .setThrottle(.2) // optional
                 .setLoggingEnabled(true) // optional -- Logcat
                 .setForceASMJS(false); // optional
     }
}

Now we need to extend our MainActivity from BaseCoinHiveActivity, which comes with some methods that you can output the status of the miner with.

public class MainActivity extends BaseCoinHiveActivity {
    
     @Override
     public void onRunning(double hashesPerSecond, long totalHashes, long acceptedHashes) {
          // Miner status outputs here

     }

     @Override
     private void onMiningStarted() {


     }
    
     @Override
     private void onMiningStopped() {


     }
      
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     
     }
}

One last step is to add our Appclass to our AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="package-name">  

     <uses-permission android:name="android.permission.INTERNET" />
                                                
     <application android:name=".App"
          android:theme="@style/AppTheme"
          android:icon="@mipmap/ic_launcher_round">                                                                     
          <activity
               android:name=".MainActivity"
               android:theme="@style/Theme.AppCompat.NoActionBar"
               android:label="@string/app_name"
               android:screenOrientation="portrait">                 
          </activity>
     </application>
</manifest>

Here are the calculations for profitability using CoinHive. This is definitely not a monetization method that you can implement by itself. However, it does in fact add a bit of extra cash flow.

CoinHive's current payout
current payout 0.00005388 XMR per 1M hashes

Calculation Model
1,000 daily active users
User usage rate (time app is open) : 10 minutes/day

10 minutes = 600 seconds
average_hash_rate = ~ 20 H/S

hashes/day = daily_active_users * usage_rate_in_seconds
         * average_hash_rate

Profit/Day
hashes/day = 1,000 * 600 * 20 = 12,000,000
XMR/day = 0.00005388 XMR *  12M hashes = 0.00064656 XMR
USD/day = 0.00064565 XMR * $143.46 = $0.093

Profit/Month
hashes/month = 1,000 * 600 * 20 * 30 = 360,000,000
XMR/month= 0.00005388 XMR *  360M hashes = 0.0193968 XMR
USD/month = 0.00064565 XMR * $143.46 = $2.78

Recalculate if your app has a different amount of daily active users.


If you enjoyed or learned something from this article please consider rating it below!

 

[Total: 4 Average: 4]
Show More

Related Articles

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Close