React Native: Error: Package name not found - android

After integrating RN into an existing Android project, I get the following error:
Error: Package name not found in /home/.../AndroidManifest.xml at Object.projectConfig (/home/.../rn_integrated_app/node_modules/#react-native-community/cli-platform-android/build/config/index.js:74:11) at Object.get project [as project]
As I understand the problem is that there is no package attribute in the relevant AndroidManifest.xml file. Since my project has many flavors, the package attribute is added dynamically, while compiling, through app/build.gradle:
def pkgDataEntry = getRightValue(packagesData, variantMap)
variant.getMergedFlavor().applicationId = pkgDataEntry.pkg
So that the final merged manifest file does have the package attribute.
The error occurs here(#react-native-community/cli-platform-android/build/config/index.js):
const packageName = userConfig.packageName || getPackageName(manifest);
if (!packageName) {
throw new Error(`Package name not found in ${manifestPath}`);
}
Is there a way to make RN read the merged manifest file?
If not, how can I modify userConfig to contain the package name? I couldn`t find anything about it in the docs.
Thank you

For me, the solution was to add the "package" tag to the manifest tag.
I didn't have to create another dummy folder.
The manifest open tag now looks like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourProjectName">

If anybody encounters the same issue, when having multiple flavors and AndroidManifest.xml files,here`s my conclusion:
RN has some kind of a bug that it requires the first folder alphabetically, in android/app/src, which has AndroidManifest.xml file,to have the package attribute. Otherwise, it will throw an error.
A simple solution would be to create a dummy folder, e.g aaa (first alphabetically) and to create AndroidManifest.xml inside of it, with the following attribute - package:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="aaa"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission
android:name="android.permission.GET_ACCOUNTS"
tools:node="remove"/>
</manifest>

For me, this error start after upgrade the sdkVersion from 31 to 33 and gradle version.
I did check the changes in git, and notice that the package attribute was removed from AndroidManifest.xml.
So I did add this attribute again.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yourpackage">

Related

Android library with SyncAdapter not running in more than 1 app in the same phone

I am creating an Android library that uses SyncAdapter to do a small task in the background every day. This works perfectly fine if used in only one app. If the same library is used by two apps in the same phone, it gives CONFLICTING_PROVIDER error.
To fix this. I used ${applicationId} in library manifest like:
<provider
android:name=".sync.DummyProvider"
android:authorities="${applicationId}.mysdk"
android:exported="false"
android:syncable="true" />
This generates dynamic authority for the provider.
Now, the problem is that SyncAdapter needs to have contentAuthority in the xml folder.
<sync-adapter
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="#string/sync_account_type"
android:allowParallelSyncs="false"
android:contentAuthority="<-----NOT SURE WHAT TO ADD HERE----->"
android:isAlwaysSyncable="true"
android:supportsUploading="false"
android:userVisible="false"/>
I tried android:contentAuthority="${applicationId}.mysdk" but applicationId doesn't get replaced by the app's package name. Instead, it take it as it is and gives error as:
E/ActivityThread: Failed to find provider info for ${applicationId}.mysdk
These are the step I have tried till now
Creating stringValue from build.gradle in library's build.gradle file, but here applicationId can not be used there.
Moved contentAuthority in the Manifest, as ${applicationId} works there but nothing happens after doing this. contentAuthority needs to be in the xml folder.
First you need to declare the application id in your build.gradle file
defaultConfig {
applicationId "com.my.package"
....
}
Then use the following xml for your syncAdapter
<sync-adapter
xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="#string/account_authorities"
android:accountType="#string/account_type"
android:userVisible="false"
android:supportsUploading="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="true"/>
#pranav-agraval's answer worked.
I didn't change anything in the SDK. The string resource I was using had a key content_provider.
I would have to ask any one who is using the SDK to add this key to their string.xml file. This should be of pattern application_package_name.sdk_name.

Error in configuring network security xml in android

In the android network security docs , it says to create a network_security_config.xml file in res/xml folder.
In order to make an entry in manifest file, it says to make an entry in this way
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="#xml/network_security_config"
... >
...
</application>
</manifest>
But I am getting the error No resource identifier found for attribute 'networkSecurityConfig' in package 'android'
I also checked for the valid attributes in application tag ( android docs ), there is no attribute as "networksecurityconfig"
You need to set your compileSdkVersion to 24 or higher and set your buildToolsVersion to 24.0.0 or higher.
With regards to the documentation, that's a documentation bug.
The documentation is fixed, the xml should live inside res/xml which is not in the root but inside android/app:
android/app/src/main/res/xml/network_security_config.xml
From minimumSDK 20 you will need the file and be sure to whitelist localhost and 10.0.2.2 and 10.0.3.2 or you'll have trouble on development

Android Studio using a library and project with the same package name

I have a library I use as a base for all my android apps and has the following manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
xmlns:tools="http://schemas.android.com/tools"
android:versionCode="36"
android:versionName="1.b" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="21"
tools:overrideLibrary="com.facebook.android"/>
I then try to use it in one of my projects which has the following manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
xmlns:tools="http://schemas.android.com/tools"
android:versionCode="36"
android:versionName="1.6" >
<uses-library
android:name="com.example.android"
android:required="true"/>
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="21"
tools:overrideLibrary="com.facebook.android"/>
Note that the package name used is the same: com.example.android. Since the app is published under com.example.android, I cannot change it for the app. As for the library, for historical reasons, it has the same pacakge name. When I build the project, I get the following error:
FAILURE: Build failed with an exception.
What went wrong: Execution failed for task ':exampleCom:processDebugResources'.
Error: A library uses the same package as this project: com.example.android You can temporarily disable this error with
android.enforceUniquePackageName=false However, this is temporary and
will be enforced in 1.0
I do not want to change the package name of either library or app. I am not sure where to add the "android.enforceUniquePackageName=false" . Any ideas? Also, how to solve beyond 1.0 (which I am already using)?
You can add enforceUniquePackageName=false in the app modules build.gradle file under android:
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
enforceUniquePackageName = false
...
}
Unfortunately this results in another problem with an unfixed bug from the build tools.
Error:Execution failed for task
':app:packageAllDebugClassesForMultiDex'.
java.util.zip.ZipException: duplicate entry: ... BuildConfig.class
See https://stackoverflow.com/a/27310034/668400
An ounce of prevention is worth a pound of cure. It may not help this particular situation, but for future developers to avoid getting into this situation, please note the JLS recommendation in Chapter 6. Names which addresses this very problem:
Package Names
Developers should take steps to avoid the possibility of two published
packages having the same name by choosing unique package names for
packages that are widely distributed. This allows packages to be
easily and automatically installed and catalogued. This section
specifies a suggested convention for generating such unique package
names. Implementations of the Java SE platform are encouraged to
provide automatic support for converting a set of packages from local
and casual package names to the unique name format described here.
If unique package names are not used, then package name conflicts may
arise far from the point of creation of either of the conflicting
packages. This may create a situation that is difficult or impossible
for the user or programmer to resolve. The class ClassLoader can be
used to isolate packages with the same name from each other in those
cases where the packages will have constrained interactions, but not
in a way that is transparent to a naïve program.
You form a unique package name by first having (or belonging to an
organization that has) an Internet domain name, such as oracle.com.
You then reverse this name, component by component, to obtain, in this
example, com.oracle, and use this as a prefix for your package names,
using a convention developed within your organization to further
administer package names. Such a convention might specify that certain
package name components be division, department, project, machine, or
login names.
Package name is a unique identifier for a package and therefore you should not have the same package name for both the library and the application. Change the package name for the library if the application is already published.
worked on as 2.2, gradle 2.14.1
android {
enforceUniquePackageName = false
//it was deprecated, but still work
android.packageBuildConfig = false
...
}
details:new-build-system

Android Studio 6.0 merger fail

Just updated to the latest version of Android Studio and i get this error in the AndroidManifest file
Manifest merger failed : Attribute application#icon value=(#drawable/project_launcher_icon) from AndroidManifest.xml:48:9
is also present at com.github.anupcowkur:reservoir:1.1.1:6:45 value=(#drawable/ic_launcher)
Suggestion: add 'tools:replace="icon"' to element at AndroidManifest.xml:44:5 to override
I tried adding tools:replace="#drawable/ic_drawer" in my manifest but i get this error:
Error:(44, 5) tools:replace specified at line:44 for attribute tools:drawable/ic_drawer, but no new value specified
Any ideas?
Following Android Studio's suggestion and adding the following attribute tools:replace="icon" should allow it to build your app successfully, without resorting to using the old manifest merger (which isn't a very forward-looking solution indeed).
Of course, you'll first have to declare the namespace "tools" in order to use it:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.sample.app" >
You should add tools:replace="icon", just like the error message says.
Additional attributes can be replaced using the syntax tools:replace="icon,name,theme"
look here:
All markers belong to the Android tools namespace, therefore you must declare the namespace in any AndroidManifest.xml containing at least one marker :
xmlns:tools="http://schemas.android.com/tools"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.tests.flavorlib.app"
**xmlns:tools="http://schemas.android.com/tools"**>
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
**tools:replace=”icon, label”**/>
</manifest>
you should add xlms:tools and tools:replace those two lines in manifest file.
Android Studio 0.6 use the new manifest merger tool. This new merger was introduced in version 0.10 of the plugin. As of 0.11, this tool is used by default by the gradle plugin.
In order to revert to the old manifest merger, please add to your build.gradle the following configuration :
android {
useOldManifestMerger true
}
For me this worked. Try adding the code in the main module(project) manifest file:
add
xmlns:tools="http://schemas.android.com/tools" in your manifest tag
add
tools:replace="android:icon,android:label,android:theme" in your application tag
These will let Android Studio know that the icon, label and theme to be used are from that manifest and not from other projects included.

Issues while generating the R file

I have some issues while generating the R file in android. I have two packages in my src folder com.mydomain and net.mydomain. Now only the net.mydomain.R file is generated not com.mydomain.R file as a result. the java files in my com.mydomain cannot access the ids like R.id R.layout. It is only populating for net.mydomain. So how can I generate com.mydomain.R file as well. Thanks
Be sure that the package you're using is the same that is in the manifest, check the line package, for example, in your case the package is com.mydomain, so:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mydomain"
android:versionCode="1"
android:versionName="1.0" >
Why not adding the net.mydomain like, com.mydomain.net?, using that way, you can solve your issue.

Categories

Resources