Search code examples
androidproguardfirebase-mlkit

Firebase ML Kit proguard issue


I am working on a project that uses custom models of Firebase ML Kit, and run into a proguard issue with the code below. I didn't find any proguard rules on the official website nor the github repo, unfortunately.

If I added -dontobfuscate, the following code works. But I want my project to be obfuscated.

import com.google.android.gms.tasks.Task
import com.google.android.gms.tasks.Tasks
import com.google.firebase.ml.common.FirebaseMLException
import com.google.firebase.ml.common.modeldownload.FirebaseLocalModel
import com.google.firebase.ml.common.modeldownload.FirebaseModelManager
import com.google.firebase.ml.custom.FirebaseModelDataType
import com.google.firebase.ml.custom.FirebaseModelInputOutputOptions
import com.google.firebase.ml.custom.FirebaseModelInputs
import com.google.firebase.ml.custom.FirebaseModelInterpreter
import com.google.firebase.ml.custom.FirebaseModelOptions

fun init(context: Context) {
    val modelOptions = FirebaseModelOptions.Builder()
        .setLocalModelName("mobilenet_v2_1.0_224_quant")
        .build()

    val localModelSource = FirebaseLocalModel.Builder(MODEL_NAME).setAssetFilePath("mobilenet_v2_1.0_224_quant.tflite").build()

    val firebaseModelManager = FirebaseModelManager.getInstance().apply {
        registerLocalModel(localModelSource)
    }

    interpreter = FirebaseModelInterpreter.getInstance(modelOptions)
    labelList = loadLabelList(context.applicationContext)

    val inputDims = intArrayOf(DIM_BATCH_SIZE, DIM_IMG_SIZE, DIM_IMG_SIZE, DIM_PIXEL_SIZE)
    val outputDims = intArrayOf(1, labelList.size)

    val dataType = FirebaseModelDataType.BYTE

    dataOptions = FirebaseModelInputOutputOptions.Builder()
        .setInputFormat(0, dataType, inputDims)
        .setOutputFormat(0, dataType, outputDims)
        .build()
}

The crash is:

java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String r.c.a.e.b() (tried Java_r_c_a_e_b and Java_r_c_a_e_b__)
    at r.c.a.e.b(Native Method)
    at com.google.android.gms.internal.firebase_ml.zzpe.<clinit>(SourceFile:1)
    at com.google.firebase.ml.custom.FirebaseModelInterpreter.<init>(SourceFile:16)
    at com.google.firebase.ml.custom.FirebaseModelInterpreter.zza(SourceFile:6)
    at com.google.firebase.ml.custom.FirebaseModelInterpreter.getInstance(SourceFile:1)

Here are the rules in my proguard.txt

-keepnames class com.google.firebase.** { *; }
-keepnames class com.google.android.gms.** { *; }

Any guideline/suggestions are appreciated!


Solution

  • This is interesting, since FirebaseModelInterpreter itself does not contain any native code.

    Could you try to keep the native methods and see whether you still see the crash?

    -keepclasseswithmembernames class * { native <methods>; }