React-native-config: Error while implementing dynamic applicationId from .env files

Created on 23 Aug 2017  路  10Comments  路  Source: luggit/react-native-config

I have set up my defaultConfig like:

    defaultConfig {
        applicationId project.env.get("APP_ID")
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 2
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        resValue "string", "build_config_package", "com.example"
    }

My .env.dev contains:

APP_ID=com.example.dev

But when I run my script to install my dev version (which uses .env.dev), I get the following error:

Starting the app on emulator-5554 (adb -s emulator-5554 shell am start -n com.example/com.example.MainActivity)...
Starting: Intent { cmp=com.example/.MainActivity }
Error type 3
Error: Activity class {com.example/com.example.MainActivity} does not exist.

I did add the advanced config to my defaultConfig, as you can see.

My AndroidManifest.xml has the packageName set to the above "com.example" value as well within the manifest tag.

Main Activity is defined as:

        <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
                android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

When I change my applicationId to be static (just "com.example") everything works fine.

Most helpful comment

@i8wu my current setup (which has com.example.dev and com.example package names) is:

AndroidManifest.xml:

<application
            android:name=".MainApplication"
    ...
    <activity
                android:name=".MainActivity"
        ...
    </activity>
</application>

All other references to applicationId are substituted using ${applicationId} for instance:

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="${applicationId}" /> <-- Substitution
            </intent-filter>
        </receiver>

build.gradle

 defaultConfig {
        applicationId "com.example"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 6
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        resValue "string", "build_config_package", "com.example" // don't think this does anything
    }
...
    buildTypes {
        release {
            ...
        }
        debug {
            applicationIdSuffix ".dev"
        }
    }

FINALLY, I've defined run scripts in my package.json because RN has some issues with different package names:

    "scripts": {
                ...
        "android-dev": "export ENVFILE=.env.dev && cd android && ./gradlew installDebug && adb shell am start -n com.example.dev/com.example.MainActivity && cd ..",
        "android-prod": "ENVFILE=.env.prod react-native run-android",
        "build-android-prod": "export ENVFILE=.env.prod && cd android && ./gradlew assembleRelease && cd .."
    },

*Note that the android-dev script is important because you need to launch the correct Intent. RN has a bug where it launches the production package name even though you installed the debug.

I've had no issues with this approach. Please let me know if it works for you!

All 10 comments

In the manifest file, if we use:

package="@string/APP_ID"

Current script doesn't replace the value, and so the application won't launch

@btrautmann if the java package is com.example , try:

  <activity
                android:name="com.example.MainActivity"
                android:label="@string/app_name"
                android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
                android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

try cleaning android project once using this command
cd android/ && ./gradlew clean
and build it again
react-native run-android

I was also facing the same issue. My folder structure was different. Make sure that your MainActivity is the same folder structure com.example.dev

   |---com
           |--example
                    |--dev
                           |--MainActivity.java

This did end up being an package/Activity naming issue. I was trying to substitute applicationId into these fields when really the packageName in the manifest tag and the Activity names just correspond with the folder structure. applicationId substitution is only important for the GCM receivers and other things that aren't just folder-structure related. I still am having issues with build flavor support but will open a separate issue. Thanks to all who helped.

@btrautmann So how did you fix the issue? Thanks.

@i8wu my current setup (which has com.example.dev and com.example package names) is:

AndroidManifest.xml:

<application
            android:name=".MainApplication"
    ...
    <activity
                android:name=".MainActivity"
        ...
    </activity>
</application>

All other references to applicationId are substituted using ${applicationId} for instance:

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="${applicationId}" /> <-- Substitution
            </intent-filter>
        </receiver>

build.gradle

 defaultConfig {
        applicationId "com.example"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 6
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        resValue "string", "build_config_package", "com.example" // don't think this does anything
    }
...
    buildTypes {
        release {
            ...
        }
        debug {
            applicationIdSuffix ".dev"
        }
    }

FINALLY, I've defined run scripts in my package.json because RN has some issues with different package names:

    "scripts": {
                ...
        "android-dev": "export ENVFILE=.env.dev && cd android && ./gradlew installDebug && adb shell am start -n com.example.dev/com.example.MainActivity && cd ..",
        "android-prod": "ENVFILE=.env.prod react-native run-android",
        "build-android-prod": "export ENVFILE=.env.prod && cd android && ./gradlew assembleRelease && cd .."
    },

*Note that the android-dev script is important because you need to launch the correct Intent. RN has a bug where it launches the production package name even though you installed the debug.

I've had no issues with this approach. Please let me know if it works for you!

So this is wrong?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="@string/APP_ID">

should be:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="br.com.app.name">

?

Instead of having to use the npm android-dev script that btrautmann suggests:

"android-dev": "export ENVFILE=.env.dev && cd android && ./gradlew installDebug && adb shell am start -n com.example.dev/com.example.MainActivity && cd ..",

You can now specifiy the appIdSuffix in react-native run-android like so:

"android-dev": "export ENVFILE=.env.dev && react-native run-android --appIdSuffix dev",

And this should work 馃憤

Reference https://stackoverflow.com/a/48340408/10211406

Was this page helpful?
0 / 5 - 0 ratings

Related issues

swl367 picture swl367  路  4Comments

ORESoftware picture ORESoftware  路  3Comments

sonlexqt picture sonlexqt  路  4Comments

joncursi picture joncursi  路  4Comments

kidnapkin picture kidnapkin  路  3Comments