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

feat: Support autolinking C++ only TurboModules on Android #2387

Merged
merged 7 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/autolinking.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ You’re already using Gradle, so Android support will work by default.

On the iOS side, you will need to ensure you have a Podspec to the root of your repo. The `react-native-webview` Podspec is a good example of a [`package.json`](https://github.com/react-native-community/react-native-webview/blob/master/react-native-webview.podspec)-driven Podspec. Note that CocoaPods does not support having `/`s in the name of a dependency, so if you are using scoped packages - you may need to change the name for the Podspec.

### Pure C++ libraries

Alternatively, if you have a pure C++ library and don't want to use Gradle, you can still use autolinking. You need to update your `react-native.config.js` to include the following:

```js
// react-native.config.js
module.exports = {
dependency: {
platforms: {
android: {
sourceDir: 'path/to/your/c++/code',
cxxModuleCMakeListsPath: `relative/path/to/CMakeLists.txt`, // This is relative to the sourceDir.
cxxModuleCMakeListsModuleName: 'MyModule', // This is the name of the CMake target.
cxxModuleHeaderName: 'MyHeader', // CLI will include this header while linking.
},
},
},
};
```

## How can I customize how autolinking works for my package?

A library can add a `react-native.config.js` configuration file, which will customize the defaults, example:
Expand Down
14 changes: 12 additions & 2 deletions packages/cli-platform-android/native_modules.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ class ReactNativeModules {
reactNativeModules.forEach { reactNativeModule ->
def nameCleansed = reactNativeModule["nameCleansed"]
def dependencyConfiguration = reactNativeModule["dependencyConfiguration"]
def isPureCxxDependency = reactNativeModule["isPureCxxDependency"]
if (isPureCxxDependency) {
return
}

appProject.dependencies {
if (reactNativeModulesBuildVariants.containsKey(nameCleansed)) {
reactNativeModulesBuildVariants
Expand Down Expand Up @@ -247,10 +252,14 @@ class ReactNativeModules {
"${prefix}${packageName}.${className}${suffix}"
})
}
packageImports = packages.collect {
packageImports = packages
.findAll { !it.isPureCxxDependency }
.collect {
"// ${it.name}\n${interpolateDynamicValues(it.packageImportPath)}"
}.join('\n')
packageClassInstances = ",\n " + packages.collect {
packageClassInstances = ",\n " + packages
.findAll { !it.isPureCxxDependency }
.collect {
interpolateDynamicValues(it.packageInstance)
}.join(",\n ")
}
Expand Down Expand Up @@ -471,6 +480,7 @@ class ReactNativeModules {
reactNativeModuleConfig.put("cxxModuleCMakeListsModuleName", androidConfig["cxxModuleCMakeListsModuleName"])
reactNativeModuleConfig.put("cxxModuleCMakeListsPath", androidConfig["cxxModuleCMakeListsPath"])
reactNativeModuleConfig.put("cxxModuleHeaderName", androidConfig["cxxModuleHeaderName"])
reactNativeModuleConfig.put("isPureCxxDependency", androidConfig["isPureCxxDependency"])

if (androidConfig["buildTypes"] && !androidConfig["buildTypes"].isEmpty()) {
reactNativeModulesBuildVariants.put(nameCleansed, androidConfig["buildTypes"])
Expand Down
43 changes: 27 additions & 16 deletions packages/cli-platform-android/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,38 @@ export function dependencyConfig(
? path.join(sourceDir, userConfig.manifestPath)
: findManifest(sourceDir);
const buildGradlePath = findBuildGradle(sourceDir, true);

if (!manifestPath && !buildGradlePath) {
const isPureCxxDependency =
userConfig.cxxModuleCMakeListsModuleName != null &&
userConfig.cxxModuleCMakeListsPath != null &&
userConfig.cxxModuleHeaderName != null &&
!manifestPath &&
!buildGradlePath;

if (!manifestPath && !buildGradlePath && !isPureCxxDependency) {
return null;
}

const packageName =
userConfig.packageName || getPackageName(manifestPath, buildGradlePath);
const packageClassName = findPackageClassName(sourceDir);
let packageImportPath = null,
packageInstance = null;

/**
* This module has no package to export
*/
if (!packageClassName) {
return null;
}
if (!isPureCxxDependency) {
const packageName =
userConfig.packageName || getPackageName(manifestPath, buildGradlePath);
const packageClassName = findPackageClassName(sourceDir);

/**
* This module has no package to export
*/
if (!packageClassName) {
return null;
}

const packageImportPath =
userConfig.packageImportPath ||
`import ${packageName}.${packageClassName};`;
packageImportPath =
userConfig.packageImportPath ||
`import ${packageName}.${packageClassName};`;

const packageInstance =
userConfig.packageInstance || `new ${packageClassName}()`;
packageInstance = userConfig.packageInstance || `new ${packageClassName}()`;
}

const buildTypes = userConfig.buildTypes || [];
const dependencyConfiguration = userConfig.dependencyConfiguration;
Expand Down Expand Up @@ -193,5 +203,6 @@ export function dependencyConfig(
cxxModuleCMakeListsModuleName,
cxxModuleCMakeListsPath,
cxxModuleHeaderName,
isPureCxxDependency,
};
}
5 changes: 3 additions & 2 deletions packages/cli-types/src/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export type AndroidProjectParams = {

export type AndroidDependencyConfig = {
sourceDir: string;
packageImportPath: string;
packageInstance: string;
packageImportPath: string | null;
packageInstance: string | null;
dependencyConfiguration?: string;
buildTypes: string[];
libraryName?: string | null;
Expand All @@ -35,6 +35,7 @@ export type AndroidDependencyConfig = {
cxxModuleCMakeListsModuleName?: string | null;
cxxModuleCMakeListsPath?: string | null;
cxxModuleHeaderName?: string | null;
isPureCxxDependency?: boolean;
};

export type AndroidDependencyParams = {
Expand Down
Loading