-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: react-native android support (#777)
- Loading branch information
1 parent
b215efd
commit b4ae6be
Showing
8 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# React Native Setup | ||
|
||
## Boring and heavy way for all OSes | ||
|
||
Go to https://reactnative.dev/docs/environment-setup click in "React Native CLI Quickstart" and follow instructions. | ||
|
||
If you don't want to fill your system with the full android studio or XCode follow the instructions below but be aware that there are dragons ahead! | ||
|
||
## Android | ||
|
||
```bash | ||
brew install --cask adoptopenjdk/openjdk/adoptopenjdk8 | ||
export ANDROID_SDK_ROOT="~/android-sdk" | ||
touch ~/.android/repositories.cfg | ||
``` | ||
|
||
Download `cmdline-tool` from [https://developer.android.com/studio#cmdline-tools](https://developer.android.com/studio#cmdline-tools) and extract to `~/android-sdk/cmdline-tools/latest` | ||
|
||
```bash | ||
|
||
~/android-sdk/cmdline-tools/latest/bin/sdkmanager --update | ||
~/android-sdk/cmdline-tools/latest/bin/sdkmanager "platform-tools" "platforms;android-29" "build-tools;29.0.2" "system-images;android-29;default;x86_64" | ||
|
||
// in your .zshrc or similar add sdk to PATH | ||
PATH=$PATH:$ANDROID_SDK_ROOT/emulator | ||
PATH=$PATH:$ANDROID_SDK_ROOT/tools | ||
PATH=$PATH:$ANDROID_SDK_ROOT/tools/bin | ||
PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools | ||
export PATH | ||
``` | ||
Now all the tools are in the `$PATH` , no need for absolute paths anymore. | ||
|
||
### Some examples | ||
|
||
You normally dont need to run any of these | ||
|
||
```bash | ||
# install new platforms, build tools and system images | ||
sdkmanager --update | ||
sdkmanager "platforms;android-29" "build-tools;29.0.2" "system-images;android-29;default;x86_64" | ||
|
||
# create an avd | ||
avdmanager create avd -n aegir-android -d pixel --package "system-images;android-29;default;x86_64" | ||
|
||
# delete avd | ||
avdmanager delete avd -n aegir-android | ||
|
||
# manually run the emulator | ||
emulator @aegir-android | ||
|
||
# list avds | ||
emulator -list-avds | ||
|
||
# redirect port trafic | ||
adb -s emulator-5554 reverse tcp:3000 tcp:3000 | ||
adb reverse --list | ||
adb reverse --remove-all | ||
``` | ||
> If the internal aegir AVD changes SDK versions you might need to run the `sdkmanager` above to update and install the new SDK versions in your system. | ||
|
||
### emulator acceleration (optional) | ||
|
||
[https://developer.android.com/studio/run/emulator-acceleration#vm-mac](https://developer.android.com/studio/run/emulator-acceleration#vm-mac) | ||
|
||
### Aegir config | ||
Android needs special attention for networks settings ([docs](https://developer.android.com/studio/run/emulator-networking)). The most common change is to use the special address `10.0.2.2` to redirect trafic to your local loopback interface `127.0.0.1`. | ||
|
||
```js | ||
module.exports = { | ||
test: { | ||
async before (options) { | ||
let echoServer = new EchoServer() | ||
await echoServer.start() | ||
const { address, port } = echoServer.server.address() | ||
let hostname = address // address will normally be 127.0.0.1 | ||
|
||
if(options.runner === 'react-native-android') { | ||
hostname = '10.0.2.2' | ||
} | ||
|
||
return { | ||
echoServer, | ||
env: { ECHO_SERVER : format({ protocol: 'http:', hostname, port })} | ||
} | ||
}, | ||
async after (options, before) { | ||
await before.echoServer.stop() | ||
} | ||
} | ||
} | ||
|
||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
'use strict' | ||
const path = require('path') | ||
const execa = require('execa') | ||
const merge = require('merge-options') | ||
|
||
/** | ||
* @typedef {import("execa").Options} ExecaOptions | ||
* @typedef {import('../types').TestOptions} TestOptions | ||
* @typedef {import('../types').GlobalOptions} GlobalOptions | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {TestOptions & GlobalOptions} argv | ||
* @param {ExecaOptions} execaOptions | ||
*/ | ||
module.exports = async (argv, execaOptions) => { | ||
const AVDName = 'aegir-android-29' | ||
const extra = argv['--'] ? argv['--'] : [] | ||
const emulator = process.env.CI ? [] : ['--emulator', AVDName] | ||
const forwardOptions = /** @type {string[]} */([ | ||
...extra, | ||
argv.timeout && `--timeout=${argv.timeout}`, | ||
argv.grep && `--grep=${argv.grep}`, | ||
argv.bail && '--bail' | ||
].filter(Boolean)) | ||
const files = argv.files.length > 0 | ||
? argv.files | ||
: [ | ||
'**/*.spec.{js,ts}', | ||
'test/browser.{js,ts}' | ||
] | ||
|
||
// before hook | ||
const before = await argv.fileConfig.test.before(argv) | ||
const beforeEnv = before && before.env ? before.env : {} | ||
|
||
await checkAndroidEnv() | ||
|
||
if (!await checkAvd(AVDName)) { | ||
await execa('avdmanager', [ | ||
'create', | ||
'avd', | ||
'-n', AVDName, | ||
'-d', 'pixel', | ||
'--package', 'system-images;android-29;default;x86_64' | ||
]) | ||
} | ||
|
||
// run pw-test | ||
await execa('rn-test', | ||
[ | ||
...files, | ||
'--platform', argv.runner === 'react-native-android' ? 'android' : 'ios', | ||
...emulator, | ||
'--reset-cache', | ||
...forwardOptions | ||
], | ||
merge( | ||
{ | ||
env: { | ||
AEGIR_RUNNER: argv.runner, | ||
NODE_ENV: process.env.NODE_ENV || 'test', | ||
...beforeEnv | ||
}, | ||
preferLocal: true, | ||
localDir: path.join(__dirname, '../..'), | ||
stdio: 'inherit' | ||
}, | ||
execaOptions | ||
) | ||
) | ||
|
||
// after hook | ||
await argv.fileConfig.test.after(argv, before) | ||
} | ||
|
||
/** | ||
* Check for avd | ||
* | ||
* @param {string} name | ||
*/ | ||
async function checkAvd (name) { | ||
const avd = await execa('emulator', ['-list-avds']) | ||
|
||
return avd.stdout.split('\n').includes(name) | ||
} | ||
|
||
async function checkAndroidEnv () { | ||
if (!process.env.ANDROID_SDK_ROOT) { | ||
throw new Error('ANDROID_SDK_ROOT is not set') | ||
} | ||
|
||
try { | ||
await execa('emulator', ['-help']) | ||
// await execa('sdkmanager') | ||
await execa('avdmanager', ['list']) | ||
} catch (err) { | ||
throw new Error(`"Command ${err.path}" is not available, you need to properly setup your android environment.`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters