{"id":17176012,"url":"https://github.com/muntashirakon/libadb-android","last_synced_at":"2025-04-13T08:26:18.847Z","repository":{"id":45168757,"uuid":"434799909","full_name":"MuntashirAkon/libadb-android","owner":"MuntashirAkon","description":"ADB library for Android","archived":false,"fork":false,"pushed_at":"2024-04-18T13:09:24.000Z","size":431,"stargazers_count":257,"open_issues_count":3,"forks_count":64,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-10T04:23:37.237Z","etag":null,"topics":["android-adb","android-library"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MuntashirAkon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-12-04T03:48:22.000Z","updated_at":"2025-03-22T12:27:55.000Z","dependencies_parsed_at":"2024-04-13T17:04:57.616Z","dependency_job_id":"3fb7ee2c-257d-4529-b63e-80e17ef7dc5f","html_url":"https://github.com/MuntashirAkon/libadb-android","commit_stats":{"total_commits":38,"total_committers":2,"mean_commits":19.0,"dds":0.02631578947368418,"last_synced_commit":"408a57b1c30aac2bd8580d40b3a27c2c30346491"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MuntashirAkon%2Flibadb-android","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MuntashirAkon%2Flibadb-android/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MuntashirAkon%2Flibadb-android/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MuntashirAkon%2Flibadb-android/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MuntashirAkon","download_url":"https://codeload.github.com/MuntashirAkon/libadb-android/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248682617,"owners_count":21144820,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android-adb","android-library"],"created_at":"2024-10-14T23:58:46.494Z","updated_at":"2025-04-13T08:26:18.825Z","avatar_url":"https://github.com/MuntashirAkon.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LibADB Android\n\nADB library for Android. It enables an app to connect to the ADB daemon (`adbd` process) belonging to the same or a\ndifferent device and execute arbitrary services or commands (via `shell:` service).\n\n**Disclaimer:** This library has never gone through a security audit. Please, proceed with caution if security is\ncrucial for your app. Avoid using the APIs for reasons other than connecting or using ADB. For the safety of your app\nand its users, open a remote service instead of using ADB and ask the user to disconnect Wireless debugging.\n\n## Getting Started\n### Adding Dependencies\nLibADB Android is available via JitPack.\n\n```groovy\n// Top level build file\nrepositories {\n    mavenCentral()\n    maven { url \"https://jitpack.io\" }\n}\n\n// Add to dependencies section\ndependencies {\n    // Add this library\n    implementation 'com.github.MuntashirAkon:libadb-android:1.0.1'\n    \n    // Library to generate X509Certificate. You can also use BouncyCastle for\n    // this. See example for use-case.\n    // implementation 'com.github.MuntashirAkon:sun-security-android:1.1'\n\n    // Bypass hidden API if you want to use the Android default Conscrypt in\n    // Android 9 (Pie) or later. It also requires additional steps. See\n    // https://github.com/LSPosed/AndroidHiddenApiBypass to find out more about\n    // this.\n    // implementation 'org.lsposed.hiddenapibypass:hiddenapibypass:2.0'\n\n    // Use custom Conscrypt library. If you want to connect to a remote ADB\n    // daemon instead of the device the app is currently running or do not want\n    // to bypass hidden API, this is the recommended choice.\n    implementation 'org.conscrypt:conscrypt-android:2.5.2'\n}\n```\n\nIf you're using the custom Conscrypt library in order to connect to a remote ADB daemon and the app targets Android\nversion below 4.4, you have to extend `android.app.Application` to apply fixes for the random number generation:\n```java\npublic class MyAwesomeApp extends Application {\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        // Fix random number generation in Android versions below 4.4.\n        PRNGFixes.apply();\n    }\n\n    @Override\n    protected void attachBaseContext(Context base) {\n        super.attachBaseContext(base);\n        // Uncomment the following line if you want to bypass hidden API as\n        // described above.\n        // if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.P) {\n        //     HiddenApiBypass.addHiddenApiExemptions(\"L\");\n        // }\n    }\n}\n```\n\n**Notice:** Conscrypt supports only API 9 (Gingerbread) or later, meaning you cannot use ADB pairing or any TLSv1.3\nfeatures in API less than 9. The corresponding methods are already annotated properly. So, you don't have to worry about\ncompatibility issues that may arise when your app's minimum SDK is set to one of the unsupported versions.\n\n### Configuring ADB\nInstead of doing everything manually, you can create a concrete implementation of the `AbsAdbConnectionManager` class. \nExample:\n\n```java\npublic class AdbConnectionManager extends AbsAdbConnectionManager {\n    private static AbsAdbConnectionManager INSTANCE;\n\n    public static AbsAdbConnectionManager getInstance() throws Exception {\n        if (INSTANCE == null) {\n            INSTANCE = new AdbConnectionManager();\n        }\n        return INSTANCE;\n    }\n\n    private PrivateKey mPrivateKey;\n    private Certificate mCertificate;\n\n    private AdbConnectionManager() throws Exception {\n        // Set the API version whose `adbd` is running\n        setApi(Build.VERSION.SDK_INT);\n        // TODO: Load private key and certificate (along with public key) from\n        //  some place such as KeyStore or file system.\n        mPrivateKey = ...;\n        mCertificate = ...;\n        if (mPrivateKey == null) {\n            // Generate a new key pair\n            int keySize = 2048;\n            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(\"RSA\");\n            keyPairGenerator.initialize(keySize, SecureRandom.getInstance(\"SHA1PRNG\"));\n            KeyPair generateKeyPair = keyPairGenerator.generateKeyPair();\n            PublicKey publicKey = generateKeyPair.getPublic();\n            mPrivateKey = generateKeyPair.getPrivate();\n            // Generate a certificate\n            // On Android, it requires sun.security-android library as mentioned\n            // above.\n            String subject = \"CN=My Awesome App\";\n            String algorithmName = \"SHA512withRSA\";\n            long expiryDate = System.currentTimeMillis() + 86400000;\n            CertificateExtensions certificateExtensions = new CertificateExtensions();\n            certificateExtensions.set(\"SubjectKeyIdentifier\", new SubjectKeyIdentifierExtension(\n                    new KeyIdentifier(publicKey).getIdentifier()));\n            X500Name x500Name = new X500Name(subject);\n            Date notBefore = new Date();\n            Date notAfter = new Date(expiryDate);\n            certificateExtensions.set(\"PrivateKeyUsage\", new PrivateKeyUsageExtension(notBefore, notAfter));\n            CertificateValidity certificateValidity = new CertificateValidity(notBefore, notAfter);\n            X509CertInfo x509CertInfo = new X509CertInfo();\n            x509CertInfo.set(\"version\", new CertificateVersion(2));\n            x509CertInfo.set(\"serialNumber\", new CertificateSerialNumber(new Random().nextInt() \u0026 Integer.MAX_VALUE));\n            x509CertInfo.set(\"algorithmID\", new CertificateAlgorithmId(AlgorithmId.get(algorithmName)));\n            x509CertInfo.set(\"subject\", new CertificateSubjectName(x500Name));\n            x509CertInfo.set(\"key\", new CertificateX509Key(publicKey));\n            x509CertInfo.set(\"validity\", certificateValidity);\n            x509CertInfo.set(\"issuer\", new CertificateIssuerName(x500Name));\n            x509CertInfo.set(\"extensions\", certificateExtensions);\n            X509CertImpl x509CertImpl = new X509CertImpl(x509CertInfo);\n            x509CertImpl.sign(mPrivateKey, algorithmName);\n            mCertificate = x509CertImpl;\n            // TODO: Store the key pair to some place else.\n        }\n    }\n\n    @NonNull\n    @Override\n    protected PrivateKey getPrivateKey() {\n        return mPrivateKey;\n    }\n\n    @NonNull\n    @Override\n    protected Certificate getCertificate() {\n        return mCertificate;\n    }\n\n    @NonNull\n    @Override\n    protected String getDeviceName() {\n        return \"MyAwesomeApp\";\n    }\n}\n```\n\n### Connecting to ADB\n\nYou can connect to ADB in several ways from the `AbsAdbConnectionManager`:\n\n| Method                          | Description                                                                                                   |\n|---------------------------------|---------------------------------------------------------------------------------------------------------------|\n| `connect(host, port)`           | Connect using a host address and a port number                                                                |\n| `connect(port)`                 | Connect using a host address set by `setHostAddress()` and a port number                                      |\n| `connectTcp(Context, timeout)`  | (SDK 16+) Discover host address and port number automatically for ADB over TCP and connect to it              | \n| `connectTls(Context, timeout)`  | (SDK 16+) Discover host address and port number automatically for TLS (from Android 9) and connect to it      |\n| `autoConnect(Context, timeout)` | (SDK 16+) Discover host address and port number automatically for both ADB over TCP and TLS and connect to it |\n\n### Wireless Debugging\nInternally, ADB over TCP and Wireless Debugging are very similar except Wireless Debugging requires an extra step of\n_pairing_ the device. In order to pair a new device, you can simply invoke `AdbConnectionManager.getInstance().pair(host, port, pairingCode)`.\nAfter the pairing, you can connect to ADB via the usual `connect()` methods without any additional steps.\n\n### Opening ADB Shell for Executing Arbitrary Commands\nSimply use `AdbConnectionManager.getInstance().openStream(\"shell:\")`. This will return an `AdbStream` which can be used\nto read/write to the ADB shell via `AdbStream#openInputStream()` and `AdbStream#openOutputStream()` methods\nrespectively like a normal Java `Process`. While it is possible to read/write in the same thread (first write and then\nread), this is not recommended because the shell might be stuck indefinitely for commands such as `top`.\n\n**NOTE:** If you want to create a full-featured terminal emulator, this approach isn't recommended. Instead, you should\ncreate a remote service via `app_process` or start an SSH server and connect to it.\n\n### Other services\nYou can also use other services via the `AdbConnectionManager#openStream()` methods. See [SERVICES.md](./SERVICES.md)\nfor more information.\n\n## For Java (non-Android) Projects\nIt is possible to modify this library to work on non-Android project. But it isn't supported because Spake2-Java only\nprovides stable releases for Android. However, you can incorporate this library in your project by manually compiling\nSpake2 library for your platforms.\n\n## Contributing\nBy contributing to this project, you permit your work to be released under the terms of GNU General Public License, \nVersion 3 or later **or** Apache License, Version 2.0.\n\n## License\nCopyright 2021 \u0026copy; Muntashir Al-Islam\n\nDual licensed under the terms of [GPL-3.0-or-later](https://www.gnu.org/licenses/gpl-3.0.html) or\n[Apache-2.0 license](https://www.apache.org/licenses/LICENSE-2.0.html). Use whatever license you need for your project.\n\n_Note regarding the Apache-2.0 license, this library has an LGPL dependency which may go against the policy of some\norganizations such as ASF._\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuntashirakon%2Flibadb-android","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmuntashirakon%2Flibadb-android","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuntashirakon%2Flibadb-android/lists"}