B4A Library App downloaded from Google Play or Amazon Lib

hi,

this is my 3rd lib. with this lib you can check if your app was downloaded from the google play store or the amazon store or from NO store and do what ever you want (like close the app or turn on self destruction :D)

now you can easily catch people that have bought your app, backed-up the apk, uninstalled it and get a refund. so no more working for free :)

note that it is not a 100% protection but at least it will be a little bit harder for people to use your paid apps for free.

the lib is very simple:

Step 1, we intialize the lib (store_check) in activity_create like this:

B4X:
Sub Activity_Create(FirstTime As Boolean)

'Intialize
    Dim check As store_check
    check.Initialize
'......

then we just check if app was downloaded from the store like this:

B4X:
    If check.notfromGoogle Then Log("this app was NOT downloaded from the google play store")  Else Log("check ok or unknow")

you may also do it (if you like) only if "Firsttime = True".

I have not tested it on an amazon app, only on google play app and it worked see attached pictures

Pic1 = installed from my PC
Pic2 = installed from Google Play

If you find any bugs please let me know, and if you like to donate me a small (or big) amount i would be very happy. :)

PS: Please make some tests before you update all your apps, i dont know if there is a minimum SDK or anything else so you use this lib on your own risk :cool:

EDIT:

Version 1.01
Version 1.02 (Bug fixed)

Version 1.03 (after @Erel has explained how to compile a code module it is now working correctly)

New Method: Check any peckagename (app) if it is installed from the Store

B4X:
    If check.notfromGoogle2("com.android.gallery") Then Log("this app was NOT downloaded from the google play store")  Else Log("check ok or unknow")

COMPLETE CODE MODULE: https://www.b4x.com/android/forum/t...e-play-or-amazon-lib.65377/page-2#post-414562

I also want to thank to @JordiCP and to @DonManfred

1.jpg
 

Attachments

  • Example.zip
    1.6 KB · Views: 548
  • Lib v1.03.zip
    4.1 KB · Views: 524
Last edited:

ilan

Expert
Licensed User
Longtime User
maybe something is blocking the app to perform that job (something from your phone)

here everything is working fine

try with a different packagename
 
D

Deleted member 103

Guest
maybe something is blocking the app to perform that job (something from your phone)
The same error at Nexus, HTC and emulator? o_O

Now I have tried with different packagename.
When I enter a wrong packagename then comes this message
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Could not find such Packagename, make sure Packagename is correct
THIEF
** Activity (main) Resume **
and if I enter the correct packagename then comes this message
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
store_check_fromgoogle2 (java line: 145)
java.lang.NullPointerException
at b4a.example.store_check._fromgoogle2(store_check.java:145)
at b4a.example.main._activity_create(main.java:344)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
 
D

Deleted member 103

Guest
Maybe if I had the source code then we could together find the error easily.
 

ilan

Expert
Licensed User
Longtime User
Maybe if I had the source code then we could together find the error easily.

if the error were in the source code then it would not work for me either. i use the same packagename as yours and i dont get any errors.
i dont think it has something to do with the source, maybe some devices dont allow access to that information.

i have not tried on an emulator only on my samsung s5.
will try to run it on an emulator and see

when you try a packagename like from youtube or google chrome do you also get an error?

or when you try check_store.from google (without manual packagename) does it work for you?
 
D

Deleted member 103

Guest
Test from App "fg.CatchTheBall"
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 405)
java.lang.ClassNotFoundException: fg$CatchTheBall$store_check
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:234)
at java.lang.Class.forName(Class.java:181)
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:273)
at anywheresoftware.b4j.object.JavaObject.InitializeStatic(JavaObject.java:74)
at b4a.example.store_check._initialize(store_check.java:187)
at fg.CatchTheBall.main._activity_create(main.java:405)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at fg.CatchTheBall.main.afterFirstLayout(main.java:102)
at fg.CatchTheBall.main.access$000(main.java:17)
at fg.CatchTheBall.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: fg$CatchTheBall$store_check
... 22 more
Caused by: java.lang.ClassNotFoundException: fg$CatchTheBall$store_check in loader dalvik.system.PathClassLoader[/data/app/fg.CatchTheBall-2.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
... 22 more
 
D

Deleted member 103

Guest
Test from App "Speedpilot"
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: fg.speedpilot_lite.autotextsizelabel, trying: fg.speedpilot_pro.autotextsizelabel
Panel size is unknown. Layout may not be loaded correctly.
Main: InitAllVariable
main_activity_create (java line: 528)
java.lang.ClassNotFoundException: fg$speedpilot_pro$store_check
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:234)
at java.lang.Class.forName(Class.java:181)
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:273)
at anywheresoftware.b4j.object.JavaObject.InitializeStatic(JavaObject.java:74)
at b4a.example.store_check._initialize(store_check.java:187)
at fg.speedpilot_pro.main._activity_create(main.java:528)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at fg.speedpilot_pro.main.afterFirstLayout(main.java:102)
at fg.speedpilot_pro.main.access$000(main.java:17)
at fg.speedpilot_pro.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: fg$speedpilot_pro$store_check
... 22 more
Caused by: java.lang.ClassNotFoundException: fg$speedpilot_pro$store_check in loader dalvik.system.PathClassLoader[/data/app/fg.speedpilot_pro-2.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
... 22 more
java.lang.ClassNotFoundException: fg$speedpilot_pro$store_check
 

ilan

Expert
Licensed User
Longtime User
ok this is weird

give me few minutes to make some checks. in the meantime try to remove the lib + xml file from your lib folder and download again the lib from the first post
and install again your app and try.

also click on the IDE right mouse and refresh
 

ilan

Expert
Licensed User
Longtime User
ok filippo i think i got it will upload a new lib and then we can make again the check with your devices

give me about 20 min ok?
 

ilan

Expert
Licensed User
Longtime User
ok i have updated the lib + example please doenload v1.02 and tell if it is now working.

i have change how the result is returning

now you ask if the packagename was NOT downloaded from google play and if true then you should make your action and if false (like before)

false can be 2 cases:

1, it was downloaded from the store
2, check could not be performed

if 2 is the case you will see it also in the logs.

so what you are now checking is if your app was NOT downloaded from the store

B4X:
    If check.notfromGoogle2("com.android.gallery") Then Log("this app was NOT downloaded from the google play store")  Else Log("check ok or unknow")
 
D

Deleted member 103

Guest
Test with Livrary v1.02

1.Test from your example
If check.notfromGoogle2("com.android.gallery") Then Log("this app was NOT downloaded from the google play store") Else Log("check ok or unknow")

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
this app was NOT downloaded from the google play store
** Activity (main) Resume **


2.Test from my App
If check.notfromGoogle Then Log("this app was NOT downloaded from the google play store") Else Log("check ok or unknow") 'tested!

** Activity (main) Pause, UserClosed = false **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 398)
java.lang.ClassNotFoundException: fg$CatchTheBall$store_check
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:234)
at java.lang.Class.forName(Class.java:181)
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:273)
at anywheresoftware.b4j.object.JavaObject.InitializeStatic(JavaObject.java:74)
at b4a.example.store_check._initialize(store_check.java:65)
at fg.CatchTheBall.main._activity_create(main.java:398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at fg.CatchTheBall.main.afterFirstLayout(main.java:102)
at fg.CatchTheBall.main.access$000(main.java:17)
at fg.CatchTheBall.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: fg$CatchTheBall$store_check
... 22 more
Caused by: java.lang.ClassNotFoundException: fg$CatchTheBall$store_check in loader dalvik.system.PathClassLoader[/data/app/fg.CatchTheBall-2.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
... 22 more


3.Test from my App
If check.notfromGoogle2("com.android.gallery") Then Log("this app was NOT downloaded from the google play store") Else Log("check ok or unknow")

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 398)
java.lang.ClassNotFoundException: fg$CatchTheBall$store_check
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:234)
at java.lang.Class.forName(Class.java:181)
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:273)
at anywheresoftware.b4j.object.JavaObject.InitializeStatic(JavaObject.java:74)
at b4a.example.store_check._initialize(store_check.java:65)
at fg.CatchTheBall.main._activity_create(main.java:398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at fg.CatchTheBall.main.afterFirstLayout(main.java:102)
at fg.CatchTheBall.main.access$000(main.java:17)
at fg.CatchTheBall.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: fg$CatchTheBall$store_check
... 22 more
Caused by: java.lang.ClassNotFoundException: fg$CatchTheBall$store_check in loader dalvik.system.PathClassLoader[/data/app/fg.CatchTheBall-2.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
... 22 more
 

ilan

Expert
Licensed User
Longtime User
when you try it from your app can you change your packagename to another to see if it will work

use for example: fg.catchtheball.com

and try check.notfromGoogle and see if you are getting that error again
 
D

Deleted member 103

Guest
Hi ilan,

I now itself programmed me an B4A-routine and it works well. :)
The packagename can uppercase and lower letters have without problems. ;)
 

ilan

Expert
Licensed User
Longtime User
if someone is interested, this is the code module:

B4X:
Sub Class_Globals
    Private store As String
    Dim MyJavaCode As JavaObject
End Sub

Public Sub Initialize ' (Target As JavaObject)
    MyJavaCode.InitializeStatic(Application.PackageName & ".store_check")
End Sub

Public Sub notfromGoogle As Boolean
    store = MyJavaCode.RunMethod("isappfromplaystore", Array(Application.PackageName))
    If store = Null Then
        Log("*** ERROR *** could not perform check")
        Return False
    End If
    If store = "com.android.vending" Then Return False Else Return True
End Sub

Public Sub notfromGoogle2(packagename As String) As Boolean
    Dim list1 As List
    list1.Initialize
   
    Dim p As PackageManager
    list1 = p.GetInstalledPackages
   
    Dim exists As Boolean = False
   
    For i = 0 To list1.Size - 1
        If list1.Get(i) = packagename Then
            exists = True
            Exit
        End If
    Next

    If exists Then
        Dim str As String = MyJavaCode.RunMethod("isappfromplaystore", Array(packagename))
        If str = Null Then
            Log("*** ERROR *** could not perform check")
            Return False
        Else
            If str = "com.android.vending" Then Return False Else Return True
        End If       
    Else
        Log("Could not find such Packagename, make sure Packagename is correct")
        Return False
    End If
End Sub

Public Sub notfromAmazon As Boolean
    store = MyJavaCode.RunMethod("isappfromplaystore", Array(Application.PackageName))   
    If store = Null Then
        Log("*** ERROR *** could not perform check")
        Return False
    End If   
    If store = "com.amazon.venezia" Then Return False Else Return True
End Sub

Public Sub notfromAmazon2(packagename As String) As Boolean
    Dim list1 As List
    list1.Initialize
   
    Dim p As PackageManager
    list1 = p.GetInstalledPackages
   
    Dim exists As Boolean = False
   
    For i = 0 To list1.Size - 1
        If list1.Get(i) = packagename Then
            exists = True
            Exit
        End If
    Next

    If exists Then
        Dim str As String = MyJavaCode.RunMethod("isappfromplaystore", Array(packagename))
        If str = Null Then
            Log("*** ERROR *** could not perform check")
            Return False
        Else
            If str = "com.amazon.venezia" Then Return False Else Return True
        End If           
    Else
        Log("Could not find such Packagename, make sure Packagename is correct")
        Return False
    End If
End Sub

Public Sub GetInstalledPeckageList As List
    Dim p As PackageManager
    p.GetInstalledPackages
    Return p.GetInstalledPackages
End Sub

#if JAVA
import android.content.Context;
public static String isappfromplaystore(String packageName) {
     return BA.applicationContext.getPackageManager().getInstallerPackageName(packageName);
}
#end if
 
D

Deleted member 103

Guest
and here is my solution. ;)

B4X:
Sub IsAppFromGooglePlay(PackageName As String) As Boolean
    Dim Obj1 As Reflector
    Dim installername As String

    Obj1.Target = Obj1.GetContext
    Obj1.Target = Obj1.RunMethod("getPackageManager")

    If PackageName = "" Then
        installername = Obj1.RunMethod2("getInstallerPackageName", GetPackageName, "java.lang.String")
    Else
        Dim lst As List = GetInstalledPackages
        Dim pname As String = PackageName
        For i = 0 To lst.Size - 1
            If PackageName.EqualsIgnoreCase(lst.Get(i)) Then
                pname = lst.Get(i)
                Exit
            End If
        Next
        installername = Obj1.RunMethod2("getInstallerPackageName", pname, "java.lang.String")
    End If

    Log("installername=" & installername)
    If installername = "com.android.vending" Then
        Return True
    Else
        Return False
    End If
End Sub

Sub GetPackageName As String
    Dim r As Reflector
    Return r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
End Sub

Sub GetInstalledPackages As List
    Dim pm As PackageManager
    Return pm.GetInstalledPackages
End Sub
 

ilan

Expert
Licensed User
Longtime User
ok after @Erel has explained to me where my mistake was and how to compile correctly a codemodule to a lib v1.03 should work with any packagename (uppercase/lowercase letters) i have tried it with different devices. so it should work now. thanks to everyone :)
 

Scantech

Well-Known Member
Licensed User
Longtime User
Thanks for this library. This is regarding ".notfromGoogle2"

Can we have a better detection when False (Check Ok or Unknow). It does not make since to have both "Check Ok" or "unknow" when False. If package is not available, the library writes a log "Could not find such Packagename, make sure Packagename is correct" which I like, but it is not traceable within the app and followed by "Check Ok or unknown" assuming it should be "unknow". I like to see a better tracking method when package is not installed. Using the .GetInstalledPeckageList is useless as it takes for ever when you have over 4000 list.
 

ilan

Expert
Licensed User
Longtime User
Thanks for this library. This is regarding ".notfromGoogle2"

Can we have a better detection when False (Check Ok or Unknow). It does not make since to have both "Check Ok" or "unknow" when False. If package is not available, the library writes a log "Could not find such Packagename, make sure Packagename is correct" which I like, but it is not traceable within the app and followed by "Check Ok or unknown" assuming it should be "unknow". I like to see a better tracking method when package is not installed. Using the .GetInstalledPeckageList is useless as it takes for ever when you have over 4000 list.

actually the purpose is just to get a true boolean if package was not installed from the app store.

so if you get that your app was installed from the store OR that package name does not exist then you will get returned a FALSE boolean and you dont want to make any action, correct?

only if the app is installed and the lib recognize that it was not installed from the app store then you get a TRUE beck and you can make your action.

Using the .GetInstalledPeckageList is useless as it takes for ever when you have over 4000 list.

4000 items in an array list is really not much. but to have 4000 installed apps is far to much. so i guess no one will have more then 200 apps installed but anyway you can make your tests and see that a list can handle very easily 4000 items and very quick.
 
Top