Andrea Grippi
Andrea Grippi

Reputation: 1589

Adding dependencies to platform specific code in Flutter

I'm trying to implement the code from answer #1 from this thread in Flutter.

The "invokeMethod" is correctly set and the Android gets correctly called. When I try to add the specific code that should come from the library "com.google.zxing", I don't understand how to import it.

I tried putting it into the Android gradle, but the zxing class methods remain "red" in MainActivity, as the library doesn't get imported.

Maybe there is a specific procedure to import external dependencies?

Upvotes: 0

Views: 922

Answers (2)

ABDERRAHMANE OUALI
ABDERRAHMANE OUALI

Reputation: 931

if you're write a flutter plugin and want to add dependency to your android side you have to go to your build.gradle file and add dependencies block below the android block


android {

    compileSdkVersion 31

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion 16
    }
}

// you're dependencies block are here and put you're dependency inside it like you would in any normal android project

dependencies {
    // your dependency here
    implementation 'org.jsoup:jsoup:1.15.1'

}

Upvotes: 0

Andrea Grippi
Andrea Grippi

Reputation: 1589

Apparently I had to import it this way in the class:

 import com.google.zxing.*;
 import com.google.zxing.common.*;

As the object "HybridBinarizer" is defined in the second one, and couldn't be retrieved otherwise.

As of the gradle, I had to add the dependency this way:

dependencies {

    classpath 'com.google.zxing:core:3.3.0'
}

in the file project/android/build.gradle, similar to the traditional Android way but using classpath instead of compile.

Another thing, a method of the zxing library (i.e. Result) was giving an error since the Result class was already defined in another library (io.flutter.plugin.common.MethodChannel.Result).

I solved this problem by creating a separate Java class and simply putting all the zxing methods there.

Upvotes: 2

Related Questions