Popular Posts

Tuesday, September 27, 2016

RootCheckUtil Library

RootCheckUtil Library



This library is specifically designed to check if the user has root access or can gain root access in android OS.
It is necessary if you have some sensitive information in your app and don't want the user to get inside the android system and steal or tamper your app.
If user has root access aka administrator access, he can access the database files, shared preference and software backed key-store files.

This library has 2 methods:


1. isDeviceRooted() - This will return a Boolean value true if the device is rooted.
Note: It will return true if used in an emulator since emulator is a virtual test device.

2. isEmulator() - This will return a Boolean value true if the device is Emulator.

You can find the library here.

Steps to include this library in your project:-


1. Create a android project.

2. Go to project root folder.

Navigate to \"app_name"\app\libs\

3. Paste the rootcheckutil.aar downloaded from the github inside the libs folder.

4. Go to File-> New -> New Module.





5. Navigate to libs folder of app where we pasted rootcheckutil.aar.


6. Sync the gradle.

7. In the project structure add the rootcheckutil as a module dependencies.


8. Now you can use the library.

Eg Code:-
TextView tv = (TextView) findViewById(R.id.tv);

tv.setText("Is Device Rooted: "+ RootCheckUtil.isDeviceRooted());

TextView tv1 = (TextView) findViewById(R.id.tv1);

tv1.setText("Is Emulator: "+ RootCheckUtil.isEmulator());

Thursday, September 15, 2016

NDK - Native Development Kit - Android



NDK - Native Development Kit


The Android NDK is a toolset that lets you implement parts of your app using native-code languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages.

1. 1 Advantages:-


1.      Native C code would use the underlying operating system's (Linux) APIs (system calls), and hence would be much faster than java code as it would have to be interpreted through the JVM.

2.      It’s harder for hackers to learn what your code is doing, and to break it. But not impossible.

1.2 Disadvantages:-


1.      NDK only compiles to specific CPUs (whereas staying in Java land means it will work on any targeted version of Android).

2.      NDK cannot access lots of APIs available in the Android SDK directly, and developing in NDK will always introduce extra complexity in application

As of now the NDK can be run on these processors architecture:-

1.      arm64-v8a

2.      armeabi

3.      armeabi-v7a

4.      mips

5.      mips64

6.      x86

7.      x86_64

1.3 Implementation:-

1.      Download NDK from Google repository.
  • Android Developer website - https://developer.android.com/ndk/downloads/index.html
  • Google github repository - https://github.com/android-ndk
2.      Extract the NDK to any location.

3.      Create a new Project.

4.      Go to File -> Project Structure or ctrl+alt+shift+s.

5.      Set the path of NDK in Android NDK Location.



6.      Build Gradle.

7.      Change the project navigator structure from Android to Project



8.      Create new folder inside the project main as jni. Jni is the path in which we will store the C++ and make files.

9.      Inside jni folder make .c file (e.g.:- ndk.c).

10.  Make two make files Android.mk and Application.mk inside jni.


11.  ndk.c will contain the c code which we need to implement.

12.  Android.mk file will contain all the prerequisite to make the .so Linux library file.

13.  Application.mk file will contain information on which CPU architectures to support.

Returning some string value from C file:-



#include<jni.h>
#include<string.h>
jstring Java_steff_com_createsofile_MainActivity_getString(JNIEnv* env, jobject obj){
   
return (*env)->NewStringUTF(env,"This is the first NDK program");
   
}


NOTE: - All the variables in NDK will start from ’j’. eg- jint, jlong, jstring etc.

  • getString is the name of the function.
  • jstring is the return type of the function getString.
  • steff_com_createsofile is the package name of the project.
  • MainActivity is the java class where the function is going to be loaded and called.

1.3.1 Making .mk file:-

1.    Android.mk:-



LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndk
LOCAL_SRC_FILES := ndk.c
include $(BUILD_SHARED_LIBRARY)


LOCAL_PATH := $(call my-dir)

An Android.mk file must begin with the definition of the LOCAL_PATH variable.It is used to locate source files in the development tree. In this example, the macro function 'my-dir', provided by the build system, is used to return the path of the current directory

Include $(CLEAR_VARS)

The CLEAR_VARS variable is provided by the build system and points to a special GNU Makefile that will clear many LOCAL_XXX variables, with the exception of LOCAL_PATH.

LOCAL_MODULE := ndk

The LOCAL_MODULE variable must be defined to identify each module you describe in your Android.mk. The name must be *unique* and not contain any spaces.

LOCAL_SRC_FILES := ndk.c

The LOCAL_SRC_FILES variables must contain a list of C and/or C++ source files that will be built and assembled into a module.

include $(BUILD_SHARED_LIBRARY)

The BUILD_SHARED_LIBRARY is a variable provided by the build system that points to a GNU Makefile script that is in charge of collecting all the information you defined in LOCAL_XXX variables since the latest 'include $(CLEAR_VARS)' and determine what to build, and how to do it exactly. There is also BUILD_STATIC_LIBRARY to generate a static library.

2.    Application.mk



APP_ABI := all


APP_ABI := all

This will create the library file for each CPU Architecture separately. We can also create the library to support only some CPU architecture.

1.3.2 Creating the .so file:-

1.      Go to project root folder.


2.      Inside the Project folder press Shift + mouse left click and select “Open Command Prompt here”

3.      Type the path of NDK root for eg :- “D:\Android\android-ndk-r12b”

4.      And then type the command ndk-build and execute it.

“D:\Android\android-ndk-r12b\ndk-build”


5.      Now we can see the obj folder created in the Android Project navigator.

6.      Inside the obj folder we can see the .so Linux library file created for each CPU Architecture.




7.      Create a new folder in App -> src -> main. Name it as jnilibs.

8.      And copy the folder of each supported cpu architecture from obj folder to the jnilibs folder.


Loading library and calling the C function in Android Studio:-

1.      Inside the class where we are going to use the NDK Library, we need to define the function with native keyword. So that compiler can know where to look for that function.

2.      We need to also load the library in the class with System.loadLibary().

3.      Now we can call the function inside the class.



import android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.widget.TextView;

public class
MainActivity extends AppCompatActivity {

   
public native String getString();
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_main);
       
TextView tv = (TextView)findViewById(R.id.tv);
       
System.loadLibrary("ndk");
       
tv.setText(getString());
   
}
}


4.      In this code we are getting the string from the NDK function and setting it to the TextView.

5.      Build and run the project.