Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Test APK Generation without Touching Gradle Files #4231

Open
noomorph opened this issue Oct 22, 2023 · 3 comments
Open

Dynamic Test APK Generation without Touching Gradle Files #4231

noomorph opened this issue Oct 22, 2023 · 3 comments

Comments

@noomorph
Copy link
Collaborator

Describe your idea

Background

  • Many Detox users are primarily from the JavaScript community and face challenges integrating Detox into their Android projects because of Gradle intricacies.
  • These challenges have resulted in a category of issues where users require assistance to properly set up their projects.
  • To address this, we propose a novel way of creating the test APK without interacting with Gradle, effectively reducing the integration friction.

Proposal

  1. Precompiled Test APK Template:

    • Have a precompiled "template" test APK that contains everything needed to run the tests: JUnit test class, optional Test Butler linking, etc.
    • This APK would have placeholders for package names (main activity, etc) and any other variables that might change between projects.
  2. Dynamic Injection of Main Activity:

    • Using tools such as Smali, Baksmali, and Apktool, dynamically disassemble the template APK.
    • Rename the package name so the instrumentation runner can find the main activity under test, e.g. com.placeholder.MainActivitycom.companyname.MainActivity
    • Adjust package references and paths to match those of the app under test.
    • Reassemble into a new APK.

Advantages

  • Simplifies the integration process for users not familiar with Gradle.
  • Reduces the number of issues related to Gradle setup.
  • Makes the process more streamlined and intuitive.

Considerations

  • While the concept seems feasible, it requires careful and detailed implementation to ensure robustness.
  • APK manipulation at this level is more low-level and error-prone. It's crucial to ensure stability and cover edge cases.
  • Testing is paramount. Given that we're manipulating bytecode directly, thorough testing is required to avoid runtime issues.

Proof of concept

  • A proof of concept (POC) should be developed to gauge the feasibility and challenges of this approach.
  • It's estimated that an initial POC could be completed by a skilled developer in approximately a week.
  • Developers with Android experience, such as @mikehardy, @valentynberehovyi, and anyone who's reading this now could provide valuable insights and guidance, or even join the effort.

Conclusion

This approach has the potential to greatly simplify the integration process of Detox on Android, enhancing the user experience and reducing common Gradle-related issues. If successfully implemented, it can be a significant improvement to the Android integration of Detox.

@github-actions
Copy link

Hello! We appreciate you bringing this issue to our attention.
It looks like this could be a valuable addition or fix to our project.
We believe that this feature would benefit greatly from contributions from the community,
even from first-time contributors.

If you're interested in contributing to this feature,
please take a look at our contribution guide.
It has all the information you need to know about how to submit a pull request and contribute to our project.

You're also welcome to join our Discord server
and discuss this feature with the collaborators or other contributors under the channel 'contributions'.

Please feel free to reach out to us if you have any questions, or need help with anything.
We appreciate your feedback and look forward to working with you!

@noomorph
Copy link
Collaborator Author

Appendix: Preliminary Technical Exploration

Below are some exploratory CLI commands and methods that can be used to directly manipulate APKs and DEX files. This is provided as a starting point for those interested in the actual implementation:

  1. Disassemble the APK:

    Using apktool:

    apktool d YourApp.apk -o output_folder
  2. Working with Smali files:

    After disassembling the APK with apktool, you'll find .smali files inside the output_folder/smali/ directory.

    To rename package references:

    find output_folder/smali/ -type f -name "*.smali" -exec sed -i 's/com\/example\/DetoxTest/com\/facebook\/DetoxTest/g' {} +
  3. Reassembling the APK:

    Using apktool:

    apktool b output_folder -o NewApp.apk
  4. Signing the APK:

    Before you can install and run the new APK, you need to sign it:

    jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore your-keystore-file.keystore NewApp.apk alias_name
  5. Converting Java to DEX:

    If there's a need to convert Java bytecode into DEX (Dalvik bytecode) for Android:

    dx --dex --output=output.dex input.class
  6. Exploring APK contents:

    To view the contents and inspect the APK, you can use the aapt tool:

    aapt list -v YourApp.apk

These are just preliminary steps and commands to start the exploration. They are generated via ChatGPT, so take them with a grain of salt. But, overall, the concept is feasible, and these things can be achieved – I talked to Valentyn, who's skilled in reverse engineering for Android, and he confirmed that this is a realistic goal, although we never know the caveats and their impact until we actually make this happen.

@noomorph
Copy link
Collaborator Author

Here's a list of issues that could have been presumably avoided if the test APK had been generated automatically:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant