Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running different App class in my application (for debug)

Tags:

android

gradle

I want to resolve some dependencies in my application only if I debug my app, for example I want to use Stetho only for debug, not in final release, how can I achieve that?

I tried creating another folder called debug and creating DebugApp that extends from my App but I don't know how to run this DebugApp, maybe I should add something in Gradle?

public class DebugApp extends App {
    @Override
    protected void init() {
        super.init();
        DebugAppInitializer.initStetho(this);
        DebugAppInitializer.initCrashczyk(this);
    }
}

It would be awesome if I can link it to my productFlavors

like image 419
henorek Avatar asked Feb 26 '16 13:02

henorek


People also ask

How do I debug an APK file?

To start debugging an APK, click Profile or debug APK from the Android Studio Welcome screen. Or, if you already have a project open, click File > Profile or Debug APK from the menu bar. In the next dialog window, select the APK you want to import into Android Studio and click OK.

How do I run a project in debug mode?

Set the project as the main project and choose Debug > Debug Main Project (Ctrl+F5) from the main menu or right-click the project in the Projects window and choose Debug.

What are Debuggable applications?

Android allows the attribute android:debuggable to be set to true in the manifest, so that the app can be debugged. By default this attribute is disabled, i.e., it is set to false , but it may be set to true to help with debugging during development of the app.


2 Answers

You can put your code like this:

if(BuildConfig.DEBUG){
... //debug init
} else {
....//release init
}

OR

1) Create project structure like this:

enter image description here

2) Gradle flavors:

 productFlavors {
    driver {
        applicationId "android.com.driver"
        versionName "1.0"
        buildConfigField "Boolean", "DRIVER_SESSION", "true"
        minSdkVersion 16
        targetSdkVersion 23
    }
    passenger {
        applicationId "android.com.passenger"
        versionName "1.0"
        buildConfigField "Boolean", "DRIVER_SESSION", "false"
        minSdkVersion 16
        targetSdkVersion 23
    }
}
sourceSets {
    passenger {
        manifest.srcFile 'src/passenger/AndroidManifest.xml'
    }
    driver {
        manifest.srcFile 'src/driver/AndroidManifest.xml'
    }
}

3) Need to create differents classes and put application name for each manifest files:

manifest in driver package ->

 <application
        android:name=".application.YourFirstAppInDriverPackage"
        ...
        >

manifest in passenger package->

 <application
        android:name=".application.YourSecondAppInPassengerPackage"
        ...
        >

4) Switch development mode between two projects:

enter image description here

like image 178
Leonid Veremchuk Avatar answered Oct 26 '22 23:10

Leonid Veremchuk


I believe you're looking for this: https://www.littlerobots.nl/blog/stetho-for-android-debug-builds-only/

TL;DR: In order to activate your DebugApp you'd need to override the manifest in your debug folder as follows:

 <manifest
    package="com.mycompany"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        tools:replace="android:name"
        android:name=".DebugApp"/>

</manifest>
like image 45
Ghedeon Avatar answered Oct 26 '22 22:10

Ghedeon