Reputation: 125
I'm building a mobile app for android and ios using ionic and cordova. I'm working on QA for it now and I want to use strictmode for the android emulator for the app, but I can't seem to find any documentation for how I turn it on with ionic.
Is it even possible?
Upvotes: 2
Views: 1005
Reputation: 649
Framework: Cordova v. 8.0.0
4/2019: At the time of this writing, no directive from Cordova nor any plugin available to enable Android StrictMode Testing (Requirement PS-P2 and PM-1). Henceforth, to enable StrictMode, I chose to manually modify our app MainActivity.java.
(1) MainActivity.java
This is your default main Android activity that functions as entry point to launch your android app. Cordova auto-generate this java file based on definition in your app's default Manifest XML, which is also auto-generated by Cordova. By default, one should not modify this file because Cordova may delete/regenerate it as necessary. But here, we MUST!
Find this file for your app: [app-cordova-project-dir]\platforms\android\app\src\main\java[app-package-dir]\MainActivity.java
Inside MainActivity.java, find its main method: onCreate(), and add the following code to enable StrictMode. This must include all all necessary import statements. Do not skip the log statement such that you can monitor and prove your changes is ON. See REF below for Android doc detail.
import android.os.StrictMode;
import android.util.Log;
public class MainActivity extends CordovaActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
//CODE-ADDED - Android StrictMode enabled
Log.d("[MYAPPNAME]", "[MYAPPNAME].MainAtivity.onCreate() - STARTING. My StrictMode code is now ENABLED!");
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.detectDiskReads()
.detectDiskWrites()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.build());
//CODE-ADDED - End
//The rest is Cordova default auto-generated syntax, leave it ALONE!
super.onCreate(savedInstanceState);
... blableblo
}
}
Copy-paste your entire MainActivity code onto temporary text file (notepad, etc.) so just in case Cordova delete/regenerate this file, you have a backup code to refer to, and copy-paste again.
(2) R.java
This is Cordova autogenerate file by its build script. By default, one should not modify this file because Cordova may delete/regenerate it as necessary. But here, we MUST delete it manually. If you did not delete this R file, any changes you did on MainActivity will be ignored by Cordova build script.
Find this file for your app: [app-cordova-project-dir]\platforms\android\app\build\generated\source\r\debug[app-package-dir]\R.java
Delete this file!
(3) Execute your Cordova build script to recompile your app APK and make sure no compile error. This should regenerate the R.java file, too.
(4) Deploy/install your app to your target Emulator/Device, and test your app as you would for any StrictMode violations, and make proper fixes on your app code.
(5) Once all done and successful, you must REMOVE the StrictMode code from your MainActivity.java file. Do not let StrictMode enabled for your production.
REF: Android StrictMode: https://developer.android.com/reference/android/os/StrictMode.html
Upvotes: 0