Ant regex for android package names

in my Android project, all my .java view files import an R file based on the name of the current package. When I change the name of my package in the Android manifest (dynamically, and not refactoring through Eclipse or any other IDE), I need the links to all my java files to point to the new R file that is generated

I am building a script through Ant, but I'm not sure if the conditional expression will look like

The lines that need to be replaced are as follows:

import com.mysite.myapp.R;

t

import com.sushiroll.california.R

This is what I have in my ant build script

 <replaceregexp file="src/*" match="import*.R" replace="import ${current.package}.R" byline="true"> <fileset dir="src/."> <include name="*.java"/> </fileset> </replaceregexp> 

where I want to combine all the lines that say import and end with .R

and replace it with import $current.package}.R

how can i say this for regex ant? the match and substitution that I wrote were just hunches

+4
source share
2 answers

You need to group old package name names to .R

if you know the name of the package

  match="import com\.mysite\.myapp\.R;" 

if you now know the name of the package, but be prepared for failure if you import incorrectly, if you import R from different packages

  match="import (.*).R;" 

replaced by

replace="import ${current.package}.R;"

and

  <fileset dir="src"> <include name="**/*.java"/> </fileset> 
+2
source
 <replaceregexp match="import .+?\.R;" replace="import ${current.package}.R;" flags="g" > ... 

where is +? it needs to work if you have two imports per line, and g needs to have more than one import per file replaced. Although you currently have only one file, since you replace all of them with the same line, this is a common regex ...

+1
source

Source: https://habr.com/ru/post/1438075/


All Articles