Android Question code module work, when compile to a lib NOT??

ilan

Expert
Licensed User
Longtime User
hi

when i use my code module from activity main everything works fine (almost)
but when i compile it to a lib then it wont work.

i will explain first what the code module does. it is checking what installer has installed an app (by his packagename)

and when my packagename (the packagename that is running the code module) is something different then "b4a.example" it wont work (after i compile the code module to a lib) but when i try it with the code module it is working

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

now try to use it in an example where the packagename is "b4a.example"
and then change it to "anything.com"

it will work, but then when you compile it to a lib it will not work anymore ONLY with packagename "b4a.example"

what am i doing wrong?
 
Last edited:

HotShoe

Well-Known Member
Licensed User
Longtime User
I don't know the answer to your question, but I did take the liberty of creating a java lib to check if the app currently running is from an app store or not. It is attached and includes a test project and the Eclipse source code project.
 

Attachments

  • checkstore.zip
    6.7 KB · Views: 123
Upvote 0

ilan

Expert
Licensed User
Longtime User
Add some logs messages to see where the package name is incorrect.

I have erel and i even check in a loop if such package name exists so the problem is not the asked package name the problem is in the packagename of the running app.

(build configuration 》packagename)

If it is the default b4a.example then it is working but if i change it to any other packagename it is not working.

But that behavior is ONLY after i compile to lib (the code module) and try to run the code with the lib but running straight the code module is working.

The error msg is that the class was not found

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)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
This the error filippo get when he tried it.

(i did get that error too)

** 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

You can the discussion about the problem here: https://www.b4x.com/android/forum/threads/app-downloaded-from-google-play-or-amazon-lib.65377/
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
When i use lowercase letters for the packagename i get that error too.
Only b4a.example as packagename is working
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
:oops::oops::oops::oops:

Ok, thank you erel, i understand now how it works.

When i compile the library i need to use the same packagename i used for compile.

And it will also work when the packagename of the running app will change?

So no need to intialize the javaobject with apllication.packagename?
 
Upvote 0
Top