LoginSignup
1
3

More than 3 years have passed since last update.

AndroidStudioでパッケージ名を変更する

Last updated at Posted at 2017-08-20

追記

最近だと、このようなことをせずともUIの方から変更できるようです。
https://mashmorgan.hatenablog.com/entry/2018/06/06/160823

以前の記事

AndroidStudioでパッケージ名を変更するのは手順が多くて少し面倒です。
なので、Rubyでスクリプトを作って半自動化してみました。

※実行は、バックアップをとった上で自己責任でお願いします。

rename_package.rb
require 'find'
require 'fileutils'
require 'tmpdir'

print "Path : "
path = gets.chop
Dir.chdir(path)
Dir.chdir("app")

# 旧パッケージ名の取得
str = File.read("src/main/AndroidManifest.xml")
old_name = str.slice(/package="(.+?)"/, 1)

puts "Old Package : #{old_name}"
print "New Package : "
new_name = gets.chop

# app 以下の全ファイルの置換
Find.find(".") {|path|
    if FileTest.file?(path) then
        str = File.read(path)

        if str.include?(old_name) then
            puts path
            f = File.open(path, "w")
            f.write(str.gsub(old_name, new_name))
            f.close
        end
    end
}

# ディレクトリ構造の変換
old_dir = old_name.gsub(".", "/")
new_dir = new_name.gsub(".", "/")

Dir.chdir("src/main/java")
Dir.mktmpdir() {|tmp_dir|
    FileUtils.mv(Dir.glob(old_dir + "/*"), tmp_dir)
    FileUtils.rm_r(old_dir.split("/")[0])
    FileUtils.mkdir_p(new_dir)
    FileUtils.mv(Dir.glob(tmp_dir + "/*"), new_dir)
}

参考ページ
http://qiita.com/nusa/items/413c8a131ebc451e80f8

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3