dadb icon indicating copy to clipboard operation
dadb copied to clipboard

Discover returns an error

Open victorjaviermartin opened this issue 2 years ago • 10 comments

Hi guys,

I'm Victor, and I'm trying to run the library in a poc application to know how to use it. But the first method that I'm trying is "discover", but it returning me a FileNotFoundException

I'm doing:

fun discover(){
        try {
            val result = Dadb.discover("localhost")
            binding.tvcommandline.text = "" + result.toString()
        }catch (ex: java.lang.RuntimeException){
            binding.tvcommandline.text = "Failed to discover emulator. No adb device found"
            ex.printStackTrace()
        }
    }

The error is:

Caused by: java.io.FileNotFoundException: .android/adbkey: open failed: ENOENT (No such file or directory)

Some advice?

I'm testing on a Samsung S20 Ultra with Android 12 and connected via USB.

Thank you

victorjaviermartin avatar Oct 07 '22 11:10 victorjaviermartin

Dadb doesn't create the keypair automatically on Android device. You have to create those keys manually and use them in the discover method.

This is a code sample to create them in your application's cache directory.

class YourViewModel(application: Application) : AndroidViewModel(application) {

    val context by lazy { getApplication<Application>().applicationContext }

    suspend fun keygen(refresh: Boolean = false): AdbKeyPair = withContext(IO) {
        val tempdir = context.cacheDir
        val pvtFile = File(tempdir, "adbkey")
        val pubFile = File(tempdir, "adbkey.pub")
        if (refresh || !pubFile.exists() || !pvtFile.exists()) {
            if (pubFile.exists()) pubFile.delete()
            if (pvtFile.exists()) pvtFile.delete()
            AdbKeyPair.generate(pvtFile, pubFile)
        }
        AdbKeyPair.read(pvtFile, pubFile)
    }
    
    suspend fun onButtonClicked() = viewModelScope.launch {
        val handler = Dadb.discover("localhost", keygen())
    }        
}        

sharpordie avatar Oct 08 '22 06:10 sharpordie

Hi @sharpordie with those lines, discover returns me a "null"

victorjaviermartin avatar Oct 13 '22 07:10 victorjaviermartin

@victorjaviermartin Can you please paste the whole stack trace?

@sharpordie Dadb does actually generate the adb key automatically now: https://github.com/mobile-dev-inc/dadb/blob/42a0125c47b196418fffdf4cdf6fdc04935e523e/dadb/src/main/kotlin/dadb/AdbKeyPair.kt#L77

Leland-Takamine avatar Oct 23 '22 17:10 Leland-Takamine

@Leland-Takamine Victor asked for an Android solution. The HOME environment variable doesn't exist by default on Android. So my code uses the current application's cache directory.

Unfortunately I don't use dadb anymore on Android. I will retry my code this week, I am sure it worked properly.

@victorjaviermartin Did you allow the adb connection on your target device?

sharpordie avatar Oct 24 '22 15:10 sharpordie

Yes @sharpordie. I need to test again to take the full stacktrace, but I don't have time at the moment. Sorry.

victorjaviermartin avatar Oct 27 '22 06:10 victorjaviermartin

@victorjaviermartin Sorry for the long waiting time.

Here is a working project using an old version (0.0.11) of Dadb: https://github.com/sharpordie/hisendal

I quickly tested with version 1.2.4, but it doesn't work unfortunately. I hope this will be helpful to you anyway.

sharpordie avatar Oct 30 '22 11:10 sharpordie

@victorjaviermartin Sorry for the long waiting time.

Here is a working project using an old version (0.0.11) of Dadb: https://github.com/sharpordie/hisendal

I quickly tested with version 1.2.4, but it doesn't work unfortunately. I hope this will be helpful to you anyway.

Thank you, I will take a look

victorjaviermartin avatar Nov 03 '22 15:11 victorjaviermartin

@victorjaviermartin Well, this should work fine with the latest version of Dadb. https://github.com/sharpordie/hisendal

It's still using the ugly GlobalScope.async. This is used to throw an exception if the target device has not accepted the connection yet and prevents it from blocking endlessly.

Please tell me if it works with your devices.

I'm not working with Dadb anymore for the moment, my initial goal was to use Dadb on Android and iOS through Kotlin Multiplatform and use it in my Flutter projects with Klutter. But that's way too much for my narrow brain. The fact that Dadb doesn't allow to pair with Android 11+ devices is also a serious problem.

sharpordie avatar Nov 21 '22 13:11 sharpordie

Thank you @sharpordie but unfortunately this is obsolete from android 11+. Thank you for your effort.

victorjaviermartin avatar Nov 23 '22 09:11 victorjaviermartin

I guess copying adbkey files from an authorized machine with the "pair" command to hisendal's cache folder doesn't work. That would be too straightforward? Or I wouldn't understand Google's action to make the connection so painful.

Damn, I'll have to find an Android 11+ emulator with bridged network capability to test.

sharpordie avatar Nov 23 '22 14:11 sharpordie