0

I'm using the following java code to check my user's hosts files to see if they blocked ads in my app (thus, I can pop up a plead window explaining why I need the ad money, etc):

public boolean IsAdBlocked()
{
    BufferedReader in = null;

    try
    {
        in = new BufferedReader(new InputStreamReader(new FileInputStream("/etc/hosts")));
        String aLine;
        while ((aLine = in.readLine()) != null) if (aLine.contains("admob")) return true;
    }
    catch (IOException e)
    {
    }
    return false;
}

I call it from C++ with the following function:

bool JavaBridge_IsAdBlocked()
{

JNIEnv* threadEnv;
g_theJavaVirtualMachine->GetEnv ((void **) &threadEnv, JNI_VERSION_1_4);

jclass aObject = threadEnv->FindClass(ACTIVITY_NAME);
if(aObject==NULL) {return true;}
jmethodID aFunction = threadEnv->GetMethodID(aObject, "IsAdBlocked", "()Z");
if(aFunction==NULL) {return true;}

jboolean aBool=threadEnv->CallBooleanMethod(g_theGlobalRefToActivityInstance,aFunction);
threadEnv->DeleteLocalRef(aObject);

return (aBool!=0);
}

When the app crashes, I get the following stack trace:

#01 pc 00000000000484b3 /system/lib/libc.so (pthread_kill+34)
#02 pc 000000000001dd89 /system/lib/libc.so (raise+10)
#03 pc 0000000000019511 /system/lib/libc.so (__libc_android_abort+34)
#04 pc 0000000000017150 /system/lib/libc.so (abort+4)
#05 pc 000000000031ba35 /system/lib/libart.so (_ZN3art7Runtime5AbortEv+252)
#06 pc 00000000000b4ccb /system/lib/libart.so (_ZN3art10LogMessageD2Ev+866)
#07 pc 00000000001bc861 /system/lib/libart.so (_ZN3art22IndirectReferenceTable17AbortIfNoCheckJNIEv+84)
#08 pc 000000000023db6f /system/lib/libart.so (_ZNK3art22IndirectReferenceTable10GetCheckedEPv+498)
#09 pc 000000000023ad1f /system/lib/libart.so (_ZN3art9JavaVMExt12DecodeGlobalEPv+10)
#10 pc 0000000000335785 /system/lib/libart.so (_ZNK3art6Thread13DecodeJObjectEP8_jobject+124)
#11 pc 000000000031789f /system/lib/libart.so (_ZN3art35InvokeVirtualOrInterfaceWithVarArgsERKNS_33ScopedObjectAccessAlreadyRunnableEP8_jobjectP10_jmethodIDSt9__va_list+42)

Can anyone explain what circumstances cause "ScopedObjectAccessAlreadyRunnable" to spiral away into total destruction?

Mark
  • 9,604
  • 5
  • 36
  • 64
Raptisoft
  • 203
  • 1
  • 10
  • They can disable the WiFi altogether so... there has to be a better way to do this. Have you looked thoroughly at the Admob API? You shouldn't even need to write native code for such a check. Another possibility is to just try to download some page on the admob server. If you can't, you know you are either blocked or there's no internet connection. In any case they won't see new ads. Finally, there's the option of ignoring these edge cases all together. How much can them be? a 1% of your user base? – Fran Marzoa Mar 17 '18 at 00:31
  • 1
    PS. If you are proficient with native code, you could probably just ping the admob server, but on the other hand it can be redirected with a custom DNS server. Probably the safest way is to try to open a page on such server using SSL ("https://admob.com/whatever", you know). – Fran Marzoa Mar 17 '18 at 00:34
  • 1
    Why is the C++ code using JNI to call Java code to check the file, instead of just opening the `hosts` file directly? – Remy Lebeau Mar 17 '18 at 00:56

2 Answers2

0

While the comments that criticize your approach are very reasonable, the most likely cause of the crash is that your JNI runs on a native thread that has not been attached to JVM.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

You are never closing the input stream.

greenapps
  • 11,154
  • 2
  • 16
  • 19