Compile and assign libraries in DEBUG mode in Android Java

8

I use the following libraries only if they are available in modo debug

debugCompile 'com.github.brianPlummer:tinydancer:0.0.9'
debugCompile 'com.facebook.stetho:stetho:1.4.1'
debugCompile 'com.uphyca:stetho_realm:2.0.0'

In MainActivity.java what I want to be compiled only if it is in modo debug

if (BuildConfig.DEBUG) {
...
}

The problem comes when I put compiling in release mode that the imports of those libraries, I get an error that can not find them.

import com.codemonkeylabs.fpslibrary.FrameDataCallback;
import com.codemonkeylabs.fpslibrary.TinyDancer;
import com.facebook.stetho.Stetho;
import com.uphyca.stetho_realm.RealmInspectorModulesProvider;

It's obvious why you should not load them, in the gradle it's defined, just load them if you're in debug mode.

I wonder if there is a way to specify in import ...; that only be imported in debug mode?

    
asked by Webserveis 29.11.2016 в 14:14
source

1 answer

2

If you want to solve the issue with dependencies and imports, you can use a Interface set with a factory to avoid having to change code.

An example

Let's imagine that you have the following packages in main :

  • main / model
  • main / views
  • main / controllers
  • main / dao
  • main / api

From the classes of the dependencies you have, you can extract methods in a Interface . for examples of a BBDB Object you need:

package api

public interface IBdConexion{

    public Map<String,Object> getQueryResult(Object[]... data);
    public boolean connect(String host, String user, String password);
    // ademas agregemos un método para ver si la conexión es posible. 
    public boolean puedeConectar();
}

Now you have the possibility to create two variants of a class that implements this interface:

  • debug / dao / MiBdConexion.java
  • release / dao / MiBdConexion.java

In debug you implement a class that has all the dependencies' imports:

package dao

import com.dependencia.uno.*;
import org.dependencia.dos.*;

public class MiBdConexion implements IBdConexion{

    @Override
    public Map<String,Object> getQueryResult(Object[]... data){
         // implementación
    }

    @Override
    public boolean connect(String host, String user, String password){
         // implementación
    }

    @Override
    public boolean puedeConectar(){
         // implementación
    }
}

In release another implementation without the imports:

package dao

public class MiBdConexion implements IBdConexion{

    @Override
    public Map<String,Object> getQueryResult(Object[]... data){
         // implementación
    }

    @Override
    public boolean connect(String host, String user, String password){
         // implementación
    }

    @Override
    public boolean puedeConectar(){
         return false;        }
}

Finally we can implement for example a way to get the implementation we need in main / controllers, without the need to change code in main:

public class MiConexionFactory{

    public static IBdConexion getConexion(){
         IBdConexion con = (IBdConexion) Class.forName("dao.MiBdConexion").newInstance();
         return con;    
    }
}

The getConexion() method will return the class corresponding to the build that implements the interface. So we can access all the methods we need via the interface without having to change the imports in the rest of the code.

    
answered by 28.01.2017 в 21:46