Android Tutorial Protect your Android application with the Licensing library

johnaaronrose

Active Member
Licensed User
Longtime User
You should only test the value of test_1 in (or after) lc_Allow.

I have done that with the same result. It's very awkward for me to check the log as B4A will not connect properly (either by wireless or bluetooth) to it so that I can see my phone's log on my PC. So I compile & run in order to generate the .apk on my PC. I then connect my PC & phone by USB cable, copy the .apk to the phone's sdcard, disconnect the USB cable, and install the app using the Easy Installer app (from Google Play). However, I'm able to view the log (after running my app) by using the Catlog app (from Google Play). The results are interesting:
CheckLicense sub is called,
neither lc_Allow, lc_DontAllow or lc_Error is called,
other Activity_Create code is executed,
'No hacking allowed' is logged,
Window Manager error: main has leaked window... PhoneWindow$DecorView...

Code extracts:
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
 'These variables can be accessed from all modules.
  Private publicKey As String
  publicKey = "MIIBIjA...
  Private test_1 As String
  test_1 = ""
  Private secret As String
  secret = "abcd"

Sub Activity_Create(FirstTime As Boolean)
  Dim ph As Phone
  Log("PhoneModel="&ph.Model)
  If ph.Model = "sdk" Then
    'emulator -> jump to Allow
    AllowEmulator
  Else
    'check license
    CheckLicense
  End If

Sub AllowEmulator
  Log("Allow Emulator")
   test_1 = secret
End Sub

Sub CheckLicense
   Log("CheckLicense")
  Dim lc As LicenseChecker
  Dim pi As PhoneId
  lc.Initialize("lc", pi.GetDeviceId, publicKey, "malach".GetBytes("UTF8"))
  lc.SetVariableAndValue("test_1", secret)
  Log("test_1="&test_1)
   ToastMessageShow("test_1="&test_1, True)
   Log("lc.CheckAccess")
   lc.CheckAccess
End Sub

Sub lc_Allow
  Log("Allow")
   Log("test_1="&test_1)
   ToastMessageShow("test_1="&test_1, True)
   If test_1 <> secret Then
      Log("No hacking allowed")
      Log("test_1="&test_1)
     ToastMessageShow("No hacking allowed: Closing application.", True)
     Activity.Finish
   End If
End Sub

Sub lc_DontAllow
  Log("DontAllow")
  ToastMessageShow("License check fails: Closing application.", True)
  Activity.Finish
End Sub

Sub lc_Error (ErrorCode As String)
  Log("License Check Error: " & ErrorCode)
  ToastMessageShow("License check error: Closing application.", True)
  Activity.Finish
End Sub

PS I have looked at the thread referred to in message #70 on this thread. The messages posted after #1 have confused the solution: should the code in #13 on that thread be used rather than the code in #1 or....?
 
Last edited:

johnaaronrose

Active Member
Licensed User
Longtime User
Apologies

Why don't you use USB debugging?

If the "no hacking message" was logged then lc_Allow was called.

I don't use USB debugging because when I connect my phone to my PC using a USB cable, B4A compile & run does not find the phone and states that no device is available. When I 'Turn on USB Storage' on the phone, I can see (on my PC) the phone's sdcard contents but B4A compile & run does not find the phone as before. Therefore, the USB connection does work but the problem is either the phone (probably Cyanogen Mod 7.2 - I've tried Googling & the Modaco Forum but with no success) or B4A: I don't know how to determine which is the problem.

Forgive my error in saying that lc_Allow was not called. It was called. But the problem is that test_1 is set to "".
 

johnaaronrose

Active Member
Licensed User
Longtime User
Log messages

Are you sure that you are reading the correct log message and not the one before lc.checkAccess ?

I think that what's confusing me is that I didn't realise till just now that Log messages are not being displayed by the CatLog app on my phone. Thus, the only message that I'm seeing in addition to PackageManager & WindowsManager ones is the one produced by a ToastMessage command in lc_Allow. I think that what's happening is that the lc.Initialize command is executed and then the lc_Allow sub is being invoked without executing the rest of the CheckLicense sub. Because if that were not so, the SetVariableAndValue command would cause test_1 to be set.

I'll check this out tomorrow by inserting more ToastMessageShow commands in the code: not able to do it this evening due to prior commitment.
 

johnaaronrose

Active Member
Licensed User
Longtime User

It's now working OK. What I did was to: kill my app (and all others running) using Advanced Task Killer app, use Easy Uninstaller app to uninstall my app, use Easy Installer app to install my app. I had Log, MsgBox & ToastMessageShow for various displays so that I couldn't miss them as the app executed. The app went through CheckLicense & lc_Allow and showed test_1 as my expected value i.e. all as expected. I'm baffled as to the problem's cause: all I can think of is that Easy Uninstaller app didn't fully clear out the app or that Google Play website is flaky. I have a question about Google Play: is the procedure for 'Publish' a new version to 'Unpublish' the old version before publishing the new version? I'll start a new thread if you think that I should, Erel.
 

MrKim

Well-Known Member
Licensed User
Longtime User
No. The result is cached locally.


What do you mean with routing list?

Cached for how long? I am having a devil of a time testing my app. Changing the "Licensed Test Response" in the Developer Console is giving me results that I can only describe as erratic. Sometimes the app will respond immediately, sometimes wrong, sometimes after 5 or 10 minutes.

And I am basically at this point just using your sample code from the Licensing Library. Clearing user data and reinstalling the app sometimes responds, sometimes not.

Will having the app on more than one device with the same account (phone and emulator) have an effect?

Kim
 
Last edited:

MrKim

Well-Known Member
Licensed User
Longtime User
Where are the extras?

I don't see any way to get at the Server Response Extras VT, GT, GR, UT. Am I missing something?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Apparently some elements or files that come with my app are not automatically overwritten when B4A is put back to normal release mode (without obfuscation).
If you have an explanation for this that would be very much appreciated.
I never encountered such an issue. If you are able to reproduce it then please start a new thread in the bugs forum.
 

Armoured

Member
Licensed User
Longtime User
Hi Erel,
With this library we must modify the manifest of the application?
If the answer is "Yes" how?

Thanks
 

johnaaronrose

Active Member
Licensed User
Longtime User
Phone & tablet

My app is free. However, I'm using Erel's code as in post #1: so it's difficult for someone else to copy my app's code. I'm not using the enhancement described in thread Android Device Unique ID - Alternative to PhoneId: mainly because I don't understand it. My app downloads & works OK on both my phone & my tablet. On my phone (using Gingerbread), I can move the app to an SD card & it works OK. On my tablet (using Ice Cream Sandwich), if I move the app to to an SD card, it fails with 'some secret value' not matching. Can anybody explain why?
 

johnaaronrose

Active Member
Licensed User
Longtime User
Now works from SD card on tablet!

Now works from SD card on tablet!

Code extracts for checking value:

B4X:
Public secret As String
   secret = "abcd"

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim ph As Phone
  Log("PhoneModel="&ph.Model)
   If ph.Model = "sdk" Then
    'emulator -> jump to Allow
    AllowEmulator
  Else
    'check license
    CheckLicense
  End If

B4X:
Sub AllowEmulator
  Log("Allow Emulator")
   test_1 = secret
End Sub

Sub CheckLicense
   Log("CheckLicense")
   Dim lc As LicenseChecker
  Dim pi As PhoneId
  lc.Initialize("lc", pi.GetDeviceId, publicKey, "abcdef".GetBytes("UTF8"))
  lc.SetVariableAndValue("test_1", secret)
  Log("test_1="&test_1)
   Log("lc.CheckAccess")
   lc.CheckAccess
End Sub

Sub lc_Allow
  Log("Allow")
   If test_1 <> secret Then
      Log("No hacking allowed")
      Log("test_1="&test_1)
      Msgbox("No hacking allowed.", "Closing application")   
      ExitApplication
   End If
End Sub

Sub lc_DontAllow
  Log("DontAllow")
   Msgbox("License check fails.", "Closing application")
  Activity.Finish
End Sub

Sub lc_Error (ErrorCode As String)
  Log("License Check Error: " & ErrorCode)
   Msgbox("License check error.", "Closing application")
  Activity.Finish
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…