Thursday 13 June 2013

Violet Example for Android using Cydia

Cydia Substrate has just arrived on Android! 
I'm assuming you all folks know what I'm talking about, (that's the reason why you're reading this post, right?) so I'm not making much of an introduction here. 

Let's talk about the example you can find in here.

We found some things this tutorial doesn't say. That's why we tried to do ours as detailed as possible, highlighting the problems we encountered while trying to make it work on our own.

1. Before starting: you have to install Cydia Substrate in your device. 
If you get a segmentation fault error during installation you may have an old or maybe buggy version of the .apk (android application package). To assure you have the right one, enter this chat and ask for a one that works. 
About that chat: it was really helpful for us because people there answered ever time we had an issue (they take their time, but if they can help, they do).

2. Make sure you can install the violetexample.apk and that it works (you will have enough time to make it for your own, but you have to be sure Substrate is working properly in your device). We used the last one saurik put in his web, so if the one I linked is not working, go again to the chat and download it from there.

Notice that once you install violetexample.apk, you have to open Substrate application and click 'Restart System (Soft)' option, so you can see the changes applied. 
We found out how to rollback the changes we've made by hooking the getColor method: just uninstall your application! Easy peasy!

3. If everything works as expected, you're ready to open Eclipse IDE (or your favorite java IDE) and start your own violetexample android application.

In order to do so, I'm using Eclipse IDE, android SDK and ADT plugin. Please, make sure you have those three working together or follow this nice tutorial.

First of all, you need to install Cydia Substrate API in your computer. This step was quit easy following this other tutorial

You will find a new folder inside you're ADT folder named 'saurikit'. You have to copy the jar file saurikit/cydia_substrate/substrate-api.jar into your project's libs folder.

4. As we expected to have some feedback whenever our application stars running or not, we create a new Android Application Project with the default main activity created by Eclipse: 

  1. File->New->Android Application Project.
  2. Name it and next, next, next, next, finish.
     Note that a new activity named NewActivity will be created.

5. Modify 'AndroidManifest.xml' and add the following lines:

  1. <uses-permission android:name="cydia.permission.SUBSTRATE"/>
     Make sure you add it between <manifest> tags right before
  the <application> tag.
  2. <meta-date android:name="com.saurik.substrate.main"
                android:value=".Main"/>
Make sure you add it between <application> tags right after you close the application opening tag and not inside it.

Notice that with meta-data you're defining the entry point for Substrate, in this case it will be a Main class.

6. Create a Main class: 
1. File->New->Class
2. Finish
3. Copy and paste this code.

7. Run As -> Android Application
  1. Make sure you have a 'Success!' message in the Android Console after the 'installing ... .apk' one.
2. Open Substrate application and click 'Restart System (Soft).
3. Then waaaaaaaaaait for it, waaait for it... voilà! There you have it!




You can also change the line where you return color and change it to get any color you want!

Here you have a more manly calculator:




And a red one:




And you can show it to your friends, to your family and... all right, enough! I think you got it :)

I hope this tutorial was helpful, and feel free to ask if you have any doubt.







  


12 comments:

  1. Hi mate:
    Thx for your work about this brilliant article. But when I try it step by step. I don't get the right result. I created a new Android App with no Acitivity (Another one has MainActivity actually). The Manifest File is copy from this http://pastebin.com/zJrsPpUu , and Main.java is copy from http://pastebin.com/W7nrXabM. Is there something wrong I have done? I found the apk file created by my project is about 240k, but the violetexample.apk is only 7k, if i have choice a wrong app project type? Thx very much.


    ReplyDelete
    Replies
    1. Hi, Germini.
      I see the manifest has a reference to MainActivity. Aren't you using it?
      Don't you have a MainActivity.hava? My first approach would be to create one and make sure the app is starting correctly (the main activity will give you some feedback if your app does start or not). Pay attention to the file paths and change them according to your project's path (note it says "com.example.cydiaexample.MainActivity" and you may have another path in your own project).
      After that, I would add some prints so you make sure the hook is working correctly:
      1. add a print to make sure it is initializing as expected. Something like this in your Main.java:
      ...
      static void initialize() {
      Log.d("DEBUG", "hook initialized");
      MS.hookClassLoad("android.content.res.Resources",
      new MS.ClassLoadHook() {
      ...


      2. add a print to make sure it finds the getColor method. Something like this in your Main.java:
      ...
      Method getColor;
      try {
      getColor = resources.getMethod("getColor",
      Integer.TYPE);
      } catch (NoSuchMethodException e) {
      Log.d("DEBUG", Log.getStackTraceString(e));
      getColor = null;
      }
      ...

      And then, check logcat to see those prints. You can read this to understand how logcat works: http://cydiaforandroid.blogspot.com.ar/2013/06/debugging-android-code.html.

      Another detail: are you restarting the device after installing this new app? Consider no Cydia changes are applied if you don't do that.

      Let me know if it helps or give me some more details about your project.

      Thanks

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi Ashy
      Thx for your reply. I changed my code as you mentioned. I can see the DEBUG info "hook initialized" now, but nothing else. And I added some other "DEBUG" codes in Main.java :

      Log.d("DEBUG", "class loding");
      Method getColor;

      Log.d("DEBUG", " get color Nunll?");
      if (getColor != null) {...

      But they all doesn't show, so I think it must be wrong in "classLoaded", I don't know it very well, if I declared it wrong?

      Thx very much again.

      Delete
    2. Germini:
      As resource class loaded is not being call, I'd check:
      1. is Cydia linked?
      2. the class you are hooking, is it "android.content.res.Resources"? please, make sure you have no typo.
      3. Which Android version are you using? And Cydia?

      Thanks

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Ashy:
      1. My Cydia has linked I think, because its first button shows " Unlink Substrate Files";
      2. The class I am hooking is actually "android.content.res.Resources"; The source is as fellow.
      3. My android version is 4.1.2 and Cydia is downloaded from google play, its version is v0.9.4011. The

      violetExample can work on it.

      The source code of my project as fellow:

      package com.example.color;

      import java.lang.reflect.Method;
      import android.util.Log;
      import com.saurik.substrate.MS;

      public class Main {
      static void initialize() {

      Log.d("DEBUG", "hook initialized");

      MS.hookClassLoad("android.content.res.Resources",
      new MS.ClassLoadHook()
      {
      public void classLoaded(Class resources)
      {

      Log.d("DEBUG", "class loding");

      Method getColor;
      try {
      getColor = resources.getMethod("getColor",Integer.TYPE);
      } catch (NoSuchMethodException e) {

      Log.d("DEBUG", Log.getStackTraceString(e));

      getColor = null;
      }

      Log.d("DEBUG", " get color Nunll?");

      if (getColor != null) {

      Log.d("DEBUG", "color is cheating!!!");

      final MS.MethodPointer old = new MS.MethodPointer();
      MS.hookMethod(resources, getColor,
      new MS.MethodHook() {
      public Object invoked(Object resources,Object... args)
      throws Throwable {
      int color = (Integer) old.invoke(resources, args);
      return color & ~0x0000ff00 | 0x00ff0000;
      }
      }, old);
      }
      }
      }
      );
      }
      }

      I really appreciate your help!!!

      Delete
    5. Everything seems to be ok. Can you send me your project in any way? So I can check if its code issue or what?
      My email address: ashy.on.line@gmail.com.
      You van share me it by dropbox, google drive, email itself or whatever you prefer :)

      Delete
  3. I am very grateful for your help. But I am on a business trip these days. I will package my project and send it to your email[ashy.on.line@gmail.com] after I back to home. Thx again. :)

    ReplyDelete
    Replies
    1. Ok. I'm looking forward to hearing from you. Have a nice trip.

      Delete
  4. INTERNATIONAL CONCEPT OF WORK FROM HOME
    Work from home theory is fast gaining popularity because of the freedom and flexibility that comes with it. Since one is not bound by fixed working hours, they can schedule their work at the time when they feel most productive and convenient to them. Women & Men benefit a lot from this concept of work since they can balance their home and work perfectly. People mostly find that in this situation, their productivity is higher and stress levels lower. Those who like isolation and a tranquil work environment also tend to prefer this way of working. Today, with the kind of communication networks available, millions of people worldwide are considering this option.

    Women & Men who want to be independent but cannot afford to leave their responsibilities at home aside will benefit a lot from this concept of work. It makes it easier to maintain a healthy balance between home and work. The family doesn't get neglected and you can get your work done too. You can thus effectively juggle home responsibilities with your career. Working from home is definitely a viable option but it also needs a lot of hard work and discipline. You have to make a time schedule for yourself and stick to it. There will be a time frame of course for any job you take up and you have to fulfill that project within that time frame.

    There are many things that can be done working from home. A few of them is listed below that will give you a general idea about the benefits of this concept.

    Baby-sitting
    This is the most common and highly preferred job that Women & Men like doing. Since in today's competitive world both the parents have to work they need a secure place to leave behind their children who will take care of them and parents can also relax without being worried all the time. In this job you don't require any degree or qualifications. You only have to know how to take care of children. Parents are happy to pay handsome salary and you can also earn a lot without putting too much of an effort.

    Nursery
    For those who have a garden or an open space at your disposal and are also interested in gardening can go for this method of earning money. If given proper time and efforts nursery business can flourish very well and you will earn handsomely. But just as all jobs establishing it will be a bit difficult but the end results are outstanding.

    Freelance
    Freelance can be in different wings. Either you can be a freelance reporter or a freelance photographer. You can also do designing or be in the advertising field doing project on your own. Being independent and working independently will depend on your field of work and the availability of its worth in the market. If you like doing jewellery designing you can do that at home totally independently. You can also work on freelancing as a marketing executive working from home. Wanna know more, email us on workfromhome.otr214423@gmail.com and we will send you information on how you can actually work as a marketing freelancer.


    Internet related work
    This is a very vast field and here sky is the limit. All you need is a computer and Internet facility. Whatever field you are into work at home is perfect match in the software field. You can match your time according to your convenience and complete whatever projects you get. To learn more about how to work from home, contact us today on workfromhome.otr214423@gmail.comand our team will get you started on some excellent work from home projects.


    Diet food
    Since now a days Women & Men are more conscious of the food that they eat hence they prefer to have homemade low cal food and if you can start supplying low cal food to various offices then it will be a very good source of income and not too much of efforts. You can hire a few ladies who will help you out and this can be a good business.

    Thus think over this concept and go ahead.

    ReplyDelete
  5. Thanks for the best blog. it was very useful for me.keep sharing such ideas in the future as well.
    The Xmod Games Hacking APK software
    Xmod Games Lite Hack

    ReplyDelete