DaGeRe / kieker-source-instrumentation-dagere

Temporary fork of source instrumentation, used until original repository can deploy to maven central
Apache License 2.0
0 stars 3 forks source link

Add a flag like `--strictModePermitAll` to set `StrictMode.ThreadPolicy` to permit network operations on the main thread before kieker initialization for Android projects #2

Closed alex-iotiq closed 1 year ago

alex-iotiq commented 1 year ago

On Android network operations aren't allowed on the main thread by default.

While initializing kieker.monitoring.core.controller.MonitoringController the method java.net.InetAddress.getLocalHost() is used which causes NetworkOnMainThreadException and crashes the process.

This limitation can by turned off by setting ThreadPolicy accordingly before the kieker initialization in every class the class which extends from Application. It will per. It will permit network operations on the main thread. See Stack Overflow

public class MyClass extends Application {

// Set "before kieker.monitoring.core.controller.MonitoringController.getInstance()"
static {
    android.os.StrictMode.ThreadPolicy policy = new android.os.StrictMode.ThreadPolicy.Builder().permitAll().build();
    android.os.StrictMode.setThreadPolicy(policy);
}
...
}
DaGeRe commented 1 year ago

Thanks for the issue. I've added a first implementation of this issue: https://github.com/DaGeRe/kieker-source-instrumentation-dagere/commit/ac5f749bbd6b6143462f27b61db107b10a9a8bd5 Does this work for you? (You can test this in Peass if you install the current version or the source-instrumentation by running mvn clean install inside of it and afterwards change the dependency of kieker-source-instrumentation-library in dependency/pom.xml:

    <dependency>
      <groupId>de.dagere.kieker</groupId>
      <artifactId>kieker-source-instrumentation-library</artifactId>
      <version>1.15.16-SNAPSHOT</version>
    </dependency>
alex-iotiq commented 1 year ago

Thanks for this implementation.

Unfortunately it adds the static initializer after kieker.monitoring.core.controller.MonitoringController.getInstance() causing the same NetworkOnMainThreadException.

I've created a fix for this https://github.com/DaGeRe/kieker-source-instrumentation-dagere/pull/3.

DaGeRe commented 1 year ago

Thanks for the PR! Its now part of the branch (by rebase). Does this work for you now?

alex-iotiq commented 1 year ago

Yes thank you.