0

I have a small android application with this manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.eliran.myapplication" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.eliran.myapplication.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I want alter the package name for testing integration with another andorid app.

However when I change to

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myTest.driver" >

I get an error that R class is not found

Error:(37, 25) error: package R does not exist

how can i fix this, with minimal effort and without changing the package name of each class?

pah
  • 4,700
  • 6
  • 28
  • 37
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

2 Answers2

1

Step #1: Return your package attribute to its original value

Step #2: Add applicationId to defaultConfig in your module's build.gradle file to your revised application ID

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 22
        applicationId "com.commonsware.android.espresso"
    }
}

This gives your app a separate application ID (the unique identifier for installation purposes) but keeps the package name the same (for R generation, etc.).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I was under the impression that either Gradle took precedence over the Manifest, or they had to match? – OneCricketeer Jul 16 '16 at 20:17
  • @cricket_007: "I was under the impression that either Gradle took precedence over the Manifest" -- correct. This is why we are changing `build.gradle`, returning the manifest to the original working package name. "or they had to match" -- they do not have to match. Otherwise, `applicationId` would be pointless, as the only valid value is the already-known `package` attribute value. – CommonsWare Jul 16 '16 at 20:18
  • Right. But when gradle goes to compile, does it not use that name for classes? Therefore, if that string doesn't match the actual, existing package name, won't it fail? – OneCricketeer Jul 16 '16 at 20:20
  • 1
    @cricket_007: "does it not use that name for classes?" -- it does not use that name for classes. `package` is used for classes. `applicationId` is only for the unique application ID. Pre-Gradle, `package` served both roles, and in the absence of `applicationId`, it still does. `applicationId` overrides `package` just for the unique application ID role, and it has no effect on `R`, `BuildConfig`, data binding, etc. class generation. – CommonsWare Jul 16 '16 at 20:22
  • Gotcha... I suppose I could have been linked here, but thanks for the clarification :) https://sites.google.com/a/android.com/tools/tech-docs/new-build-system/applicationid-vs-packagename – OneCricketeer Jul 16 '16 at 20:27
  • my app opens another app (say app `B`) that checks a whitelist of apps that can open it. Will this work with applicationId ? – Elad Benda2 Jul 16 '16 at 20:41
  • @EladBenda2: Presumably. I do not know how it "checks a whitelist of apps that can open it". – CommonsWare Jul 16 '16 at 20:52
0

You need to change the applicationID in your build.gradle file

ligi
  • 39,001
  • 44
  • 144
  • 244
  • my app opens another app (say app `B`) that checks a whitelist of apps that can open it. Will this work with applicationId ? – Elad Benda2 Jul 16 '16 at 20:41