Cordova Build - ignore files - android

When compiling a cordova application every single file in my /www folder gets copied to the assets/www folder(android) but I'd like to customize what files are copied. I use several pseudo languages like CoffeeScript, Jade or Stylus which are auto-compiled by my IDE and they shouldn't be shipped into the final application.

With the help of this article, I found that you can create/edit the platform/android/ant.properties, and add the following line to it:
aapt.ignore.assets=!*.map:!thumbs.db:!.git:.*:*~
With this line, any file or directory that matches one of these patterns will not be included in the .apk file:
*.map
thumbs.db
.git
.*
*~
I found that editing the platforms/android/build.xml won't work because it's overwritten each time the build is invoked; also, creating a build.xml file in the root of the project didn't work.
The rules for this property are the following, taken from $ANDROID_HOME/tools/ant/build.xml:
<!-- 'aapt.ignore.assets' is the list of file patterns to ignore under /res and /assets.
Default is "!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
Overall patterns syntax is:
[!][<dir>|<file>][*suffix-match|prefix-match*|full-match]:more:patterns...
- The first character flag ! avoids printing a warning.
- Pattern can have the flag "<dir>" to match only directories
or "<file>" to match only files. Default is to match both.
- Match is not case-sensitive.
-->
<property name="aapt.ignore.assets" value="" />

Hidden (.*) folder & files will be ignored by default
All hidden files and folders will be ignored while build for example .git/ & .gitignore
To hide : Rename the folder by a . (dot) prepended to the folder/file name.
Use Terminal in Linux/Mac to rename the folder.
mv dev .dev
Use cmd in Windows
ren dev .dev

I do not know any way how to filter files for cordova, but I can describe you approach that we use in our projects.
We are using gruntjs with phonegap plugin. We configure it to use prebuilt folder (which contains only necessary files and folders) and it:
creates cordova/phonegap project
adds plugins,platforms
builds native applicaiton
launches native application on emulator
Main thing in this approach is that cordova/phonegap project (directory with .cordova, platforms and www folders) is just a build artefact.
Here is relevant part of our Gruntfile.js as an example:
phonegap : {
config : {
root : './out/dist',
config : {
template : './config.tpl.xml',
data: {
id: pkg.id,
version: pkg.version,
name: pkg.name,
author : pkg.author
}
},
path : 'out/phonegap',
plugins : [
// PHONEGAP OFFICIAL PLUGINS
'org.apache.cordova.globalization',
'org.apache.cordova.network-information',
'org.apache.cordova.splashscreen',
//THIRD-PARTY PLUGINS
'de.appplant.cordova.plugin.local-notification'
],
platforms : [
'android'
],
maxBuffer : 200, // You may need to raise this for iOS.
verbose : false,
releases : 'out/releases',
releaseName : function() {
return pkg.name + '-v' + pkg.version;
},
// Android-only integer version to increase with each release.
// See http://developer.android.com/tools/publishing/versioning.html
versionCode : function() {
return 1;
}
}
}
Note, out/dist is generated by previous build step and contains concatenated and minified version of code.

I've created a custom script to ignore files/folders which works not just for android:
Add this to your config.xml:
<ignore entries="lang,foo/bar,ignore/asdf.html" />
And you need two more files located in hooks/before_build and hooks/after_build folders.

I highly recommend cordova-plugin-exclude-files. You simply specify files to exclude as pattern attributes of exclude-files tags in your config.xml file.
For example, to exclude all .scss files:
<exclude-files pattern="**/*.scss" />
And to exclude all files with the string "ios-only" on the Android platform only:
<platform name="android">
<exclude-files pattern="ios-only" /> </platform>

This is what I'm doing. I've moved the src files for the app that was in www folder to another folder created in root - web-app. Next, the following script was added in package.json :
This surely needs to be refined, but am working with it as makeshift arrangement.
Good Luck...

Related

qmake install ignores android /res folder

I found out that I can use INSTALLS to replace ANDROID_PACKAGE_SOURCE_DIR. But for some reason it ignores /res folder.
My project has 2 android related folders. /android and /android_extra.
At first it was all one folder that was included in the build with ANDROID_PACKAGE_SOURCE_DIR, but that's not the best solution, since some files, depending on the build, shouldn't be included.
So I put the optional files inside android_extra.
If I use the ANDROID_PACKAGE_SOURCE_DIR for the mandatory /android folder and INSTALLS for the optional /andorid_extra it doesn't include the /android_extra/src files.
If however I use INSTALLS instead of ANDROID_PACKAGE_SOURCE_DIR it doesn't include the android/res files, but the /android_extra/src will be included...
I don't know what else to try, mainly I'm just looking at how the build system is making fun of me...
Any solution to this, or how to force it to include files?
I also noticed that if I put the INSTALLS path to /res2 it will copy the files, so I assume it can't copy to /res. Probably the same case is for /src with ANDROID_PACKAGE_SOURCE_DIR. Is there some "write protection"?
This is the code part in my .pro file:
# ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
# can be repalced by
ANDROID_SOURCES.path = /
ANDROID_SOURCES.files = $$PWD/android/*
INSTALLS += ANDROID_SOURCES
vmxBuild {
VMX_SOURCES.path = /
VMX_SOURCES.files = $$files($$PWD/android_extra/VmxPlayer/*)
INSTALLS += VMX_SOURCES
}
The compile output seems normal:
qmake -install qinstall /home/user/Projects/myProject/android/res /home/user/Projects/build-myProject-Android_for_arm64_v8a_Clang_Qt_5_12_5_for_Android_ARM64_v8a-Debug/android-build/res
The process "/home/user/Android/android-ndk-r19c/prebuilt/linux-x86_64/bin/make" exited normally.

Assets Folder Not Including HTML Files In NativeScript Android Build

I am trying to display a local html file with JS and CSS and PNG images referenced by it. I put the files into my assets folder of a NativeScript x Angular app for Android. When I run tns run android --bundle and the app gets built - the assets folder does not include the html. Folder structure goes from assets > index.html (and index.html references the other files).
Is there a certain place or way I need to include html files so they get included in the build?
You must update CopyWebpackPlugin in your webpack.config.js to include the asset folder
// Copy assets to out dir. Add your own globs as needed.
new CopyWebpackPlugin([
{ from: "fonts/**" },
{ from: "**/*.jpg" },
{ from: "**/*.png" },
{ from: "assets/**" }, // Add your asset folder path like this
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),

Get config.xml file of Cordova applications using Androguard

Is it possible to get the config.xml file that is generated by Cordova based applications using Adroguard?
I cannot find it under the /res folder and it is not listed in the output of get_files() method.
apktool, on the other hand, is able to get the config.xml from the apk that is use it on.
Since it is under res folder, You need to get it by unzipping the file to a temporary directory and then parse it using Axml parser. In get_files(), you must see "resources.arsc" file. The res files are part of that. You can do something like :
config_xml_path = os.path.join(<your apk unzipped path>, 'res', 'xml', 'config.xml')
with io.open(config_xml_path, 'rb') as axml_file:
parsed_axml_file = AXMLPrinter(axml_file.read())
axml_content = parsed_axml_file.get_buff().decode('utf-8)
parsed_axml = minidom.parseString(axml_content.encode('utf-8'))
You might get some errors if the config.xml is badly formatted but I am not including the solution to handle those case. I hope you will get an idea with the above example.

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.

Why is aapt not adding `<values/>` in the apk?

I am using Titanium which is a cross-platform development Tool. Titanium uses it own building script to create the apk.
I have a Titanium application that uses a native module : a Java library that makes VOIP calls. The Java application works, and when I create a Titanium application with an empty module, it works.
But I have a build problem using a module based on the VOIP module inside the Titanium application : some Resources are missing in the apk.
First the build system merges some Resources from our Titanium application and some native android modules and put them in a directory build. I have for exemple : build/android/res/values/strings.xml file where all keys and values seems ok and valid xml.
Then the build calls aapt and create the apk using also the Titanium Resources for library widgets (ex: strings for notifications). And in this apk, the build/android/res/values/strings.xml is missing. It doesn't exists. The application starts, the VOIP service starts, and then it's crashes as soon that a required missing Resource is called.
I can prove with aapt list or unzipping the apk that strings.xml is missing.
Looking deep in the titanium javascript build file, I see this command is executed :
aapt "package" "-f" "-m" "-J" "/Users/nicorama/ti-voip/build/android/gen"
"-M" "/Users/nicorama/ti-voip/project/build/android/AndroidManifest.xml"
"-S" "/project/build/android/res"
"-S" "/var/folders/6f/twxz46614h7_q/res"
"-S" "/var/folders/6f/twxz46614h7_q/res" ....
The /var/folders/... are files for Titanium widgets. Compilation of the apk fails if I remove them.
I have executed this command outside the build, adding -v for verbose mode and saving result into a log.txt. I'm searching in this file for strings.xml and I find :
Found 18 custom asset files in /Users/nicorama/ti-voip/build/android/bin/assets
Configurations:
(default)
v11
v14
....
Src: () /var/folders/6f/twxz46614h7_q/res/values/ids.xml
values/strings.xml
Src: () /var/folders/6f/twxz46614h7_q/res/values/strings.xml
Src: (af) /var/folders/6f/twxz46614h7_q/res/values-af/strings.xml
Src: (am) /var/folders/6f/twxz46614h7_q/res/values-am/strings.xml
...
But nothing about my /Users/nicorama/ti-voip/build/android/res/values/strings.xml folder which is first in the appt command.
I do have all references needed for images or other xml files thought :
(new resource id hidden from /Users/nicorama/ti-voip/build/android/res/layout/hidden.xml)
(new resource id launcher from /Users/nicorama/ti-voip/build/android/res/layout/launcher.xml)
Any idea where and why this strings.xml has disappeared ?
aapt will merge all strings.xml "values" into resources.arsc, which is why you do not see the file strings.xml in the apk. I double checked this with few of my apks, i do not see the strings.xml inside the apk.
If you would like to see the string values that were packaged with your 'apk', run the below command it will dump all string values.
aapt.exe d strings myapp.apk
Verify some of the string values you defined in your app, are listed in the above string dump. If not, try passing in absolute path to the "res" folder "/Users/nicorama/ti-voip/project/build/android/res" at the beginning:
aapt "package" "-f" "-m" "-J" "/Users/nicorama/ti-voip/build/android/gen"
"-M" "/Users/nicorama/ti-voip/project/build/android/AndroidManifest.xml"
"-S" "/Users/nicorama/ti-voip/project/build/android/res"
"-S" "/var/folders/6f/twxz46614h7_q/res"
"-S" "/var/folders/6f/twxz46614h7_q/res" ....
the new apk should have the xml values, test again with
aapt.exe d strings myapp.apk
to make sure your strings.xml values are in the above string dump.

Categories

Resources