3

i want to rename package name using gradle on cmd.because i have multiple project customization and change package name using ide very difficult to change each package name separately.please give me solution.thanks in advance

 apply plugin: 'com.android.application'
    apply plugin: 'maven'

    android {
        compileSdkVersion 21
        buildToolsVersion "23.0.0 rc2"


        sourceSets {
            defaultConfig {
                applicationId "com.appbell.restaurant.genericresto"
                minSdkVersion 14
                targetSdkVersion 19
                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_1_7
                    targetCompatibility JavaVersion.VERSION_1_7
                }

            }
            main {
                manifest.srcFile 'src\\main\\AndroidManifest.xml'
                println('android'+manifest.srcFile)
            }

        }

        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    }

    task changePackage{
        replaceInManifest("com.appbell.restaurant.example","com.appbell.imenu4u")
    }

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            // Make sure this is at least 0.10.+
            classpath 'com.android.tools.build:gradle:1.0.+'
        }
    }

    task appStart(type: Exec, dependsOn: 'installDebug') {
        // linux
        //commandLine 'adb', 'shell', 'am', 'start', '-n', 'com.example/.MyActivity'
        // windows
         commandLine 'cmd', '/c', 'adb', 'shell', 'am', 'start', '-n', 'com.appbell.restaurant.genericresto/com.appbell.and.resto.and.ui.SplashActivity'
    }

    dependencies {
        compile project(':facebook')
        compile project(':stripe')
        compile 'com.google.android.gms:play-services:+'
        compile 'com.android.support:appcompat-v7:21.0.3'
        compile files('libs/mobileappclasses.jar')
        compile files('libs/pgsdk.jar')
    }

    def replaceInManifest(fromString, toString) {
        def manifestFile = "C:\\Users\\Ntin Gorle\\AndroidstudioProjects\\MyApplication\\RestoAppNEw\\src\\main\\AndroidManifest.xml"
        def updatedContent = new File(manifestFile).getText('UTF-8').replaceAll(fromString, toString)
        new File(manifestFile).write(updatedContent, 'UTF-8')
    }

I this code package name change but not reflect in all .java file.please tell me how to change in all .java classes.

nitin
  • 41
  • 3
  • It's not difficult to change package names with the IDE. It's called refactoring. Rightclick on your package ->Refactor->Rename. – davidgiga1993 Jul 10 '15 at 05:02
  • i want change on command prompt dude using gradle? – nitin Jul 10 '15 at 05:09
  • Was just a hint. Gradle itself can't do refactoring. You'll need to use text replacement just as you already did with your manifest file, but for all *.java files in your project. – davidgiga1993 Jul 10 '15 at 05:14
  • thats my question how to reflect package name in *.java files.i cant understand. – nitin Jul 10 '15 at 05:35
  • nitin Im looking same, found any solution? – Aqib Mumtaz Jul 15 '15 at 11:01
  • Possible duplicate of [Gradle String Replacement - no placeholders](https://stackoverflow.com/questions/10532632/gradle-string-replacement-no-placeholders) – serv-inc Oct 31 '17 at 08:33

1 Answers1

0

If you need to change the package name often and in multiple projects. I recommend the plugin "Android Package Renamer", it's available on the Android Studio plugin market and Intellij IDEA. All you need to do is download it and use it. It works well on many different types of projects.

Usage

  • Open Your Project.
  • Click -> File -> Rename Package
  • Input the Package you want to change.
  • Click Ok.
  • Sync Project with Gradle Files or Invalidate Caches

You can see my detailed answer here or head over to the github repo for a closer look at the instructions.

Phuc VR
  • 111
  • 1
  • 3