admin管理员组

文章数量:1023827

I need to add the following lines to the default config within my android build.gradle for my expo managed project

{ missingDimensionStrategy 'store', 'play' }

I believe I need to make a mod/plugin to add to this, but what would this look like?

Here is what I've tried to implement so far: /guides/config-plugins/#mod-plugins

using the withAppBuildGradle I believe is the best bet, but I'm not sure how to specifically add just the line in the android default config.

I need to add the following lines to the default config within my android build.gradle for my expo managed project

{ missingDimensionStrategy 'store', 'play' }

I believe I need to make a mod/plugin to add to this, but what would this look like?

Here is what I've tried to implement so far: https://docs.expo.dev/guides/config-plugins/#mod-plugins

using the withAppBuildGradle I believe is the best bet, but I'm not sure how to specifically add just the line in the android default config.

Share Improve this question asked Dec 28, 2022 at 16:24 guitarman0456guitarman0456 711 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If the question is still valid, this is how you can achieve it using Expo modified flow. Starting from Expo SDK 41 you can use Config plugins

If you want to inject properties to android gradle this is how it can be achieved.

  1. Create a new file in a root directory called injectedAndroidConfig.js
const { withAppBuildGradle } = require('@expo/config-plugins');    

module.exports = function withAndroidStrategiesPlugin(config) {
    return withAppBuildGradle(config, (config) => {
        config.modResults.contents += `
            android {
                pileSdkVersion 31
                buildToolsVersion "31.0.0"
       
                defaultConfig {
                    targetSdkVersion 31
                    missingDimensionStrategy "store", "play"
                }
            }
        `;
        return config;
      });
    };

Here you can also set other properties you want (for example sdk version and so on)

  1. You should update your app.json to use your created plugin
{
  "expo": {
    "name": "appName",
    [...]
    "plugins": ["./injectedAndroidConfig", "react-native-iap"]
   }
}

In case you are using this with react-native-iap on Expo (which you probably are), you do not need to modify build.gradle. I fell into this trap and was stuck for 2+ hours.

Looking at the docs, in the FAQ there is a section about using it in Expo: https://react-native-iap.dooboolab./docs/faq#how-do-i-use-react-native-iap-in-expo

Tl/dr: Add the following to app.json

{
  "expo": {
    "plugins": ["react-native-iap"]
  }
}

And rebuild your development build and it should all work, without any modification to build.gradle

Please note that react-native-iap package cannot be used in the "Expo Go" app because it requires custom native code.

Following @AsMartynas answer, I've used his solution to add a dependecy in build.gradle. To insert the dependecy I had to do some string manipulation, finding the index of dependencies { - which is the part of the file where dependencies lives - and replacing it with the content I needed. Here's the final code

const { withAppBuildGradle } = require("@expo/config-plugins");

const PLAY_BILLING_LIBRARY_VERSION = "6.0.1"

module.exports = function withAndroidStrategiesPlugin(config) {
  return withAppBuildGradle(config, (config) => {
    const initialIndex = config.modResults.contents.indexOf("dependencies {");

    // Add the billing dependency
    config.modResults.contents =
      config.modResults.contents.slice(0, initialIndex) +
      `dependencies {
    implementation(".android.billingclient:billing:${PLAY_BILLING_LIBRARY_VERSION}")` +
      config.modResults.contents.slice(initialIndex + "dependencies {".length);

    return config;
  });
};

I need to add the following lines to the default config within my android build.gradle for my expo managed project

{ missingDimensionStrategy 'store', 'play' }

I believe I need to make a mod/plugin to add to this, but what would this look like?

Here is what I've tried to implement so far: /guides/config-plugins/#mod-plugins

using the withAppBuildGradle I believe is the best bet, but I'm not sure how to specifically add just the line in the android default config.

I need to add the following lines to the default config within my android build.gradle for my expo managed project

{ missingDimensionStrategy 'store', 'play' }

I believe I need to make a mod/plugin to add to this, but what would this look like?

Here is what I've tried to implement so far: https://docs.expo.dev/guides/config-plugins/#mod-plugins

using the withAppBuildGradle I believe is the best bet, but I'm not sure how to specifically add just the line in the android default config.

Share Improve this question asked Dec 28, 2022 at 16:24 guitarman0456guitarman0456 711 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If the question is still valid, this is how you can achieve it using Expo modified flow. Starting from Expo SDK 41 you can use Config plugins

If you want to inject properties to android gradle this is how it can be achieved.

  1. Create a new file in a root directory called injectedAndroidConfig.js
const { withAppBuildGradle } = require('@expo/config-plugins');    

module.exports = function withAndroidStrategiesPlugin(config) {
    return withAppBuildGradle(config, (config) => {
        config.modResults.contents += `
            android {
                pileSdkVersion 31
                buildToolsVersion "31.0.0"
       
                defaultConfig {
                    targetSdkVersion 31
                    missingDimensionStrategy "store", "play"
                }
            }
        `;
        return config;
      });
    };

Here you can also set other properties you want (for example sdk version and so on)

  1. You should update your app.json to use your created plugin
{
  "expo": {
    "name": "appName",
    [...]
    "plugins": ["./injectedAndroidConfig", "react-native-iap"]
   }
}

In case you are using this with react-native-iap on Expo (which you probably are), you do not need to modify build.gradle. I fell into this trap and was stuck for 2+ hours.

Looking at the docs, in the FAQ there is a section about using it in Expo: https://react-native-iap.dooboolab./docs/faq#how-do-i-use-react-native-iap-in-expo

Tl/dr: Add the following to app.json

{
  "expo": {
    "plugins": ["react-native-iap"]
  }
}

And rebuild your development build and it should all work, without any modification to build.gradle

Please note that react-native-iap package cannot be used in the "Expo Go" app because it requires custom native code.

Following @AsMartynas answer, I've used his solution to add a dependecy in build.gradle. To insert the dependecy I had to do some string manipulation, finding the index of dependencies { - which is the part of the file where dependencies lives - and replacing it with the content I needed. Here's the final code

const { withAppBuildGradle } = require("@expo/config-plugins");

const PLAY_BILLING_LIBRARY_VERSION = "6.0.1"

module.exports = function withAndroidStrategiesPlugin(config) {
  return withAppBuildGradle(config, (config) => {
    const initialIndex = config.modResults.contents.indexOf("dependencies {");

    // Add the billing dependency
    config.modResults.contents =
      config.modResults.contents.slice(0, initialIndex) +
      `dependencies {
    implementation(".android.billingclient:billing:${PLAY_BILLING_LIBRARY_VERSION}")` +
      config.modResults.contents.slice(initialIndex + "dependencies {".length);

    return config;
  });
};

本文标签: javascriptHow can I add to default config in buildgradle in expo managed projectStack Overflow