title: How to get ANDROID_ID with Frida
body: <p>The <a href="https://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID" rel="nofollow noreferrer">ANDROID_ID</a> is unique in each application in Android.  </p>
  
  <p>To get the <code>ANDROID_ID</code> with Java inside Android I amd using this code:  </p>
  
  <pre><code>import android.content.Context;
  import android.content.ContentResolver;
  import android.provider.Settings;
  protected void onCreate(...) {
    context = (Context)this;
    String androidId = Settings.Secure.getString((ContentResolver)context.getContentResolver(), (String)"android_id");
  }
  </code></pre>
  
  <p>But I want to run it inside some other application on my andoird phone.<br>
  I wanted to use <a href="https://www.frida.re/" rel="nofollow noreferrer">Frida</a> for this.  </p>
  
  <p>I am loading my injected script with Python:  </p>
  
  <pre><code>import frida
  device = frida.get_usb_device()
  pid = device.spawn(["com.target.app"])
  device.resume(pid)
  time.sleep(1) #Without it Java.perform silently fails
  session = device.attach(pid)
  script = session.create_script(open("jsfrida.js").read())
  script.load()
  
  #prevent the python script from terminating
  raw_input()
  </code></pre>
  
  <p>But inside my script I don't understand how to call it, this is what I tried:  </p>
  
  <pre><code>Java.perform(function (){
    console.log("Inside java perform function");
    var ActivityThread = Java.use('android.app.ActivityThread');
    var Context = Java.use('android.content.Context');
    var settings = Java.use('android.provider.Settings.Secure');
    var ctx = Java.cast(ActivityThread.currentApplication().getApplicationContext(), Context);
    //console.log(ctx.getPackageName());
    //console.log(ctx.getContentResolver());
    var androidId = settings.Secure.getString(ctx.getContentResolver(), "android_id");
    console.log(androidId);
    console.log("END");
   });
  </code></pre>
  
  <p>But it doesn't print the <code>androidId</code>, it only prints:  </p>
  
  <pre><code>Script loaded successfully 
  Inside java perform function
  </code></pre>