specify R package path (Android) - android

Is it possible to specify the package path for R.class ?
The R class package is using the package path specified in the AndroidManifest.xml
The reason I ask that because I am trying to deploy the same codebase to different application.
The difference between the apps will be the image and text. while code logic remain the same.
To deploy the same code to multiple apps require unique package name for each app in the AndroidManifest.xml.
The problem is when the package name change, R is regenerated. All java file import for R is invalid with the outdated package. And I have to modify each java file for the R.class import. If I can specify a common path for R. This problem will be solved.

You no need to go every screen to change import com.packagename.R.
1) press ctrl+h
2) Select "File Search" tab and enter your old packagename to search
3) Press "Replace" button
4) Give your new package name to replace with.
5) Clean build and enjoy.
If your using Linux machine single command do a trick
find yourprojectpath -type f -exec sed -i 's/com.oldpackage.name/com.new.packagename/g' {} \\;

Related

Change package name for Android in React Native

I used react-native init MyApp to initialise a new React Native app.
This created among others an Android project with the package com.myapp.
What's the best way to change this package name, for example to: com.mycompany.myapp?
I tried changing it in AndroidManifest.xml but it created other errors, so I'm assuming it's not the way.
Any idea?
I've renamed the project' subfolder from: "android/app/src/main/java/MY/APP/OLD_ID/" to: "android/app/src/main/java/MY/APP/NEW_ID/"
Then manually switched the old and new package ids:
In:
android/app/src/main/java/MY/APP/NEW_ID/MainActivity.java:
package MY.APP.NEW_ID;
In android/app/src/main/java/MY/APP/NEW_ID/MainApplication.java:
package MY.APP.NEW_ID;
In android/app/src/main/AndroidManifest.xml:
package="MY.APP.NEW_ID"
And in android/app/build.gradle:
applicationId "MY.APP.NEW_ID"
In android/app/BUCK:
android_build_config(
package="MY.APP.NEW_ID"
)
android_resource(
package="MY.APP.NEW_ID"
)
Gradle' cleaning in the end (in /android folder):
./gradlew clean
I use the react-native-rename* npm package. Install it via
npm install react-native-rename -g
Then, from the root of your React Native project, execute the following:
react-native-rename "MyApp" -b com.mycompany.myapp
To change the package name from com.myapp to: com.mycompany.myapp (for example),
For iOS app of the react app, use xcode - under general.
For the android app, open the build.gradle at module level. The one in the android/app folder. You will find
// ...
defaultConfig {
applicationId com.myapp
// ...
}
// ...
Change the com.myapp to whatever you need.
Hope this helps.
you can simply use react-native-rename npm package.
Install using
npm install react-native-rename -g
Then from the root of your React Native project execute the following
react-native-rename "MyApp" -b com.mycompany.myapp
react-native-rename on npm
but notice that, this lib remove your MainActivity.java and MainApplication.java.
before changing your package name, give a backup from this two file and, after changing package name just put it back to their place. this solution work for me
more info:
react-native-rename
REACT 0.64 | 2021 NO EXPO
Let original App name be: com.myApp
Let New App Name: 'com.newname.myApp`
android\app\src\main\AndroidManifest.xml
Rename xml attribute: `package="com.newname.myApp"
android\app\src\main\java\com\[yourAppName]\MainActivity.java
Rename first line from package com.myApp; To package com.newname.myApp;
android\app\src\main\java\com\[yourAppName]\MainApplication.java
Rename first line To package com.newname.myApp;
In class private static void initializeFlipper
reneme this line:
Class<?> aClass = Class.forName("com.myApp.ReactNativeFlipper");
To
Class<?> aClass = Class.forName("com.newname.myApp.ReactNativeFlipper");
android\app\build.gradle
On the defaultConfig rename applicationId
to "com.newname.myApp"
Run on command line:
$ npx react-native start
$ npx react-native run-android
Goto Android studio
Right click your package (most probably com)-> Refractor -> Rename -> Enter new package name in the dialog -> Do Refractor
It will rename your package name everywhere.
In VS Code, press Ctrl + Shift + F and enter your old package name in 'Find' and enter your new package in 'Replace'. Then press 'Replace all occurrences'.
Definitely not the pragmatic way. But, it's done the trick for me.
If you are using Android Studio-
changing com.myapp to com.mycompany.myapp
create a new package hierarchy com.mycompany.myapp under android/app/src/main/java
Copy all classes from com.myapp to com.mycompany.myapp using Android studio GUI
Android studio will take care of putting suitable package name for all copied classes. This is useful if you have some custom modules and don't want to manually replace in all the .java files.
Update android/app/src/main/AndroidManifest.xml and android/app/build.gradle (replace com.myapp to com.mycompany.myapp
Sync the project (gradle build)
The init script generates a unique identifier for Android based on the name you gave it (e.g. com.acmeapp for AcmeApp).
You can see what name was generated by looking for the applicationId key in android/app/build.gradle.
If you need to change that unique identifier, do it now as described below:
In the /android folder, replace all occurrences of com.acmeapp by com.acme.app
Then change the directory structure with the following commands:
mkdir android/app/src/main/java/com/acme
mv android/app/src/main/java/com/acmeapp android/app/src/main/java/com/acme/app
You need a folder level for each dot in the app identifier.
Source: https://blog.elao.com/en/dev/from-react-native-init-to-app-stores-real-quick/
I have a solution based on #Cherniv's answer (works on macOS for me). Two differences: I have a Main2Activity.java in the java folder that I do the same thing to, and I don't bother calling ./gradlew clean since it seems like the react-native packager does that automatically anyways.
Anyways, my solution does what Cherniv's does, except I made a bash shell script for it since I'm building multiple apps using one set of code and want to be able to easily change the package name whenever I run my npm scripts.
Here is the bash script I used. You'll need to modify the packageName you want to use, and add anything else you want to it... but here are the basics. You can create a .sh file, give permission, and then run it from the same folder you run react-native from:
rm -rf ./android/app/src/main/java
mkdir -p ./android/app/src/main/java/com/MyWebsite/MyAppName
packageName="com.MyWebsite.MyAppName"
sed -i '' -e "s/.*package.*/package "$packageName";/" ./android/app/src/main/javaFiles/Main2Activity.java
sed -i '' -e "s/.*package.*/package "$packageName";/" ./android/app/src/main/javaFiles/MainActivity.java
sed -i '' -e "s/.*package.*/package "$packageName";/" ./android/app/src/main/javaFiles/MainApplication.java
sed -i '' -e "s/.*package=\".*/ package=\""$packageName"\"/" ./android/app/src/main/AndroidManifest.xml
sed -i '' -e "s/.*package = '.*/ package = '"$packageName"',/" ./android/app/BUCK
sed -i '' -e "s/.*applicationId.*/ applicationId \""$packageName"\"/" ./android/app/build.gradle
cp -R ./android/app/src/main/javaFiles/ ./android/app/src/main/java/com/MyWebsite/MyAppName
DISCLAIMER: You'll need to edit MainApplication.java's comment near the bottom of the java file first. It has the word 'package' in the comment. Because of how the script works, it takes any line with the word 'package' in it and replaces it. Because of this, this script may not be future proofed as there might be that same word used somewhere else.
Second Disclaimer: the first 3 sed commands edit the java files from a directory called javaFiles. I created this directory myself since I want to have one set of java files that are copied from there (as I might add new packages to it in the future). You will probably want to do the same thing. So copy all the files from the java folder (go through its subfolders to find the actual java files) and put them in a new folder called javaFiles.
Third Disclaimer: You'll need to edit the packageName variable to be in line with the paths at the top of the script and bottom (com.MyWebsite.MyAppName to com/MyWebsite/MyAppName)
If you are using VSCode and Windows.
1.Press Control + Shift + F.
2.Find Your Package Name and Replace All with your new Package Name.
type "cd android"
type "./gradlew clean"
To change the Package name you have to edit four files in your project :
1. android/app/src/main/java/com/reactNativeSampleApp/MainActivity.java
2. android/app/src/main/java/com/reactNativeSampleApp/MainApplication.java
3. android/app/src/main/AndroidManifest.xml
4. android/app/build.gradle
The first 2 files have the package name as something like below.
package com.WHATEVER_YOUR_PACKAGE_NAME_IS;
Change it to your desired package name.
package com.YOUR_DESIRED_PACKAGE_NAME;
You have to also edit the package name in 2 other files.
android/app/build.gradle
android/app/src/main/AndroidManifest.xml
Note if you have a google-services.json file in your project then you have to also change your package name in that file.
After this, you have to ./gradlew clean your project.
Go to Android Studio, open app, right click on java and choose
New and then Package.
Give the name you want (e.g. com.something).
Move the files from the other package (you want to rename) to the new Package. Delete the old package.
Go to your project in your editor and use the shortcut for searching in all the files (on mac is shift cmd F). Type in the name of your old package. Change all the references to the new package name.
Go to Android Studio, Build, Clean Project, Rebuild Project.
Done!
Happy coding :)
After running this:
react-native-rename "MyApp" -b com.mycompany.myapp
Make sure to also goto Build.gradle under android/app/...
and rename the application Id as shown below:
defaultConfig {
applicationId:com.appname.xxx
........
}
I will provide you step by step procedure.
First of all, we need to understand why we need to rename a package?
Mostly for uniquely identifying App right? So React Native is a cross-platform to develop an app that runs on both iOS and Android. So What I recommended is to need the same identifier in both the platforms. In case Android Package Name is a unique identifier of your App and for iOS its bundle ID.
Steps:
Firstly close all editors/studio eg: xcode android studio vscode and then Install package "react-native-rename" in the terminal by running the command (Keep your project folder copy in another folder for backup if you don't feel this safe):
npm install react-native-rename -g
After sucessfully installing package open your project folder in the terminal and hit the below command where "Your app name Within double quotes" is app name and <Your Package Name without double/single quotes> is replaced with package/bundle name/id:
react-native-rename "< Your app name Within double quotes >" -b <Your Package Name without double/single quotes>
Example: react-native-rename "Aptitude Test App" -b com.vaibhavmojidra.aptitudetestapp
Note Make sure the package/bundle name/id is all in small case
This is not yet done yet now every file will change where the it had bundle/package names except one in ios project which we manually need to do for that open the xcode>open a project or file> choose file from projectfolder/ios/projectname.xcodeproj and click open
Click on your projectname.xcodeproj that is first file in the project navigator then in field of Bundle identifier enter the package name/Bundle ID as below:
After that run in the terminal
cd ios
Then run
pod install
Then run
cd ..
Now you are done for all the changes for ios just last step is to open Android Studio>Open an Existing Project> select projectfolder/android folder > Click on open
Let it load and complete all the process and then in the Build Menu click "Clean Project" and the in File Menu > Close Project
Close All editors and Now You can try the commands to run project on ios and android it will run with renamed packaged just while start server add extra flag --reset-cache:
npm start --reset-cache
Enjoy it will work now
I fixed this by manually updating package name in following places
1) app.json | ./
2) index.js | ./
3) package.json | ./
4) settings.gradle | ./android/
5) BUCK | ./android/app/
6) build.gradle | ./android/app/
7) AndroidManifest.xml | ./android/app/src/main/
8) MainActivity.java | ./android/app/src/main/java/**
9) MainApplication.java | ./android/app/src/main/java/**
10)strings.xml | ./android/app/src/main/res/values
11)ReactNativeFlipper.java| ./android/app/src/debug/java/com/<package-name>/
There are a lot of bad answers here
if you want to change the package name, go to your build.gradle in app
defaultConfig {
applicationId "your.new.package.name" //change this line
then when running the app from the command line, pass in the new package name as an arg
react-native run-android --appId your.new.package.name
There's no need to rename your whole project
I use the react-native-rename* npm package. Install it via
npm install react-native-rename -g
Then, from the root of your React Native project, execute the following:
react-native-rename "MyApp" -b com.mycompany.myapp
very simple way :
cd /your/project/dir
run this command :
grep -rl "com.your.app" . | xargs sed -i 's/com.your.app/com.yournew.newapp/g'
rename your folder
android/app/src/main/java/com/your/app
change to
android/app/src/main/java/com/yournew/newapp
After renaming package name everywhere,
from android studio. File -> Invalidate caches/Restart may fix this type of errors
this worked for me
This information can be helpful for you so listen properly...
After updating package name and changing files, don't forget to change in
package.json and app.json file at the root directory of your react native project
Otherwise you will get this error below
Invariant Violation: "Your new package name" has not been registered
Ivan Chernykh's answer is correct for react-native 0.67 and below
For react-native 0.68 and above with the new architecture once you're down with the steps which Ivan Chernykh has mentioned
For android you have to go to app => src => main => jni folder
In MainApplicationTurboModuleManagerDelegate.h
change below line
static constexpr auto kJavaDescriptor =
"Lcom/yournewpackagename/newarchitecture/modules/MainApplicationTurboModuleManagerDelegate;";
In MainComponentsRegistry.h
constexpr static auto kJavaDescriptor =
"Lcom/yournewpackagename/newarchitecture/components/MainComponentsRegistry;";
I just learned this today but adding this answer to help others. You just need to add a namespace to the app/build.gradle that references the old name, like this:
defaultConfig {
applicationId "com.mycompany.myapp"
namespace "com.myapp"
}
Then you can have a bash script that changes the applicationId with something like this:
function sedi {
if [ "$(uname)" == "Linux" ]; then
\sed -i "$#"
else
\sed -i "" "$#"
fi
}
bundle_name=com.mycompany.myapp
sedi -E "s/applicationId \"[-0-9a-zA-Z._]*\"/applicationId \"${bundle_name}\"/" android/app/build.gradle
React Native#0.70.x
Tried to rename package name from com.x to com.y.z
I got through this painful process by doing these:
Change displayName in app.json to z.
{
"name": "<THIS_SHOULD_STAY_SAME>",
"displayName": "<Z>"
}
Change the content of the string app_name in strings.xml
<resources>
...
<string name="app_name"><Z></string>
...
</resources>
Search through your react-native project for com.x and replace all com.x with com.y.z
!important: In case you're using external services, and in case if
you're generated a file and put it under /android you may want to
make sure the services that you're using will still work. I recommend
you to generate a new config file(s) using your new package name
com.y.z and put it after the finishing the steps described below.
Update the folder structure to this:
old: .../android/app/src/main/java/com/x
new: .../android/app/src/main/java/com/y/z
Move all the files under .../android/app/src/main/java/com/x to .../android/app/src/main/java/com/y/z.
Clean up.
$ ./gradlew clean
Build.
$ react-native run-android
Go to file android/app/src/main/AndroidManifest.xml -
Update :
attr android:label of Element application as :
Old value - android:label="#string/app_name"
New Value - android:label="(string you want to put)"
and
attr android:label of Element activity as :
Old value - android:label="#string/app_name"
New Value - android:label="(string you want to put)"
Worked for me, hopefully it will help.

Changing the package name

I planned to change the package name through smali(reverse)
when I open up apktool.yml, I saw this
forced-package-id: '127'
I have tried to change it but it crash
why should i do?
Assuming your goal is to rename the package name of the apk, the package names used for the classes are irrelevant. The package name of the apk is mostly unrelated to the package names of any classes in the apk. And there's no reason you need to touch the package id.
I would recommend unpacking the apk with apktool, and then edit the apktool.yml, setting renameManifestPackage to the new package name. Then when you rebuild the apk with apktool, it should use aapt's --rename-manifest-package functionality to change the package name.
After that, just resign the new apk and you should be good to go.
apktool d app.apk
// change "renameManifestPackage: null" in app/apktool.yml
// to "renameManifestPackage: my.new.package"
apktool b app -o new_app.apk
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ~/my.keystore new_app.apk mykeyname
And just to reiterate, you don't need to modify the package names of any classes.
To my knowledge there are no fast ways to change the package name.
You have to change the package="com.mycompany.myapp" in Manifest.xml and then manually replace all the package name occurrences in smali files (and folders). Finally edit apktool.yml replacing the old package name, with your new package name.
A full detailed tutorial can be found here: http://forum.xda-developers.com/showthread.php?t=2760965
I've tried different approaches but the most reliable for me has been the method described here:
https://forum.xda-developers.com/t/how-to-guide-mod-change-package-names-of-apks.2760965/
These are the steps:
Decode APK (using apktool)
Edit apktool.yml
Edit AndroidManifest.xml
Rename smali subfolders
Search & Replace *.smali (using Notepad++)
Build APK (using apktool)
Sign APK (using apksigner)
Tools needed:
Apktool - https://ibotpeaches.github.io/Apktool/
Notepad++ - https://notepad-plus-plus.org/downloads/
apksigner - https://developer.android.com/studio/command-line/apksigner
Download apktool and wrapper script to C:\apktool.
Let's assume you want to modify from/to as specified here:
From: XVipreSettings.apk (Package name: com.xvipre.settings)
To: ModdedApp.apk (Package name: com.modded.app)
1. Decode APK (using apktool)
apktool.bat d XVipreSettings.apk
2. Edit apktool.yml
apkFileName: XVipreSettings.apk
...
renameManifestPackage: null
is changed to:
apkFileName: ModdedApp.apk
...
renameManifestPackage: 'com.modded.app'
3. Edit AndroidManifest.xml
package="com.xvipre.settings"
is changed to:
package="com.modded.app"
4. Rename smali subfolders
move .\XVipreSettings\smali\com\xvipre\settings .\XVipreSettings\smali\com\xvipre\app
move .\XVipreSettings\smali\com\xvipre .\XVipreSettings\smali\com\modded
5. Search & Replace *.smali
Start Notepad++
Menu > Search > Find in Files... (Ctrl+Shift+F)
Find what : "Lcom/xvipre/settings"
Replace with : "Lcom/modded/app"
Filters : "*.smali"
Directory : "C:\apktool\XVipreSettings\smali\com"
Press Replace in Files (may take a few minutes)
6. Build APK
apktool.bat b XVipreSettings -o ModdedApp.apk
7. Sign APK
(I'm assuming 31.0.0 here yours may be different, also if you don't want to sign using debug certificate you need to modify this step accordingly)
%homedrive%%homepath%\AppData\Local\Android\Sdk\build-tools\31.0.0\apksigner.bat sign --ks %homedrive%%homepath%\.android\debug.keystore --ks-pass pass:android --key-pass pass:android C:\apktool\ModdedApp.apk

mobile-chrome-apps --link-to "Hello World" default

using the switches:
cca create YourApp --link-to=path/to/manifest.json
The packaged *.apk is built from the default create namespacing of "HelloWorld" and not "YourApp".
A cursory rename gave me INSTALL_PARSE_FAILED_MANIFEST_MALFORMED errors.
It seems a bit brittle. Is there something in the cca toolchain for this?
If not which files need to be changed?
Can someone with a 1500pt reputation add a tag mobile-chrome-apps please?
Thanks,
Nic
The argument YourApp is not the name of the application, but the folder where it is created. The name of your application is specified inside manifest.json and the Android packageId inside the manifest.mobile.json.
After you create your application (whether using --link-to or just starting with the default), modify those files and run cca prepare. The name and packageId should be correctly updated.
(Caveat: there are some leftover bits that don't get cleaned up, we have an open bug, but it should not impact you)
Probably your problem is because you have used capital letter in your project name:
Com.Example.PackageName
Try again using only low letter:
com.example.packagename
It should work!

IBM Worklight 5.0.5.2 - How to change the Android package name

In worklight it seems that the android package name is set by the varible ${packageName}.
Where is this variable set? And how can I change it?
Right now the default seems to be com.applicationName. In the app im working on, this package name already exists in Google Play, so I would like to change it to com.corperationName.applicationName.
I know I can do this via Ant during Android project compilation, but I was wondering if there was somewhere within Worklight I can do this.
I was able to do this for Shell and Inner projects by making the following changes to the Shell project (check in or backup the shell and test before committing changes):
Add the following dir structure to the ShellApp/android/native/src
directory: com/corpname/{$appName}
Copy the contents of the ${packageDirectory} directory into the new
{$appName} directory (for me it was
${appName}.java.wltemplate.wluser, ForegroundService.java.wltemplate,
GCMIntentService.java.wltemplate).
In the files copied, every reference to ${packageName} needs to be
replaced with com.corpname.{$appName}
In AndroidManifest.xml.wltemplate.wluser, every reference to
${packageName} needs to be replaced with com.corpname.{$appName}.
Remove the ${packageDirectory} from the project.
Every inner project created from this Shell project should now have the package structure as com.corpname.appname

Queries regarding 'backport-android-bluetooth'

I have started working on android just 1 month back (so please consider my question even if it looks simple). I have my Bluetooth code running on Android 2.2. So I am working on using backport-android-bluetooth to migrate it to Android 1.6 (as I have some old pads with 1.6).
I came across foll. link: http://code.google.com/p/backport-android-bluetooth/
I have queries regarding point no. 1 and 2 under 'Install' which are as follows:
1. download backport-android-bluetooth2.jar, and put into your projects's reference libraries. - Where is exactly 'projects' reference library'? How to put .jar file in that 'reference library
2. put backport_android_bluetooth.properties in to your src directory. -Is this file to be put in 'src' folder or '*Project_Name*' folder like project.properties file?
Thanks in advance.
Put a new file into your src dir,named backport_android_bluetooth.properties
and then put the code below in it:
#permission_name = ${your package name).PERMISSION_BLUETOOTH
permission_name = com.example.bluetooth.PERMISSION_BLUETOOTH
#request_enable = ${your package name}.action.REQUEST_ENABLE
request_enable = com.example.bluetooth.action.REQUEST_ENABLE
#request_discoverable = ${your package name}.action.REQUEST_DISCOVERABLE
request_discoverable = com.example.bluetooth.action.REQUEST_DISCOVERABLE
if u r using eclipse than right click on ur project New/Folder,
give folder name as libs and copy & paste android-bluetooth2.jar in libs folder.
than right click on libs folder go to Build Path/ Configure Build Path
Click on Add Jar, select your project/libs/android-bluetooth2.jar
press ok

Categories

Resources