Thursday, August 4, 2016

Calling android app from browser (Test)

Deep Link

Friday, May 15, 2015

Android JNI

I use JNI when I hide a certain string/code in android, like if someone reverse engineer your app, your string will be visible and also the code. I usually create some C function to hide the implementation of it from android, like how the data is transform in that function. Some used ProGuard, but my preference is combining the 2, JNI together with proguard. This is not the 100% solution about securing your data/code, but this will make it harder for other exploiters/hackers to obtain your data. Here's the basic JNI, and from here you can implement your own logic. Sample C file, that will return the message passed through JNI. Just simple, It's not as complicated as you think, just like this.

sample.c
#include <jni.h>
jstring Java_com_my_library_Util_message(JNIEnv * env, jobject this, jstring msg) {
    return msg;
}

The convention here is that

  • jstring 
    • This is a String in java.
  • Java_
    • just a prefix but required to be there.
  • com_my_library_
    • is the package name (e.g. com.my.library).
  • Util_
    • the class
  • message
    • the method

Always add the first 2 parameters (JNIEnv * env, jobject this), the 3rd parameter which is "jstring msg" is the String parameter that will be in our java class, you can see it later.

Also create the Android.mk file, and paste the following.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE    := thelib
LOCAL_SRC_FILES := sample.c
include $(BUILD_SHARED_LIBRARY)

This is our class. In android studio you can create a module with the same package name and class just like below.

package com.my.library;

 class Util {
     static {
         System.loadLibrary("thelib");
     }

     public native static String message(String message);
 }



Now remember the structure of our directory here is.

root-folder
|
- jni-folder

Just like that. You can place your root-folder anywhere, just remember where you put it. Save the Android.mk & sample.c inside the jni-folder, and you're ready to go.

Now we will download the NDK in here (NDK), extract it to anywhere you like, and make the ndk-build global, to be able to access it anywhere in your directory. If you're lazy enough to not make it global, you can call it in the terminal by its path.

Now navigate to the root-folder using terminal, then execute "ndk-build" without the double-quotes (or you can call it from its path /path/of/the/ndk/folder/ndk-build, you have to execute this inside root-folder). If successful, your .so is available in the root-folder/libs/armeabi/libthelib.so. Notice the name, libthelib.so? Where we declare only the "thelib" in Android.mk. That's how .so are made, but we will only call the "thelib" name of the .so in our java class.

root-folder
  |
- jni
| |
| - Android.mk
| - sample.c
- libs
| |
| - armeabi
| |
| - libthelib.so
- obj

Copy the libthelib.so to your project, in android studio add it to your /src/main/jniLibs/armeabi/libthelib.so, and call it anywhere in your project, like,

Util.message("This is a redirect message to JNI");


Done!









Thursday, April 30, 2015

Dynamic Admob layout in android to display an ad

I am making a free app that has an ad in it, and I want the ad to be some kind of dynamic, like if there are no ads serve, the layout that wrap the ad will not be visible. It's just like this.


  1. Guidelines of implementing the ad
  2. Make sure that the layout of the ad is set to View.GONE. We will show it only if there is an ad serve.
  3. Add the AdListener
mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdClosed() {
        super.onAdClosed();
    }

    @Override
    public void onAdLoaded() {
        super.onAdLoaded();
        mAdView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAdOpened() {
        super.onAdOpened();
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        super.onAdFailedToLoad(errorCode);
        mAdView.setVisibility(View.GONE);
    }
});
As you see, we only set the method onAdLoaded() to be visible and onAdFailedToLoad to be gone. Because, 
onAdOpened() - This will only happen if the ad will overlay the app after the user taps the ad. onAdClosed() - This will trigger if the overlay of the ad will be closed by the user.

That's it. Thanks.

Friday, March 14, 2014

Restore iPhone without updating

I have an iPhone 4 iOS 6 and I want just to restore to its original state without updating to iOS 7. It's really not a good idea in itunes that it will restore your phone plus you will really have to update to the latest iOS version. Really? And I don't like it that way.

Fortunately, there's an alternative on how to do a restore without updating the iOS version. Just navigate to

Settings > General > Reset > Erase All Content & Settings

And YES! it'll begin restoring your phone.

After restart, just fill out the fields and you're done.

Wednesday, March 12, 2014

Exit Functionality in Android

Usually we want our app to perform an exit function, so I wrote a simple tutorial about it.



Monday, February 24, 2014

Layouting UIScrollView via autolayout in xcode interface builder (iOS)

I am tired of writing code just to get the UIScrollView work. Well lucky for me I got a work around in xcode's interface builder using autolayout. Here's how.